test_overwrite.py 5.4 KB

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