1"""test distributed temporal databases with str3ds 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 testRaster3dExtraction(TestCase): 18 19 mapsets_to_remove = [] 20 outfile = 'rast3dlist.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 = "test3d%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("r3.mapcalc", expression="a1 = 100") 36 cls.runModule("r3.mapcalc", expression="a2 = 200") 37 cls.runModule("r3.mapcalc", expression="a3 = 300") 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="str3ds", temporaltype="absolute", 42 output="A", title="A test3d", description="A test3d") 43 cls.runModule( 44 "t.register", flags="i", type="raster_3d", input="A", 45 maps="a1,a2,a3", 46 start="2001-01-01", increment="%i months" % i) 47 48 # Add the new mapsets to the search path 49 for mapset in cls.mapsets_to_remove: 50 cls.runModule("g.mapset", mapset=mapset) 51 cls.runModule("g.mapsets", operation="add", mapset=','.join(cls.mapsets_to_remove)) 52 53 @classmethod 54 def tearDownClass(cls): 55 gisenv = SimpleModule('g.gisenv', get='GISDBASE') 56 cls.runModule(gisenv, expecting_stdout=True) 57 gisdbase = gisenv.outputs.stdout.strip() 58 gisenv = SimpleModule('g.gisenv', get='LOCATION_NAME') 59 cls.runModule(gisenv, expecting_stdout=True) 60 location = gisenv.outputs.stdout.strip() 61 cls.runModule("g.mapset", mapset=cls.old_mapset) 62 for mapset_name in cls.mapsets_to_remove: 63 mapset_path = os.path.join(gisdbase, location, mapset_name) 64 silent_rmtree(mapset_path) 65 66 def test_tlist(self): 67 self.runModule("g.mapset", mapset="test3d1") 68 69 list_string = """A|test3d1|2001-01-01 00:00:00|2001-04-01 00:00:00|3 70 A|test3d2|2001-01-01 00:00:00|2001-07-01 00:00:00|3 71 A|test3d3|2001-01-01 00:00:00|2001-10-01 00:00:00|3 72 A|test3d4|2001-01-01 00:00:00|2002-01-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="str3ds", 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="str3ds", 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="test3d1") 101 102 list_string = """a1|test3d1|2001-01-01 00:00:00|2001-02-01 00:00:00 103 a2|test3d1|2001-02-01 00:00:00|2001-03-01 00:00:00 104 a3|test3d1|2001-03-01 00:00:00|2001-04-01 00:00:00""" 105 106 trast_list = SimpleModule( 107 "t.rast3d.list", quiet=True, flags="s", input="A@test3d1") 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|test3d2|2001-01-01 00:00:00|2001-03-01 00:00:00 116 a2|test3d2|2001-03-01 00:00:00|2001-05-01 00:00:00 117 a3|test3d2|2001-05-01 00:00:00|2001-07-01 00:00:00""" 118 119 trast_list = SimpleModule( 120 "t.rast3d.list", quiet=True, flags="s", input="A@test3d2") 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|test3d3|2001-01-01 00:00:00|2001-04-01 00:00:00 129 a2|test3d3|2001-04-01 00:00:00|2001-07-01 00:00:00 130 a3|test3d3|2001-07-01 00:00:00|2001-10-01 00:00:00""" 131 132 trast_list = SimpleModule( 133 "t.rast3d.list", quiet=True, flags="s", input="A@test3d3") 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|test3d4|2001-01-01 00:00:00|2001-05-01 00:00:00 142 a2|test3d4|2001-05-01 00:00:00|2001-09-01 00:00:00 143 a3|test3d4|2001-09-01 00:00:00|2002-01-01 00:00:00""" 144 145 trast_list = SimpleModule( 146 "t.rast3d.list", quiet=True, flags="s", input="A@test3d4") 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 trast_list = SimpleModule("t.rast3d.list", quiet=True, flags="s", 155 input="A@test3d4", output=self.outfile) 156 self.assertModule(trast_list) 157 self.assertFileExists(self.outfile) 158 with open(self.outfile, 'r') as f: 159 read_data = f.read() 160 for a, b in zip(list_string.split("\n"), read_data.split("\n")): 161 self.assertEqual(a.strip(), b.strip()) 162 if os.path.isfile(self.outfile): 163 os.remove(self.outfile) 164 165 def test_strds_info(self): 166 self.runModule("g.mapset", mapset="test3d4") 167 tinfo_string = """id=A@test3d1 168 name=A 169 mapset=test3d1 170 start_time='2001-01-01 00:00:00' 171 end_time='2001-04-01 00:00:00' 172 granularity='1 month'""" 173 174 info = SimpleModule( 175 "t.info", flags="g", type="str3ds", input="A@test3d1") 176 self.assertModuleKeyValue( 177 module=info, reference=tinfo_string, precision=2, sep="=") 178 179 self.runModule("g.mapset", mapset="test3d3") 180 tinfo_string = """id=A@test3d2 181 name=A 182 mapset=test3d2 183 start_time='2001-01-01 00:00:00' 184 end_time='2001-07-01 00:00:00' 185 granularity='2 months'""" 186 187 info = SimpleModule( 188 "t.info", flags="g", type="str3ds", input="A@test3d2") 189 self.assertModuleKeyValue( 190 module=info, reference=tinfo_string, precision=2, sep="=") 191 192 self.runModule("g.mapset", mapset="test3d2") 193 tinfo_string = """id=A@test3d3 194 name=A 195 mapset=test3d3 196 start_time='2001-01-01 00:00:00' 197 end_time='2001-10-01 00:00:00' 198 granularity='3 months'""" 199 200 info = SimpleModule( 201 "t.info", flags="g", type="str3ds", input="A@test3d3") 202 self.assertModuleKeyValue( 203 module=info, reference=tinfo_string, precision=2, sep="=") 204 205 self.runModule("g.mapset", mapset="test3d1") 206 tinfo_string = """id=A@test3d4 207 name=A 208 mapset=test3d4 209 start_time='2001-01-01 00:00:00' 210 end_time='2002-01-01 00:00:00' 211 granularity='4 months'""" 212 213 info = SimpleModule( 214 "t.info", flags="g", type="str3ds", input="A@test3d4") 215 self.assertModuleKeyValue( 216 module=info, reference=tinfo_string, precision=2, sep="=") 217 218 def test_raster_info(self): 219 self.runModule("g.mapset", mapset="test3d3") 220 tinfo_string = """id=a1@test3d1 221 name=a1 222 mapset=test3d1 223 temporal_type=absolute 224 start_time='2001-01-01 00:00:00' 225 end_time='2001-02-01 00:00:00'""" 226 227 info = SimpleModule( 228 "t.info", flags="g", type="raster_3d", input="a1@test3d1") 229 self.assertModuleKeyValue( 230 module=info, reference=tinfo_string, precision=2, sep="=") 231 232 tinfo_string = """id=a1@test3d2 233 name=a1 234 mapset=test3d2 235 temporal_type=absolute 236 start_time='2001-01-01 00:00:00' 237 end_time='2001-03-01 00:00:00'""" 238 239 info = SimpleModule( 240 "t.info", flags="g", type="raster_3d", input="a1@test3d2") 241 self.assertModuleKeyValue( 242 module=info, reference=tinfo_string, precision=2, sep="=") 243 244 tinfo_string = """id=a1@test3d3 245 name=a1 246 mapset=test3d3 247 temporal_type=absolute 248 start_time='2001-01-01 00:00:00' 249 end_time='2001-04-01 00:00:00'""" 250 251 info = SimpleModule( 252 "t.info", flags="g", type="raster_3d", input="a1@test3d3") 253 self.assertModuleKeyValue( 254 module=info, reference=tinfo_string, precision=2, sep="=") 255 256 tinfo_string = """id=a1@test3d4 257 name=a1 258 mapset=test3d4 259 temporal_type=absolute 260 start_time='2001-01-01 00:00:00' 261 end_time='2001-05-01 00:00:00'""" 262 263 info = SimpleModule( 264 "t.info", flags="g", type="raster_3d", input="a1@test3d4") 265 self.assertModuleKeyValue( 266 module=info, reference=tinfo_string, precision=2, sep="=") 267 268if __name__ == '__main__': 269 from grass.gunittest.main import test 270 test() 271