test_r_support.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """Test of r.support basic functionality
  2. @author Maris Nartiss
  3. @copyright 2021 by Maris Nartiss and the GRASS Development Team
  4. @license This program is free software under the GNU General Public License (>=v2).
  5. Read the file COPYING that comes with GRASS
  6. for details
  7. """
  8. import random
  9. import string
  10. from grass.gunittest.case import TestCase
  11. from grass.gunittest.main import test
  12. from grass.script.core import tempname
  13. from grass.pygrass.gis import Mapset
  14. from grass.pygrass import utils
  15. from grass.lib.gis import G_remove_misc
  16. from grass.lib.raster import Rast_read_semantic_label
  17. class RSupportSemanticLabelHandlingTestCase(TestCase):
  18. @classmethod
  19. def setUpClass(cls):
  20. cls.map = tempname(10)
  21. cls.mapset = Mapset().name
  22. cls.semantic_label = "The_Doors"
  23. cls.use_temp_region()
  24. cls.runModule("g.region", n=1, s=0, e=1, w=0, res=1)
  25. cls.runModule("r.mapcalc", expression="{} = 1".format(cls.map))
  26. @classmethod
  27. def tearDownClass(cls):
  28. cls.del_temp_region()
  29. cls.runModule("g.remove", flags="f", type="raster", name=cls.map)
  30. def test_exclusitivity(self):
  31. self.assertModuleFail(
  32. "r.support", map=self.map, semantic_label=self.semantic_label, b=True
  33. )
  34. def test_semantic_label_invalid(self):
  35. self.assertModuleFail(
  36. "r.support",
  37. map=self.map,
  38. semantic_label="".join(random.choices(string.ascii_letters, k=256)),
  39. )
  40. def test_set_semantic_label(self):
  41. G_remove_misc("cell_misc", "semantic_label", self.map)
  42. self.assertModule("r.support", map=self.map, semantic_label=self.semantic_label)
  43. ret = utils.decode(Rast_read_semantic_label(self.map, self.mapset))
  44. self.assertEqual(ret, self.semantic_label)
  45. def test_remove_semantic_label(self):
  46. self.assertModule("r.support", map=self.map, semantic_label=self.semantic_label)
  47. ret = Rast_read_semantic_label(self.map, self.mapset)
  48. self.assertTrue(bool(ret))
  49. self.assertModule("r.support", map=self.map, b=True)
  50. ret = Rast_read_semantic_label(self.map, self.mapset)
  51. self.assertFalse(bool(ret))
  52. if __name__ == "__main__":
  53. test()