diff --git a/raster/r.clump/tests/conftest.py b/raster/r.clump/tests/conftest.py new file mode 100644 index 00000000000..393bd31ba25 --- /dev/null +++ b/raster/r.clump/tests/conftest.py @@ -0,0 +1,50 @@ +import os +import pytest +import grass.script as gs + + +@pytest.fixture +def setup_maps(tmp_path): + """Set up a GRASS session and create test raster maps.""" + + # Initialize GRASS project + project = tmp_path / "r_clump_project" + gs.create_project(project) + with gs.setup.init(project, env=os.environ.copy()) as session: + # Set the region + gs.run_command( + "g.region", + n=3, + s=0, + e=3, + w=0, + res=1, + env=session.env, + ) + + gs.mapcalc( + "custom_map = " + "if(row() == 1 && col() == 1, 5, " + "if(row() == 1 && col() == 2, 5.5, " + "if(row() == 2 && col() == 2, 5, " + "if(row() == 3 && col() >= 2, 5.5, null()))))", + overwrite=True, + env=session.env, + ) + + gs.mapcalc( + "custom_map1 = " + "if(row() == 1 && col() == 1, 1, " + "if(row() == 1 && col() == 2, 2, " + "if(row() == 1 && col() == 3, 3, " + "if(row() == 2 && col() == 1, 4, " + "if(row() == 2 && col() == 2, 5, " + "if(row() == 2 && col() == 3, 6, " + "if(row() == 3 && col() == 1, 7, " + "if(row() == 3 && col() == 2, 8, " + "if(row() == 3 && col() == 3, 9, null())))))))))", + overwrite=True, + env=session.env, + ) + + yield session # Pass the session to tests diff --git a/raster/r.clump/tests/test_clump.py b/raster/r.clump/tests/test_clump.py new file mode 100644 index 00000000000..c719a2a209f --- /dev/null +++ b/raster/r.clump/tests/test_clump.py @@ -0,0 +1,117 @@ +import grass.script as gs + + +def results_check(actual_categories, expected_categories): + # Test if keys match + assert set(actual_categories.keys()) == set(expected_categories.keys()) + + # Test to check if labels are empty + for key in expected_categories.keys(): + assert actual_categories[key] == expected_categories[key] + + +def test_clump_basic(setup_maps): + """Test basic clumped map.""" + session = setup_maps + gs.run_command( + "r.clump", + input="custom_map", + output="clumped_map", + overwrite=True, + env=session.env, + ) + + output_maps = gs.parse_command("g.list", type="raster", env=session.env) + assert "clumped_map" in output_maps, "Output raster map 'clumped_map' should exist" + + category_output = gs.parse_command("r.category", map="clumped_map", env=session.env) + + actual_categories = { + int(line.split("\t")[0]): line.split("\t")[1].strip() if "\t" in line else "" + for line in category_output + } + + expected_categories = {1: "", 2: "", 3: "", 4: ""} + + results_check(actual_categories, expected_categories) + + +def test_clump_diagonal(setup_maps): + """Test clumped map with diagonal connectivity.""" + session = setup_maps + gs.run_command( + "r.clump", + input="custom_map", + output="clumped_map", + flags="d", + overwrite=True, + env=session.env, + ) + + output_maps = gs.parse_command("g.list", type="raster", env=session.env) + assert "clumped_map" in output_maps, "Output raster map 'clumped_map' should exist" + + category_output = gs.parse_command("r.category", map="clumped_map", env=session.env) + + actual_categories = { + int(line.split("\t")[0]): line.split("\t")[1].strip() if "\t" in line else "" + for line in category_output + } + + expected_categories = {1: "", 2: "", 3: ""} + + results_check(actual_categories, expected_categories) + + +def test_clump_minsize(setup_maps): + """Test clumped map with minimum size parameter.""" + session = setup_maps + gs.run_command( + "r.clump", + input="custom_map", + output="clumped_map", + minsize=2, + overwrite=True, + env=session.env, + ) + + output_maps = gs.parse_command("g.list", type="raster", env=session.env) + assert "clumped_map" in output_maps, "Output raster map 'clumped_map' should exist" + + category_output = gs.parse_command("r.category", map="clumped_map", env=session.env) + + actual_categories = { + int(line.split("\t")[0]): line.split("\t")[1].strip() if "\t" in line else "" + for line in category_output + } + + expected_categories = {1: "", 2: ""} + + results_check(actual_categories, expected_categories) + + +def test_clump_threshold(setup_maps): + """Test clumped map with threshold parameter.""" + session = setup_maps + gs.run_command( + "r.clump", + input="custom_map1", + output="clumped_map", + threshold=0.2, + overwrite=True, + env=session.env, + ) + + output_maps = gs.parse_command("g.list", type="raster", env=session.env) + assert "clumped_map" in output_maps, "Output raster map 'clumped_map' should exist" + + category_output = gs.parse_command("r.category", map="clumped_map", env=session.env) + + actual_categories = { + int(line.split("\t")[0]): line.split("\t")[1].strip() if "\t" in line else "" + for line in category_output + } + + expected_categories = {1: "", 2: "", 3: ""} + + results_check(actual_categories, expected_categories)