1"""test distributed temporal databases with strds
2
3(C) 2014 by the GRASS Development Team
4This program is free software under the GNU General Public
5License (>=v2). Read the file COPYING that comes with GRASS
6for details.
7
8:authors: Soeren Gebbert
9"""
10
11from grass.gunittest.case import TestCase
12from grass.gunittest.gmodules import SimpleModule
13from grass.gunittest.utils import silent_rmtree
14import os
15
16
17class TestRasterExtraction(TestCase):
18
19    mapsets_to_remove = []
20    outfile = 'rastlist.txt'
21    gisenv = SimpleModule('g.gisenv', get='MAPSET')
22    TestCase.runModule(gisenv, expecting_stdout=True)
23    old_mapset = gisenv.outputs.stdout.strip()
24
25    @classmethod
26    def setUpClass(cls):
27        os.putenv("GRASS_OVERWRITE", "1")
28        for i in range(1, 7):
29            mapset_name = "test%i" % i
30            cls.runModule("g.mapset", flags="c", mapset=mapset_name)
31            cls.mapsets_to_remove.append(mapset_name)
32            cls.runModule("g.region", s=0, n=80,
33                          w=0, e=120, b=0, t=50, res=10, res3=10)
34            cls.runModule("t.connect", flags="d")
35            cls.runModule("t.info", flags="d")
36            cls.runModule("r.mapcalc", expression="a1 = 100")
37            cls.runModule("r.mapcalc", expression="a2 = 200")
38            cls.runModule("r.mapcalc", expression="a3 = 300")
39            cls.runModule("t.create", type="strds", temporaltype="absolute",
40                          output="A", title="A test", description="A test")
41            cls.runModule("t.register", flags="i", type="raster", input="A",
42                          maps="a1,a2,a3",
43                          start="2001-01-01", increment="%i months" % i)
44
45        # Add the new mapsets to the search path
46        for mapset in cls.mapsets_to_remove:
47            cls.runModule("g.mapset", mapset=mapset)
48            cls.runModule("g.mapsets", operation="add", mapset=','.join(cls.mapsets_to_remove))
49
50
51    @classmethod
52    def tearDownClass(cls):
53        gisenv = SimpleModule('g.gisenv', get='GISDBASE')
54        cls.runModule(gisenv, expecting_stdout=True)
55        gisdbase = gisenv.outputs.stdout.strip()
56        gisenv = SimpleModule('g.gisenv', get='LOCATION_NAME')
57        cls.runModule(gisenv, expecting_stdout=True)
58        location = gisenv.outputs.stdout.strip()
59        cls.runModule("g.mapset", mapset=cls.old_mapset)
60        for mapset_name in cls.mapsets_to_remove:
61            mapset_path = os.path.join(gisdbase, location, mapset_name)
62            silent_rmtree(mapset_path)
63
64    def test_tlist(self):
65        self.runModule("g.mapset", mapset="test1")
66
67        list_string = """A|test1|2001-01-01 00:00:00|2001-04-01 00:00:00|3
68                                A|test2|2001-01-01 00:00:00|2001-07-01 00:00:00|3
69                                A|test3|2001-01-01 00:00:00|2001-10-01 00:00:00|3
70                                A|test4|2001-01-01 00:00:00|2002-01-01 00:00:00|3
71                                A|test5|2001-01-01 00:00:00|2002-04-01 00:00:00|3
72                                A|test6|2001-01-01 00:00:00|2002-07-01 00:00:00|3"""
73
74        t_list = SimpleModule(
75            "t.list", quiet=True,
76            columns=["name", "mapset,start_time", "end_time", "number_of_maps"],
77            type="strds", where='name = "A"')
78        self.assertModule(t_list)
79
80        out = t_list.outputs["stdout"].value
81
82        for a, b in zip(list_string.split("\n"), out.split("\n")):
83            self.assertEqual(a.strip(), b.strip())
84
85        t_list = SimpleModule(
86            "t.list", quiet=True,
87            columns=["name", "mapset,start_time", "end_time", "number_of_maps"],
88            type="strds", where='name = "A"', output=self.outfile)
89        self.assertModule(t_list)
90        self.assertFileExists(self.outfile)
91        with open(self.outfile, 'r') as f:
92            read_data = f.read()
93        for a, b in zip(list_string.split("\n"), read_data.split("\n")):
94            self.assertEqual(a.strip(), b.strip())
95        #self.assertLooksLike(reference=read_data, actual=list_string)
96        if os.path.isfile(self.outfile):
97            os.remove(self.outfile)
98
99    def test_trast_list(self):
100        self.runModule("g.mapset", mapset="test1")
101
102        list_string = """a1|test1|2001-01-01 00:00:00|2001-02-01 00:00:00
103                                a2|test1|2001-02-01 00:00:00|2001-03-01 00:00:00
104                                a3|test1|2001-03-01 00:00:00|2001-04-01 00:00:00"""
105
106        trast_list = SimpleModule(
107            "t.rast.list", quiet=True, flags="u", input="A@test1")
108        self.assertModule(trast_list)
109
110        out = trast_list.outputs["stdout"].value
111
112        for a, b in zip(list_string.split("\n"), out.split("\n")):
113            self.assertEqual(a.strip(), b.strip())
114
115        list_string = """a1|test2|2001-01-01 00:00:00|2001-03-01 00:00:00
116                                a2|test2|2001-03-01 00:00:00|2001-05-01 00:00:00
117                                a3|test2|2001-05-01 00:00:00|2001-07-01 00:00:00"""
118
119        trast_list = SimpleModule(
120            "t.rast.list", quiet=True, flags="u", input="A@test2")
121        self.assertModule(trast_list)
122
123        out = trast_list.outputs["stdout"].value
124
125        for a, b in zip(list_string.split("\n"), out.split("\n")):
126            self.assertEqual(a.strip(), b.strip())
127
128        list_string = """a1|test3|2001-01-01 00:00:00|2001-04-01 00:00:00
129                                a2|test3|2001-04-01 00:00:00|2001-07-01 00:00:00
130                                a3|test3|2001-07-01 00:00:00|2001-10-01 00:00:00"""
131
132        trast_list = SimpleModule(
133            "t.rast.list", quiet=True, flags="u", input="A@test3")
134        self.assertModule(trast_list)
135
136        out = trast_list.outputs["stdout"].value
137
138        for a, b in zip(list_string.split("\n"), out.split("\n")):
139            self.assertEqual(a.strip(), b.strip())
140
141        list_string = """a1|test4|2001-01-01 00:00:00|2001-05-01 00:00:00
142                                a2|test4|2001-05-01 00:00:00|2001-09-01 00:00:00
143                                a3|test4|2001-09-01 00:00:00|2002-01-01 00:00:00"""
144
145        trast_list = SimpleModule(
146            "t.rast.list", quiet=True, flags="u", input="A@test4")
147        self.assertModule(trast_list)
148
149        out = trast_list.outputs["stdout"].value
150
151        for a, b in zip(list_string.split("\n"), out.split("\n")):
152            self.assertEqual(a.strip(), b.strip())
153
154        list_string = """a1|test5|2001-01-01 00:00:00|2001-06-01 00:00:00
155                                a2|test5|2001-06-01 00:00:00|2001-11-01 00:00:00
156                                a3|test5|2001-11-01 00:00:00|2002-04-01 00:00:00"""
157
158        trast_list = SimpleModule(
159            "t.rast.list", quiet=True, flags="u", input="A@test5")
160        self.assertModule(trast_list)
161
162        out = trast_list.outputs["stdout"].value
163
164        for a, b in zip(list_string.split("\n"), out.split("\n")):
165            self.assertEqual(a.strip(), b.strip())
166
167        trast_list = SimpleModule("t.rast.list", quiet=True, flags="u",
168                                  input="A@test5", output=self.outfile)
169        self.assertModule(trast_list)
170        self.assertFileExists(self.outfile)
171        with open(self.outfile, 'r') as f:
172            read_data = f.read()
173        for a, b in zip(list_string.split("\n"), read_data.split("\n")):
174            self.assertEqual(a.strip(), b.strip())
175        if os.path.isfile(self.outfile):
176            os.remove(self.outfile)
177
178    def test_strds_info(self):
179        self.runModule("g.mapset", mapset="test4")
180        tinfo_string = """id=A@test1
181                                    name=A
182                                    mapset=test1
183                                    start_time='2001-01-01 00:00:00'
184                                    end_time='2001-04-01 00:00:00'
185                                    granularity='1 month'"""
186
187        info = SimpleModule("t.info", flags="g", input="A@test1")
188        self.assertModuleKeyValue(
189            module=info, reference=tinfo_string, precision=2, sep="=")
190
191        self.runModule("g.mapset", mapset="test3")
192        tinfo_string = """id=A@test2
193                                    name=A
194                                    mapset=test2
195                                    start_time='2001-01-01 00:00:00'
196                                    end_time='2001-07-01 00:00:00'
197                                    granularity='2 months'"""
198
199        info = SimpleModule("t.info", flags="g", input="A@test2")
200        self.assertModuleKeyValue(
201            module=info, reference=tinfo_string, precision=2, sep="=")
202
203        self.runModule("g.mapset", mapset="test2")
204        tinfo_string = """id=A@test3
205                                    name=A
206                                    mapset=test3
207                                    start_time='2001-01-01 00:00:00'
208                                    end_time='2001-10-01 00:00:00'
209                                    granularity='3 months'"""
210
211        info = SimpleModule("t.info", flags="g", input="A@test3")
212        self.assertModuleKeyValue(
213            module=info, reference=tinfo_string, precision=2, sep="=")
214
215        self.runModule("g.mapset", mapset="test1")
216        tinfo_string = """id=A@test4
217                                    name=A
218                                    mapset=test4
219                                    start_time='2001-01-01 00:00:00'
220                                    end_time='2002-01-01 00:00:00'
221                                    granularity='4 months'"""
222
223        info = SimpleModule("t.info", flags="g", input="A@test4")
224        self.assertModuleKeyValue(
225            module=info, reference=tinfo_string, precision=2, sep="=")
226
227        tinfo_string = """id=A@test5
228                                    name=A
229                                    mapset=test5
230                                    start_time='2001-01-01 00:00:00'
231                                    end_time='2002-04-01 00:00:00'
232                                    granularity='5 months'"""
233
234        info = SimpleModule("t.info", flags="g", input="A@test5")
235        self.assertModuleKeyValue(
236            module=info, reference=tinfo_string, precision=2, sep="=")
237
238    def test_raster_info(self):
239        self.runModule("g.mapset", mapset="test3")
240        tinfo_string = """id=a1@test1
241                                name=a1
242                                mapset=test1
243                                temporal_type=absolute
244                                start_time='2001-01-01 00:00:00'
245                                end_time='2001-02-01 00:00:00'"""
246
247        info = SimpleModule(
248            "t.info", flags="g", type="raster", input="a1@test1")
249        self.assertModuleKeyValue(
250            module=info, reference=tinfo_string, precision=2, sep="=")
251
252        tinfo_string = """id=a1@test2
253                                name=a1
254                                mapset=test2
255                                temporal_type=absolute
256                                start_time='2001-01-01 00:00:00'
257                                end_time='2001-03-01 00:00:00'"""
258
259        info = SimpleModule(
260            "t.info", flags="g", type="raster", input="a1@test2")
261        self.assertModuleKeyValue(
262            module=info, reference=tinfo_string, precision=2, sep="=")
263
264        tinfo_string = """id=a1@test3
265                                name=a1
266                                mapset=test3
267                                temporal_type=absolute
268                                start_time='2001-01-01 00:00:00'
269                                end_time='2001-04-01 00:00:00'"""
270
271        info = SimpleModule(
272            "t.info", flags="g", type="raster", input="a1@test3")
273        self.assertModuleKeyValue(
274            module=info, reference=tinfo_string, precision=2, sep="=")
275
276        tinfo_string = """id=a1@test4
277                                name=a1
278                                mapset=test4
279                                temporal_type=absolute
280                                start_time='2001-01-01 00:00:00'
281                                end_time='2001-05-01 00:00:00'"""
282
283        info = SimpleModule(
284            "t.info", flags="g", type="raster", input="a1@test4")
285        self.assertModuleKeyValue(
286            module=info, reference=tinfo_string, precision=2, sep="=")
287
288        tinfo_string = """id=a1@test5
289                                name=a1
290                                mapset=test5
291                                temporal_type=absolute
292                                start_time='2001-01-01 00:00:00'
293                                end_time='2001-06-01 00:00:00'"""
294
295        info = SimpleModule(
296            "t.info", flags="g", type="raster", input="a1@test5")
297
298
299if __name__ == '__main__':
300    from grass.gunittest.main import test
301    test()
302