1"""test distributed temporal databases with stvds
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 = 'vectlist.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, 5):
29            mapset_name = "testvect%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            # Use always the current mapset as temporal database
35            cls.runModule("v.random", output="a1", npoints=20)
36            cls.runModule("v.random", output="a2", npoints=20)
37            cls.runModule("v.random", output="a3", npoints=20)
38            # Create the temporal database
39            cls.runModule("t.connect", flags="d")
40            cls.runModule("t.info", flags="d")
41            cls.runModule("t.create", type="stvds", temporaltype="absolute",
42                          output="A", title="A testvect", description="A testvect")
43            cls.runModule("t.register", flags="i", type="vector", input="A",
44                          maps="a1,a2,a3",
45                          start="2001-01-01", increment="%i months" % i)
46
47        # Add the new mapsets to the search path
48        for mapset in cls.mapsets_to_remove:
49            cls.runModule("g.mapset", mapset=mapset)
50            cls.runModule("g.mapsets", operation="add", mapset=','.join(cls.mapsets_to_remove))
51
52    @classmethod
53    def tearDownClass(cls):
54        gisenv = SimpleModule('g.gisenv', get='GISDBASE')
55        cls.runModule(gisenv, expecting_stdout=True)
56        gisdbase = gisenv.outputs.stdout.strip()
57        gisenv = SimpleModule('g.gisenv', get='LOCATION_NAME')
58        cls.runModule(gisenv, expecting_stdout=True)
59        location = gisenv.outputs.stdout.strip()
60        cls.runModule("g.mapset", mapset=cls.old_mapset)
61        for mapset_name in cls.mapsets_to_remove:
62            mapset_path = os.path.join(gisdbase, location, mapset_name)
63            silent_rmtree(mapset_path)
64
65    def test_tlist(self):
66        self.runModule("g.mapset", mapset="testvect1")
67
68        list_string = """A|testvect1|2001-01-01 00:00:00|2001-04-01 00:00:00|3
69                                A|testvect2|2001-01-01 00:00:00|2001-07-01 00:00:00|3
70                                A|testvect3|2001-01-01 00:00:00|2001-10-01 00:00:00|3
71                                A|testvect4|2001-01-01 00:00:00|2002-01-01 00:00:00|3"""
72
73        t_list = SimpleModule(
74            "t.list", quiet=True,
75            columns=["name", "mapset,start_time", "end_time", "number_of_maps"],
76            type="stvds", where='name = "A"')
77        self.assertModule(t_list)
78
79        out = t_list.outputs["stdout"].value
80
81        for a, b in zip(list_string.split("\n"), out.split("\n")):
82            self.assertEqual(a.strip(), b.strip())
83
84        t_list = SimpleModule(
85            "t.list", quiet=True,
86            columns=["name", "mapset,start_time", "end_time", "number_of_maps"],
87            type="stvds", where='name = "A"', output=self.outfile)
88        self.assertModule(t_list)
89        self.assertFileExists(self.outfile)
90        with open(self.outfile, 'r') as f:
91            read_data = f.read()
92        for a, b in zip(list_string.split("\n"), read_data.split("\n")):
93            self.assertEqual(a.strip(), b.strip())
94        #self.assertLooksLike(reference=read_data, actual=list_string)
95        if os.path.isfile(self.outfile):
96            os.remove(self.outfile)
97
98    def test_tvect_list(self):
99        self.runModule("g.mapset", mapset="testvect1")
100
101        list_string = """a1|testvect1|2001-01-01 00:00:00|2001-02-01 00:00:00
102                                a2|testvect1|2001-02-01 00:00:00|2001-03-01 00:00:00
103                                a3|testvect1|2001-03-01 00:00:00|2001-04-01 00:00:00"""
104
105        trast_list = SimpleModule("t.vect.list", quiet=True, flags="u",
106                                  columns=[
107                                      "name", "mapset", "start_time", "end_time"],
108                                  input="A@testvect1")
109        self.assertModule(trast_list)
110
111        out = trast_list.outputs["stdout"].value
112
113        for a, b in zip(list_string.split("\n"), out.split("\n")):
114            self.assertEqual(a.strip(), b.strip())
115
116        list_string = """a1|testvect2|2001-01-01 00:00:00|2001-03-01 00:00:00
117                                a2|testvect2|2001-03-01 00:00:00|2001-05-01 00:00:00
118                                a3|testvect2|2001-05-01 00:00:00|2001-07-01 00:00:00"""
119
120        trast_list = SimpleModule(
121            "t.vect.list", quiet=True, flags="u",
122            columns=["name", "mapset", "start_time", "end_time"],
123            input="A@testvect2")
124        self.assertModule(trast_list)
125
126        out = trast_list.outputs["stdout"].value
127
128        for a, b in zip(list_string.split("\n"), out.split("\n")):
129            self.assertEqual(a.strip(), b.strip())
130
131        list_string = """a1|testvect3|2001-01-01 00:00:00|2001-04-01 00:00:00
132                                a2|testvect3|2001-04-01 00:00:00|2001-07-01 00:00:00
133                                a3|testvect3|2001-07-01 00:00:00|2001-10-01 00:00:00"""
134
135        trast_list = SimpleModule("t.vect.list", quiet=True, flags="u",
136                                  columns=[
137                                      "name", "mapset", "start_time", "end_time"],
138                                  input="A@testvect3")
139        self.assertModule(trast_list)
140
141        out = trast_list.outputs["stdout"].value
142
143        for a, b in zip(list_string.split("\n"), out.split("\n")):
144            self.assertEqual(a.strip(), b.strip())
145
146        list_string = """a1|testvect4|2001-01-01 00:00:00|2001-05-01 00:00:00
147                                a2|testvect4|2001-05-01 00:00:00|2001-09-01 00:00:00
148                                a3|testvect4|2001-09-01 00:00:00|2002-01-01 00:00:00"""
149
150        trast_list = SimpleModule(
151            "t.vect.list", quiet=True, flags="u",
152            columns=["name", "mapset", "start_time", "end_time"],
153            input="A@testvect4")
154        self.assertModule(trast_list)
155
156        out = trast_list.outputs["stdout"].value
157
158        for a, b in zip(list_string.split("\n"), out.split("\n")):
159            self.assertEqual(a.strip(), b.strip())
160
161        trast_list = SimpleModule("t.vect.list", quiet=True, flags="u",
162                                  columns=["name", "mapset", "start_time", "end_time"],
163                                  input="A@testvect4", output=self.outfile)
164        self.assertModule(trast_list)
165        self.assertFileExists(self.outfile)
166        with open(self.outfile, 'r') as f:
167            read_data = f.read()
168        for a, b in zip(list_string.split("\n"), read_data.split("\n")):
169            self.assertEqual(a.strip(), b.strip())
170        if os.path.isfile(self.outfile):
171            os.remove(self.outfile)
172
173    def test_stvds_info(self):
174        self.runModule("g.mapset", mapset="testvect4")
175        tinfo_string = """id=A@testvect1
176                                    name=A
177                                    mapset=testvect1
178                                    start_time='2001-01-01 00:00:00'
179                                    end_time='2001-04-01 00:00:00'
180                                    granularity='1 month'"""
181
182        info = SimpleModule(
183            "t.info", flags="g", type="stvds", input="A@testvect1")
184        self.assertModuleKeyValue(
185            module=info, reference=tinfo_string, precision=2, sep="=")
186
187        self.runModule("g.mapset", mapset="testvect3")
188        tinfo_string = """id=A@testvect2
189                                    name=A
190                                    mapset=testvect2
191                                    start_time='2001-01-01 00:00:00'
192                                    end_time='2001-07-01 00:00:00'
193                                    granularity='2 months'"""
194
195        info = SimpleModule(
196            "t.info", flags="g", type="stvds", input="A@testvect2")
197        self.assertModuleKeyValue(
198            module=info, reference=tinfo_string, precision=2, sep="=")
199
200        self.runModule("g.mapset", mapset="testvect2")
201        tinfo_string = """id=A@testvect3
202                                    name=A
203                                    mapset=testvect3
204                                    start_time='2001-01-01 00:00:00'
205                                    end_time='2001-10-01 00:00:00'
206                                    granularity='3 months'"""
207
208        info = SimpleModule(
209            "t.info", flags="g", type="stvds", input="A@testvect3")
210        self.assertModuleKeyValue(
211            module=info, reference=tinfo_string, precision=2, sep="=")
212
213        self.runModule("g.mapset", mapset="testvect1")
214        tinfo_string = """id=A@testvect4
215                                    name=A
216                                    mapset=testvect4
217                                    start_time='2001-01-01 00:00:00'
218                                    end_time='2002-01-01 00:00:00'
219                                    granularity='4 months'"""
220
221        info = SimpleModule(
222            "t.info", flags="g", type="stvds", input="A@testvect4")
223        self.assertModuleKeyValue(
224            module=info, reference=tinfo_string, precision=2, sep="=")
225
226    def testv_vector_info(self):
227        self.runModule("g.mapset", mapset="testvect3")
228        tinfo_string = """id=a1@testvect1
229                                name=a1
230                                mapset=testvect1
231                                temporal_type=absolute
232                                start_time='2001-01-01 00:00:00'
233                                end_time='2001-02-01 00:00:00'"""
234
235        info = SimpleModule(
236            "t.info", flags="g", type="vector", input="a1@testvect1")
237        self.assertModuleKeyValue(
238            module=info, reference=tinfo_string, precision=2, sep="=")
239
240        tinfo_string = """id=a1@testvect2
241                                name=a1
242                                mapset=testvect2
243                                temporal_type=absolute
244                                start_time='2001-01-01 00:00:00'
245                                end_time='2001-03-01 00:00:00'"""
246
247        info = SimpleModule(
248            "t.info", flags="g", type="vector", input="a1@testvect2")
249        self.assertModuleKeyValue(
250            module=info, reference=tinfo_string, precision=2, sep="=")
251
252        tinfo_string = """id=a1@testvect3
253                                name=a1
254                                mapset=testvect3
255                                temporal_type=absolute
256                                start_time='2001-01-01 00:00:00'
257                                end_time='2001-04-01 00:00:00'"""
258
259        info = SimpleModule(
260            "t.info", flags="g", type="vector", input="a1@testvect3")
261        self.assertModuleKeyValue(
262            module=info, reference=tinfo_string, precision=2, sep="=")
263
264        tinfo_string = """id=a1@testvect4
265                                name=a1
266                                mapset=testvect4
267                                temporal_type=absolute
268                                start_time='2001-01-01 00:00:00'
269                                end_time='2001-05-01 00:00:00'"""
270
271        info = SimpleModule(
272            "t.info", flags="g", type="vector", input="a1@testvect4")
273        self.assertModuleKeyValue(
274            module=info, reference=tinfo_string, precision=2, sep="=")
275
276if __name__ == '__main__':
277    from grass.gunittest.main import test
278    test()
279