From 65ecf17a3c95c48507b33260a8ecce766caa49b1 Mon Sep 17 00:00:00 2001 From: Valyrian-Code Date: Sun, 12 Jul 2026 20:29:03 +0530 Subject: [PATCH 1/4] r.null: migrate tests from gunittest to pytest --- raster/r.null/tests/r_null_test.py | 59 +++++++++++++++++ raster/r.null/testsuite/test_r_null.py | 87 -------------------------- 2 files changed, 59 insertions(+), 87 deletions(-) create mode 100644 raster/r.null/tests/r_null_test.py delete mode 100644 raster/r.null/testsuite/test_r_null.py diff --git a/raster/r.null/tests/r_null_test.py b/raster/r.null/tests/r_null_test.py new file mode 100644 index 00000000000..ff5683ab289 --- /dev/null +++ b/raster/r.null/tests/r_null_test.py @@ -0,0 +1,59 @@ +import os + +import pytest + +import grass.script as gs +from grass.tools import Tools + + +@pytest.fixture +def session_with_maps(tmp_path): + """A GRASS session with small rasters for r.null. + + map_basic: integer categories 1, 2, 3 by row + map_fill_nulls: NULL, 2, 3 by row + map_float: floating point values 1, 2.5, 3 by row + """ + project = tmp_path / "r_null_project" + gs.create_project(project) + with gs.setup.init(project, env=os.environ.copy()) as session: + tools = Tools(session=session) + tools.g_region(n=3, s=0, e=3, w=0, res=1) + tools.r_mapcalc(expression="map_basic = row()") + tools.r_mapcalc( + expression="map_fill_nulls = if(row() == 1, null(), if(row() == 2, 2, 3))" + ) + tools.r_mapcalc( + expression="map_float = if(row() == 1, 1, if(row() == 2, 2.5, 3))" + ) + yield session + + +def test_setnull_integer(session_with_maps): + """Cells matching setnull become NULL in an integer map (-i).""" + tools = Tools(session=session_with_maps) + + tools.r_null(map="map_basic", setnull="1", flags="i") + + described = tools.r_describe(map="map_basic", format="json", flags="1").json + assert described == {"has_nulls": True, "values": [2, 3]} + + +def test_setnull_float(session_with_maps): + """Cells matching setnull become NULL in a floating point map (-f).""" + tools = Tools(session=session_with_maps) + + tools.r_null(map="map_float", setnull="1", flags="f") + + described = tools.r_describe(map="map_float", format="json", flags="r").json + assert described == {"has_nulls": True, "ranges": [{"min": 2.5, "max": 3}]} + + +def test_fill_nulls(session_with_maps): + """NULL cells are replaced by the null= value.""" + tools = Tools(session=session_with_maps) + + tools.r_null(map="map_fill_nulls", null="1") + + described = tools.r_describe(map="map_fill_nulls", format="json", flags="1").json + assert described == {"has_nulls": False, "values": [1, 2, 3]} diff --git a/raster/r.null/testsuite/test_r_null.py b/raster/r.null/testsuite/test_r_null.py deleted file mode 100644 index 8532a246f18..00000000000 --- a/raster/r.null/testsuite/test_r_null.py +++ /dev/null @@ -1,87 +0,0 @@ -import grass.script as gs -from grass.gunittest.case import TestCase -from grass.gunittest.main import test -from grass.gunittest.gmodules import SimpleModule - - -class TestRNull(TestCase): - """Test case for r.null module""" - - @classmethod - def setUpClass(cls): - """Set up a temporary region and create test raster maps""" - cls.use_temp_region() - cls.runModule("g.region", n=3, s=0, e=3, w=0, res=1) - - # Create map1: categories 1, 2, 3 as rows - cls.runModule( - "r.mapcalc", - expression="map_basic = row()", - overwrite=True, - ) - - # Create map1: categories null, 2, 3 as rows - cls.runModule( - "r.mapcalc", - expression="map_fill_nulls = if(row() == 1, null(), if(row() == 2, 2, if(row() == 3, 3, null())))", - overwrite=True, - ) - - # Create map1: categories 1, 2.5, 3 as rows - cls.runModule( - "r.mapcalc", - expression="map2 = if(row() == 1, 1, if(row() == 2, 2.5, if(row() == 3, 3, null())))", - overwrite=True, - ) - - @classmethod - def tearDownClass(cls): - """Remove temporary maps and region""" - cls.runModule( - "g.remove", - flags="f", - type="raster", - name=["map_basic", "map_fill_nulls", "map2"], - ) - cls.del_temp_region() - - def test_basic(self): - """Verify module execute with -i flag""" - module = SimpleModule("r.null", map="map_basic", setnull="1", flags="i") - self.assertModule(module) - - # Validate category mappings using r.category - category_output = gs.parse_command( - "r.describe", map="map_basic", format="json", flags="1" - ) - expected_output = {"has_nulls": True, "values": [2, 3]} - - self.assertEqual(category_output, expected_output) - - def test_float_flag(self): - """Verify module execute with -f flag""" - module = SimpleModule("r.null", map="map2", setnull="1", flags="f") - self.assertModule(module) - - category_output = gs.parse_command( - "r.describe", map="map2", format="json", flags="r" - ) - expected_output = {"has_nulls": True, "ranges": [{"min": 2.5, "max": 3}]} - - self.assertEqual(category_output, expected_output) - - def test_fill_nulls(self): - """Verify module fills nulls""" - module = SimpleModule("r.null", map="map_fill_nulls", null="1") - self.assertModule(module) - - category_output = gs.parse_command( - "r.describe", map="map_fill_nulls", format="json", flags="1" - ) - expected_output = {"has_nulls": False, "values": [1, 2, 3]} - - self.assertEqual(category_output, expected_output) - - -if __name__ == "__main__": - test() From 190eb6d193f24a65c2392027f8bb119e88a9a141 Mon Sep 17 00:00:00 2001 From: Rajveer* Date: Sun, 12 Jul 2026 22:05:58 +0530 Subject: [PATCH 2/4] Update raster/r.null/tests/r_null_test.py Co-authored-by: Vaclav Petras --- raster/r.null/tests/r_null_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raster/r.null/tests/r_null_test.py b/raster/r.null/tests/r_null_test.py index ff5683ab289..763fced9d40 100644 --- a/raster/r.null/tests/r_null_test.py +++ b/raster/r.null/tests/r_null_test.py @@ -33,7 +33,7 @@ def test_setnull_integer(session_with_maps): """Cells matching setnull become NULL in an integer map (-i).""" tools = Tools(session=session_with_maps) - tools.r_null(map="map_basic", setnull="1", flags="i") + tools.r_null(map="map_basic", setnull=1, flags="i") described = tools.r_describe(map="map_basic", format="json", flags="1").json assert described == {"has_nulls": True, "values": [2, 3]} From 5e815505af73320c57b43d41f32ecb7e6dcf1177 Mon Sep 17 00:00:00 2001 From: Rajveer* Date: Sun, 12 Jul 2026 22:06:07 +0530 Subject: [PATCH 3/4] Update raster/r.null/tests/r_null_test.py Co-authored-by: Vaclav Petras --- raster/r.null/tests/r_null_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raster/r.null/tests/r_null_test.py b/raster/r.null/tests/r_null_test.py index 763fced9d40..ad093d6a404 100644 --- a/raster/r.null/tests/r_null_test.py +++ b/raster/r.null/tests/r_null_test.py @@ -53,7 +53,7 @@ def test_fill_nulls(session_with_maps): """NULL cells are replaced by the null= value.""" tools = Tools(session=session_with_maps) - tools.r_null(map="map_fill_nulls", null="1") + tools.r_null(map="map_fill_nulls", null=1) described = tools.r_describe(map="map_fill_nulls", format="json", flags="1").json assert described == {"has_nulls": False, "values": [1, 2, 3]} From c28ea8a6f35ac3f3afc2c5238be1c0f263894104 Mon Sep 17 00:00:00 2001 From: Rajveer* Date: Sun, 12 Jul 2026 22:06:13 +0530 Subject: [PATCH 4/4] Update raster/r.null/tests/r_null_test.py Co-authored-by: Vaclav Petras --- raster/r.null/tests/r_null_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raster/r.null/tests/r_null_test.py b/raster/r.null/tests/r_null_test.py index ad093d6a404..c31487f4d3c 100644 --- a/raster/r.null/tests/r_null_test.py +++ b/raster/r.null/tests/r_null_test.py @@ -43,7 +43,7 @@ def test_setnull_float(session_with_maps): """Cells matching setnull become NULL in a floating point map (-f).""" tools = Tools(session=session_with_maps) - tools.r_null(map="map_float", setnull="1", flags="f") + tools.r_null(map="map_float", setnull=1, flags="f") described = tools.r_describe(map="map_float", format="json", flags="r").json assert described == {"has_nulls": True, "ranges": [{"min": 2.5, "max": 3}]}