test_g_mremove.py 3.8 KB

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