test_overwrite.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """g.remove tests
  2. (C) 2013 by the GRASS Development Team
  3. This program is free software under the GNU General Public
  4. License (>=v2). Read the file COPYING that comes with GRASS
  5. for details.
  6. :author: Vaclav Petras
  7. """
  8. from grass.gunittest.case import TestCase
  9. from grass.gunittest.main import test
  10. from grass.gunittest.gmodules import SimpleModule
  11. from grass.gunittest.gutils import is_map_in_mapset
  12. from grass.gunittest.checkers import (text_to_keyvalue, keyvalue_equals,
  13. diff_keyvalue)
  14. class RasterRenameTestCase(TestCase):
  15. """Test wrong input of parameters for g.list module"""
  16. def setUp(self):
  17. """Create maps in a small region.
  18. The raster exists must be renewed for every test.
  19. """
  20. self.use_temp_region()
  21. self.runModule("g.region", s=0, n=5, w=0, e=5, res=1)
  22. self.runModule("r.mapcalc", expression="rename_1 = 1")
  23. self.runModule("r.mapcalc", expression="rename_2 = 20")
  24. self.runModule("r.mapcalc", expression="rename_3 = 300")
  25. self.runModule("r.mapcalc", expression="exists = 50000")
  26. self.to_remove = ['rename_1', 'rename_2', 'rename_3', 'exists']
  27. def tearDown(self):
  28. """Remove temporary region and renamed maps (and also old if needed)"""
  29. self.runModule('g.remove', name=self.to_remove, type=['raster'], flags='f')
  30. self.del_temp_region()
  31. def test_raster(self):
  32. """Test that raster rename works"""
  33. module = SimpleModule('g.rename', raster=['rename_1', 'renamed_1'])
  34. self.assertModule(module)
  35. new_names = ['renamed_1']
  36. self.to_remove.extend(new_names)
  37. for name in new_names:
  38. self.assertRasterExists(name)
  39. def test_preserve_existing_raster(self):
  40. """Test that existing raster is preserved"""
  41. # TODO: write the same for other types
  42. # TODO: create a general functions to avoid duplication
  43. runivar = SimpleModule('r.univar', flags='g', map='exists')
  44. self.runModule(runivar, expecting_stdout=True)
  45. original_runivar = text_to_keyvalue(runivar.outputs.stdout,
  46. sep='=', skip_empty=True)
  47. module = SimpleModule('g.rename', raster=['rename_3', 'exists'], overwrite=False)
  48. self.assertModule(module)
  49. self.assertRasterExists('exists', msg="Destination (existing) map (to) should exist")
  50. self.assertRasterExists('rename_3', msg="Source map (from) should exist")
  51. runivar = SimpleModule('r.univar', flags='g', map='exists')
  52. self.runModule(runivar, expecting_stdout=True)
  53. new_runivar = text_to_keyvalue(runivar.outputs.stdout,
  54. sep='=', skip_empty=True)
  55. if not keyvalue_equals(dict_a=original_runivar, dict_b=new_runivar,
  56. precision=1e-7):
  57. unused, missing, mismatch = diff_keyvalue(dict_a=original_runivar,
  58. dict_b=new_runivar,
  59. precision=1e-7)
  60. if mismatch:
  61. msg = "Raster map changed. It was probably overwritten.\n"
  62. msg += "Difference between r.univar of maps:\n"
  63. msg += "mismatch values"
  64. msg += " (key, reference, actual): %s\n" % mismatch
  65. self.fail(msg)
  66. def test_overwrite_existing_raster(self):
  67. """Test that existing raster is overridden if desired"""
  68. runivar_source = SimpleModule('r.univar', flags='g', map='rename_3')
  69. self.runModule(runivar_source, expecting_stdout=True)
  70. original_runivar_source = text_to_keyvalue(runivar_source.outputs.stdout,
  71. sep='=', skip_empty=True)
  72. runivar_target = SimpleModule('r.univar', flags='g', map='exists')
  73. self.runModule(runivar_target, expecting_stdout=True)
  74. original_runivar_target = text_to_keyvalue(runivar_target.outputs.stdout,
  75. sep='=', skip_empty=True)
  76. module = SimpleModule('g.rename', raster=['rename_3', 'exists'], overwrite=True)
  77. self.assertModule(module)
  78. self.assertRasterExists('exists', msg="Destination (here: existing) map (to) should exist after rename")
  79. self.assertFalse(is_map_in_mapset('rename_3', type='raster'),
  80. msg="Source map (from) should not exist after rename")
  81. runivar = SimpleModule('r.univar', flags='g', map='exists')
  82. self.runModule(runivar, expecting_stdout=True)
  83. new_runivar = text_to_keyvalue(runivar.outputs.stdout,
  84. sep='=', skip_empty=True)
  85. # both these tests are probably redundant but let's test thoroughly
  86. if keyvalue_equals(dict_a=original_runivar_target, dict_b=new_runivar,
  87. precision=1e-7):
  88. msg = "Raster map did not change. It probably wasn't overwritten."
  89. self.fail(msg)
  90. if not keyvalue_equals(dict_a=original_runivar_source, dict_b=new_runivar,
  91. precision=1e-7):
  92. unused, missing, mismatch = diff_keyvalue(dict_a=original_runivar_source,
  93. dict_b=new_runivar,
  94. precision=1e-7)
  95. if mismatch:
  96. msg = "Destination raster map is not the same as source."
  97. msg += " It probably wasn't overwritten.\n"
  98. msg += "Difference between r.univar of maps:\n"
  99. msg += "mismatch values"
  100. msg += " (key, reference, actual): %s\n" % mismatch
  101. self.fail(msg)
  102. if __name__ == '__main__':
  103. test()