|
| 1 | +import grass.script as gs |
| 2 | +from grass.gunittest.case import TestCase |
| 3 | +from grass.gunittest.main import test |
| 4 | +from grass.gunittest.gmodules import SimpleModule |
| 5 | + |
| 6 | + |
| 7 | +class TestRNull(TestCase): |
| 8 | + """Test case for r.null module""" |
| 9 | + |
| 10 | + @classmethod |
| 11 | + def setUpClass(cls): |
| 12 | + """Set up a temporary region and create test raster maps""" |
| 13 | + cls.use_temp_region() |
| 14 | + cls.runModule("g.region", n=3, s=0, e=3, w=0, res=1) |
| 15 | + |
| 16 | + # Create map1: categories 1, 2, 3 as rows |
| 17 | + cls.runModule( |
| 18 | + "r.mapcalc", |
| 19 | + expression="map_basic = row()", |
| 20 | + overwrite=True, |
| 21 | + ) |
| 22 | + |
| 23 | + # Create map1: categories null, 2, 3 as rows |
| 24 | + cls.runModule( |
| 25 | + "r.mapcalc", |
| 26 | + expression="map_fill_nulls = if(row() == 1, null(), if(row() == 2, 2, if(row() == 3, 3, null())))", |
| 27 | + overwrite=True, |
| 28 | + ) |
| 29 | + |
| 30 | + # Create map1: categories 1, 2.5, 3 as rows |
| 31 | + cls.runModule( |
| 32 | + "r.mapcalc", |
| 33 | + expression="map2 = if(row() == 1, 1, if(row() == 2, 2.5, if(row() == 3, 3, null())))", |
| 34 | + overwrite=True, |
| 35 | + ) |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def tearDownClass(cls): |
| 39 | + """Remove temporary maps and region""" |
| 40 | + cls.runModule( |
| 41 | + "g.remove", |
| 42 | + flags="f", |
| 43 | + type="raster", |
| 44 | + name=["map_basic", "map_fill_nulls", "map2"], |
| 45 | + ) |
| 46 | + cls.del_temp_region() |
| 47 | + |
| 48 | + def test_basic(self): |
| 49 | + """Verify module execute with -i flag""" |
| 50 | + module = SimpleModule("r.null", map="map_basic", setnull="1", flags="i") |
| 51 | + self.assertModule(module) |
| 52 | + |
| 53 | + # Validate category mappings using r.category |
| 54 | + category_output = gs.parse_command( |
| 55 | + "r.describe", map="map_basic", format="json", flags="1" |
| 56 | + ) |
| 57 | + expected_output = {"has_nulls": True, "values": [2, 3]} |
| 58 | + |
| 59 | + self.assertEqual(category_output, expected_output) |
| 60 | + |
| 61 | + def test_float_flag(self): |
| 62 | + """Verify module execute with -f flag""" |
| 63 | + module = SimpleModule("r.null", map="map2", setnull="1", flags="f") |
| 64 | + self.assertModule(module) |
| 65 | + |
| 66 | + category_output = gs.parse_command( |
| 67 | + "r.describe", map="map2", format="json", flags="r" |
| 68 | + ) |
| 69 | + expected_output = {"has_nulls": True, "ranges": [{"min": 2.5, "max": 3}]} |
| 70 | + |
| 71 | + self.assertEqual(category_output, expected_output) |
| 72 | + |
| 73 | + def test_fill_nulls(self): |
| 74 | + """Verify module fills nulls""" |
| 75 | + module = SimpleModule("r.null", map="map_fill_nulls", null="1") |
| 76 | + self.assertModule(module) |
| 77 | + |
| 78 | + category_output = gs.parse_command( |
| 79 | + "r.describe", map="map_fill_nulls", format="json", flags="1" |
| 80 | + ) |
| 81 | + expected_output = {"has_nulls": False, "values": [1, 2, 3]} |
| 82 | + |
| 83 | + self.assertEqual(category_output, expected_output) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + test() |
0 commit comments