| 1 |
import os |
| 2 |
from pathlib import Path |
| 3 |
import grass.script as gs |
| 4 |
from grass.gunittest.case import TestCase |
| 5 |
from grass.gunittest.main import test |
| 6 |
|
| 7 |
|
| 8 |
class TestITarget(TestCase): |
| 9 |
"""Regression tests for the i.target GRASS module.""" |
| 10 |
|
| 11 |
@classmethod |
| 12 |
def setUpClass(cls): |
| 13 |
cls.use_temp_region() |
| 14 |
cls.runModule("g.region", n=50, s=0, e=50, w=0, rows=5, cols=5) |
| 15 |
cls.runModule("r.mapcalc", expression="test_base = 100", overwrite=True) |
| 16 |
cls.runModule("r.mapcalc", expression="test_extra = 200", overwrite=True) |
| 17 |
cls.groups = ["test_group1", "test_group2", "test_group3"] |
| 18 |
for group in cls.groups: |
| 19 |
cls.runModule("i.group", group=group, input="test_base") |
| 20 |
|
| 21 |
@classmethod |
| 22 |
def tearDownClass(cls): |
| 23 |
cls.del_temp_region() |
| 24 |
cls.runModule("g.remove", type="raster", name="test_base,test_extra", flags="f") |
| 25 |
cls.runModule("g.remove", type="group", name=cls.groups, flags="f") |
| 26 |
|
| 27 |
def _target_path(self, group): |
| 28 |
"""Return full filesystem path to group's TARGET file.""" |
| 29 |
env = gs.gisenv() |
| 30 |
return os.path.join( |
| 31 |
env["GISDBASE"], |
| 32 |
env["LOCATION_NAME"], |
| 33 |
env["MAPSET"], |
| 34 |
"group", |
| 35 |
group, |
| 36 |
"TARGET", |
| 37 |
) |
| 38 |
|
| 39 |
def _get_target_values(self, group): |
| 40 |
"""Return (location, mapset) tuple from group's TARGET file.""" |
| 41 |
with open(self._target_path(group)) as f: |
| 42 |
return (f.readline().strip(), f.readline().strip()) |
| 43 |
|
| 44 |
def test_current_location_flag(self): |
| 45 |
"""Verify -c flag creates target with current environment values.""" |
| 46 |
group = self.groups[0] |
| 47 |
target_file = self._target_path(group) |
| 48 |
|
| 49 |
if Path(target_file).exists(): |
| 50 |
os.remove(target_file) |
| 51 |
|
| 52 |
self.assertModule("i.target", flags="c", group=group) |
| 53 |
self.assertTrue(Path(target_file).exists()) |
| 54 |
|
| 55 |
current_env = gs.gisenv() |
| 56 |
loc, mset = self._get_target_values(group) |
| 57 |
self.assertEqual(loc, current_env["LOCATION_NAME"]) |
| 58 |
self.assertEqual(mset, current_env["MAPSET"]) |
| 59 |
|
| 60 |
def test_specific_location_mapset(self): |
| 61 |
"""Verify explicit location/mapset parameters set correct values.""" |
| 62 |
group = self.groups[0] |
| 63 |
current_env = gs.gisenv() |
| 64 |
|
| 65 |
self.assertModule( |
| 66 |
"i.target", |
| 67 |
group=group, |
| 68 |
location=current_env["LOCATION_NAME"], |
| 69 |
mapset=current_env["MAPSET"], |
| 70 |
) |
| 71 |
|
| 72 |
loc, mset = self._get_target_values(group) |
| 73 |
self.assertEqual(loc, current_env["LOCATION_NAME"]) |
| 74 |
self.assertEqual(mset, current_env["MAPSET"]) |
| 75 |
|
| 76 |
def test_independent_group_targets(self): |
| 77 |
"""Verify target settings don't propagate between groups. |
| 78 |
Ensures configuration changes in one group don't affect others.""" |
| 79 |
group1, group2 = self.groups[0], self.groups[1] |
| 80 |
self.assertModule("i.target", group=group1, location="loc1", mapset="mapset1") |
| 81 |
self.assertModule("i.target", group=group2, location="loc2", mapset="mapset2") |
| 82 |
|
| 83 |
loc1, mset1 = self._get_target_values(group1) |
| 84 |
loc2, mset2 = self._get_target_values(group2) |
| 85 |
self.assertNotEqual((loc1, mset1), (loc2, mset2)) |
| 86 |
|
| 87 |
def test_target_survives_group_modification(self): |
| 88 |
"""Ensure modifying a group's composition does not reset its target settings.""" |
| 89 |
group = self.groups[1] |
| 90 |
self.assertModule("i.target", flags="c", group=group) |
| 91 |
|
| 92 |
self.runModule("i.group", group=group, input="test_base", flags="r") |
| 93 |
self.runModule("i.group", group=group, input="test_extra") |
| 94 |
|
| 95 |
current_env = gs.gisenv() |
| 96 |
loc, mset = self._get_target_values(group) |
| 97 |
self.assertEqual(loc, current_env["LOCATION_NAME"]) |
| 98 |
self.assertEqual(mset, current_env["MAPSET"]) |
| 99 |
|
| 100 |
def test_subgroups_persist_after_target_change(self): |
| 101 |
"""Verify subgroup existence after target modification.""" |
| 102 |
group = self.groups[2] |
| 103 |
subgroup = "test_sub" |
| 104 |
|
| 105 |
self.runModule("i.group", group=group, subgroup=subgroup, input="test_base") |
| 106 |
self.assertModule( |
| 107 |
"i.target", group=group, location="new_loc", mapset="PERMANENT" |
| 108 |
) |
| 109 |
|
| 110 |
subgroups = gs.read_command("i.group", flags="sg", group=group).split() |
| 111 |
self.assertIn(subgroup, subgroups) |
| 112 |
|
| 113 |
|
| 114 |
if __name__ == "__main__": |
| 115 |
test() |