test_g_remove.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """Test of g.remove module"""
  2. # TODO: rmapcalc probably fatals, replace or add raise on error?
  3. from grass.script.raster import mapcalc as rmapcalc
  4. from grass.gunittest.case import TestCase
  5. from grass.gunittest.main import test
  6. from grass.gunittest.gutils import get_current_mapset
  7. from grass.gunittest.gmodules import SimpleModule
  8. # when used user1 must be replaced by current mapset
  9. REMOVE_RASTERS = """raster/test_map_0@user1
  10. raster/test_map_1@user1
  11. raster/test_map_2@user1
  12. raster/test_map_3@user1
  13. raster/test_map_4@user1
  14. raster/test_map_5@user1
  15. raster/test_map_6@user1
  16. raster/test_map_7@user1
  17. raster/test_map_8@user1
  18. raster/test_map_9@user1
  19. raster/test_two@user1
  20. """
  21. REMOVING_RASTERS_LOG = """Removing raster <test_map_0>
  22. Removing raster <test_map_1>
  23. Removing raster <test_map_2>
  24. Removing raster <test_map_3>
  25. Removing raster <test_map_4>
  26. Removing raster <test_map_5>
  27. Removing raster <test_map_6>
  28. Removing raster <test_map_7>
  29. Removing raster <test_map_8>
  30. Removing raster <test_map_9>
  31. Removing raster <test_two>
  32. """
  33. class GMRemoveTest(TestCase):
  34. """Test removing with g.remove"""
  35. @classmethod
  36. def setUpClass(cls):
  37. """Set up small region for fast map creation."""
  38. cls.use_temp_region()
  39. cls.runModule("g.region", s=0, n=5, w=0, e=5, res=1)
  40. @classmethod
  41. def tearDownClass(cls):
  42. """Remove temporary region"""
  43. cls.del_temp_region()
  44. def test_remove_procedure(self):
  45. """Test that maps are removed only with -f"""
  46. for i in range(0, 10):
  47. rmapcalc("test_map_%i = 100" % i)
  48. rmapcalc("test_two = 2")
  49. module = SimpleModule("g.remove", type="raster", pattern="test_map_*,*two")
  50. self.assertModule(module)
  51. self.assertMultiLineEqual(
  52. module.outputs.stdout, REMOVE_RASTERS.replace("user1", get_current_mapset())
  53. )
  54. module = SimpleModule(
  55. "g.remove", type="raster", pattern="test_map_*,*two", flags="f"
  56. )
  57. self.assertModule(module)
  58. self.assertMultiLineEqual(module.outputs.stdout, "")
  59. self.assertMultiLineEqual(module.outputs.stderr, REMOVING_RASTERS_LOG)
  60. def test_remove_procedure_exclude(self):
  61. """Test that exclude does not list excluded maps"""
  62. rmapcalc("test_apples = 100")
  63. rmapcalc("test_oranges = 200")
  64. rmapcalc("test_apples_big = 300")
  65. rmapcalc("test_apples_small = 300")
  66. module = SimpleModule(
  67. "g.remove",
  68. type="raster",
  69. pattern="test_{apples,oranges}*",
  70. exclude="*_small",
  71. )
  72. self.assertModule(module)
  73. self.assertMultiLineEqual(
  74. module.outputs.stdout,
  75. "raster/test_apples@user1\n"
  76. "raster/test_apples_big@user1\n"
  77. "raster/test_oranges@user1\n".replace("user1", get_current_mapset()),
  78. )
  79. module = SimpleModule(
  80. "g.remove",
  81. type="raster",
  82. pattern="test_{apples,oranges}{_small,_big,*}",
  83. flags="f",
  84. )
  85. self.assertModule(module)
  86. self.assertMultiLineEqual(module.outputs.stdout, "")
  87. self.assertRegexpMatches(
  88. module.outputs.stderr, "(.*<.+>[^\n]*\n){4}", msg="4 maps should be removed"
  89. )
  90. class GMRemoveWrongInputTest(TestCase):
  91. """Test wrong input of parameters for g.remove module"""
  92. def test_re_flags(self):
  93. """Test that -r and -e flags are exclusive"""
  94. module = SimpleModule(
  95. "g.remove", flags="re", type="raster", pattern="xxxyyyzzz"
  96. )
  97. self.assertModuleFail(module)
  98. stderr = module.outputs.stderr
  99. self.assertIn("-r", stderr)
  100. self.assertIn("-e", stderr)
  101. if __name__ == "__main__":
  102. test()