Debian Codegraph @

temporal/t.rename/testsuite/test_t_rename.py

raw
1 """Test t.register
2 (C) 2014-2023 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 Soeren Gebbert
7 Ported to Python by Stefan Blumentrath
8
9 Tests the rename module of space time datasets
10
11 We need to set a specific region in the
12 @preprocess step of this test. We generate
13 raster with r.mapcalc and create two space time raster inputs
14 with absolute time
15
16 The region setting should work for UTM and LL test locations
17 """
18
19 import grass.script as gs
20 import grass.temporal as tgis
21 from grass.gunittest.case import TestCase
22 from grass.gunittest.gmodules import SimpleModule
23
24
25 class TestRenameSTDS(TestCase):
26 @classmethod
27 def setUpClass(cls):
28 """Initiate the temporal GIS and set the region"""
29
30 tgis.init()
31 cls.use_temp_region()
32 cls.runModule(
33 "g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10, flags="p3"
34 )
35
36 # Generate data
37 cls.runModule("r.mapcalc", expression="prec_1 = rand(0, 550)", overwrite=True)
38 cls.runModule("r.mapcalc", expression="prec_2 = rand(0, 450)", overwrite=True)
39
40 # We need to create three space time dataset
41 cls.runModule(
42 "t.create",
43 verbose=True,
44 overwrite=True,
45 type="strds",
46 temporaltype="absolute",
47 output="precip_abs1",
48 title="Test",
49 description="This is the 1 test strds",
50 semantictype="sum",
51 )
52 cls.runModule(
53 "t.register",
54 flags="i",
55 overwrite=True,
56 input="precip_abs1",
57 maps="prec_1,prec_2",
58 start="2001-01-01",
59 increment="1 seconds",
60 )
61 cls.runModule(
62 "t.create",
63 verbose=True,
64 overwrite=True,
65 type="strds",
66 temporaltype="absolute",
67 output="precip_abs2",
68 title="Test",
69 description="This is the 2 test strds",
70 semantictype="sum",
71 )
72 cls.runModule(
73 "t.register", overwrite=True, input="precip_abs2", maps="prec_1,prec_2"
74 )
75 cls.runModule(
76 "t.create",
77 verbose=True,
78 overwrite=True,
79 type="strds",
80 temporaltype="absolute",
81 output="precip_abs3",
82 title="Test",
83 description="This is the 3 test strds",
84 semantictype="sum",
85 )
86 cls.runModule(
87 "t.register", overwrite=True, input="precip_abs3", maps="prec_1,prec_2"
88 )
89 cls.runModule(
90 "t.create",
91 verbose=True,
92 overwrite=True,
93 type="strds",
94 temporaltype="absolute",
95 output="precip_abs4",
96 title="Test",
97 description="This is the 4 test strds",
98 semantictype="sum",
99 )
100 cls.runModule(
101 "t.create",
102 verbose=True,
103 overwrite=True,
104 type="strds",
105 temporaltype="absolute",
106 output="precip_abs5",
107 title="Test",
108 description="This is the 5 test strds",
109 semantictype="sum",
110 )
111
112 @classmethod
113 def tearDownClass(cls):
114 """Remove the temporary region"""
115 cls.runModule(
116 "t.remove",
117 flags="rf",
118 verbose=True,
119 type="strds",
120 inputs="precip_abs2,precip_abs4,precip_abs5,precip_abs6",
121 )
122 # cls.runModule("t.unregister", type="raster", maps="prec_1,prec_2")
123 cls.runModule("g.remove", flags="f", type="raster", name="prec_1,prec_2")
124
125 cls.del_temp_region()
126
127 def test_rename(self):
128 """Test renaming a space time raster dataset"""
129 rename_module = SimpleModule(
130 "t.rename",
131 overwrite=True,
132 type="strds",
133 input="precip_abs3",
134 output="precip_abs6",
135 )
136 self.assertModule(rename_module)
137
138 strds = tgis.open_old_stds("precip_abs6", type="strds")
139
140 print(gs.read_command("t.info", type="strds", input="precip_abs6"))
141
142 self.assertEqual(strds.metadata.get_description(), "This is the 3 test strds")
143 self.assertTrue("t.rename" in strds.metadata.get_command())
144
145 def test_rename_with_overwrite(self):
146 """Test renaming a space time raster dataset while overwriting
147 an existing one"""
148 rename_module = SimpleModule(
149 "t.rename",
150 overwrite=True,
151 type="strds",
152 input="precip_abs1",
153 output="precip_abs2",
154 )
155 self.assertModule(rename_module)
156
157 strds = tgis.open_old_stds("precip_abs2", type="strds")
158
159 print(gs.read_command("t.info", type="strds", input="precip_abs2"))
160
161 self.assertEqual(strds.metadata.get_description(), "This is the 1 test strds")
162 self.assertTrue("t.rename" in strds.metadata.get_command())
163
164 def test_fail_overwrite_without_flag(self):
165 """Test that renaming a space time raster dataset fails if the
166 output stds exists and the overwrite flag is not set"""
167 rename_module = SimpleModule(
168 "t.rename",
169 type="strds",
170 input="precip_abs2",
171 output="precip_abs4",
172 )
173 self.assertModuleFail(rename_module)
174
175 def test_fail_rename_to_other_mapset(self):
176 """Test that renaming a space time raster dataset fails if the
177 output name contains the name of a mapset that is not the current
178 one"""
179 rename_module = SimpleModule(
180 "t.rename",
181 type="strds",
182 input="precip_abs2",
183 output="precip_abs7@PERMANENT",
184 )
185 self.assertModuleFail(rename_module)
186
187
188 if __name__ == "__main__":
189 from grass.gunittest.main import test
190
191 test()