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