| 1 | # -*- coding: utf-8 -*-
|
|---|
| 2 |
|
|---|
| 3 | """
|
|---|
| 4 | Tests assertion methods.
|
|---|
| 5 | """
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | import os
|
|---|
| 9 |
|
|---|
| 10 | import grass.script.core as gcore
|
|---|
| 11 | from grass.pygrass.modules import Module
|
|---|
| 12 |
|
|---|
| 13 | from grass.gunittest.case import TestCase
|
|---|
| 14 | from grass.gunittest.main import test
|
|---|
| 15 | from grass.gunittest.gmodules import SimpleModule
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | class TestTextAssertions(TestCase):
|
|---|
| 19 | # pylint: disable=R0904
|
|---|
| 20 |
|
|---|
| 21 | std_newline = "aaa\nbbb\n"
|
|---|
| 22 | platfrom_newline = "aaa{nl}bbb{nl}".format(nl=os.linesep)
|
|---|
| 23 |
|
|---|
| 24 | def test_assertLooksLike(self):
|
|---|
| 25 | self.assertLooksLike("Generated map is <elevation>",
|
|---|
| 26 | "Generated map is <...>")
|
|---|
| 27 | self.assertRaises(self.failureException,
|
|---|
| 28 | self.assertLooksLike,
|
|---|
| 29 | "Generated map is elevation.",
|
|---|
| 30 | "Generated map is <...>")
|
|---|
| 31 | self.assertLooksLike("Projection string: '+proj=longlat +datum=WGS84'",
|
|---|
| 32 | "Projection string: ...")
|
|---|
| 33 |
|
|---|
| 34 | def test_assertLooksLike_multiline(self):
|
|---|
| 35 | self.assertLooksLike("a=123\nb=456\nc=789",
|
|---|
| 36 | "a=...\nb=...\nc=...")
|
|---|
| 37 |
|
|---|
| 38 | def test_assertLooksLike_multiline_platform_dependent(self):
|
|---|
| 39 | self.assertLooksLike("a=123\nb=456\nc=789",
|
|---|
| 40 | "a=...{nl}b=...{nl}c=...".format(nl=os.linesep))
|
|---|
| 41 |
|
|---|
| 42 | def test_assertLooksLike_numbers(self):
|
|---|
| 43 | self.assertLooksLike("abc = 125521",
|
|---|
| 44 | "abc = 125...")
|
|---|
| 45 | self.assertLooksLike("abc = 689.156",
|
|---|
| 46 | "abc = 689...")
|
|---|
| 47 | self.assertLooksLike("abc = 689.159589",
|
|---|
| 48 | "abc = 689.15...")
|
|---|
| 49 | # this should fail according to the implementation
|
|---|
| 50 | # first three dots are considered as ellipses
|
|---|
| 51 | self.assertRaises(self.failureException,
|
|---|
| 52 | self.assertLooksLike,
|
|---|
| 53 | "abc = 689.159589",
|
|---|
| 54 | "abc = 689....")
|
|---|
| 55 |
|
|---|
| 56 | def do_all_combidnations(self, first, second, function):
|
|---|
| 57 | function(first, first)
|
|---|
| 58 | function(first, second)
|
|---|
| 59 | function(second, first)
|
|---|
| 60 | function(second, second)
|
|---|
| 61 |
|
|---|
| 62 | def test_assertMultiLineEqual(self):
|
|---|
| 63 | r"""Test different combinations of ``\n`` and os.linesep"""
|
|---|
| 64 | self.do_all_combidnations(self.std_newline, self.platfrom_newline,
|
|---|
| 65 | function=self.assertMultiLineEqual)
|
|---|
| 66 |
|
|---|
| 67 | def test_assertMultiLineEqual_raises(self):
|
|---|
| 68 | """Test mixed line endings"""
|
|---|
| 69 | self.assertRaises(self.failureException,
|
|---|
| 70 | self.assertMultiLineEqual,
|
|---|
| 71 | "aaa\n\rbbb\r",
|
|---|
| 72 | "aaa\nbbb\n")
|
|---|
| 73 |
|
|---|
| 74 | def test_assertEqual(self):
|
|---|
| 75 | """Test for of newlines for strings (uses overwritten assertMultiLineEqual())"""
|
|---|
| 76 | self.do_all_combidnations(self.std_newline, self.platfrom_newline,
|
|---|
| 77 | function=self.assertEqual)
|
|---|
| 78 |
|
|---|
| 79 | def test_assertEqual_raises(self):
|
|---|
| 80 | """Test mixed line endings"""
|
|---|
| 81 | self.assertRaises(self.failureException,
|
|---|
| 82 | self.assertEqual,
|
|---|
| 83 | "aaa\n\rbbb\r",
|
|---|
| 84 | "aaa\nbbb\n")
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | R_UNIVAR_ELEVATION_SUBSET = """n=2025000
|
|---|
| 88 | null_cells=0
|
|---|
| 89 | min=55.5787925720215
|
|---|
| 90 | max=156.329864501953
|
|---|
| 91 | """
|
|---|
| 92 |
|
|---|
| 93 | RANDOM_KEYVALUES = """abc=2025000
|
|---|
| 94 | aaa=55.5787925720215
|
|---|
| 95 | bbb=156.329864501953
|
|---|
| 96 | """
|
|---|
| 97 |
|
|---|
| 98 | R_INFO_ELEVATION_SUBSET = """rows=1350
|
|---|
| 99 | cols=1500
|
|---|
| 100 | cells=2025000
|
|---|
| 101 | datatype=FCELL
|
|---|
| 102 | """
|
|---|
| 103 |
|
|---|
| 104 | # r.info -gre map=elevation
|
|---|
| 105 | ELEVATION_MAPSET_DICT = {'mapset': 'PERMANENT'}
|
|---|
| 106 |
|
|---|
| 107 | # r.univar map=elevation
|
|---|
| 108 | ELEVATION_MINMAX = """min=55.5787925720215
|
|---|
| 109 | max=156.329864501953
|
|---|
| 110 | """
|
|---|
| 111 |
|
|---|
| 112 | # values rounded manually to maximal expected perecision
|
|---|
| 113 | ELEVATION_MINMAX_DICT = {'min': 55.58, 'max': 156.33}
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 | class TestAssertModuleKeyValue(TestCase):
|
|---|
| 117 | """Test usage of `assertModuleKeyValue` method."""
|
|---|
| 118 | # pylint: disable=R0904
|
|---|
| 119 |
|
|---|
| 120 | @classmethod
|
|---|
| 121 | def setUpClass(cls):
|
|---|
| 122 | cls.use_temp_region()
|
|---|
| 123 | cls.runModule(SimpleModule('g.region', raster='elevation'))
|
|---|
| 124 |
|
|---|
| 125 | @classmethod
|
|---|
| 126 | def tearDownClass(cls):
|
|---|
| 127 | cls.del_temp_region()
|
|---|
| 128 |
|
|---|
| 129 | def test_pygrass_module(self):
|
|---|
| 130 | """Test syntax with Module and required parameters as module"""
|
|---|
| 131 | module = Module('r.info', map='elevation', flags='gr',
|
|---|
| 132 | run_=False, finish_=True)
|
|---|
| 133 | self.assertModuleKeyValue(module,
|
|---|
| 134 | reference=dict(min=55.58, max=156.33),
|
|---|
| 135 | precision=0.01, sep='=')
|
|---|
| 136 |
|
|---|
| 137 | def test_pygrass_simple_module(self):
|
|---|
| 138 | """Test syntax with SimpleModule as module"""
|
|---|
| 139 | module = SimpleModule('r.info', map='elevation', flags='gr')
|
|---|
| 140 | self.assertModuleKeyValue(module,
|
|---|
| 141 | reference=dict(min=55.58, max=156.33),
|
|---|
| 142 | precision=0.01, sep='=')
|
|---|
| 143 |
|
|---|
| 144 | def test_direct_parameters(self):
|
|---|
| 145 | """Test syntax with module and its parameters as function parameters"""
|
|---|
| 146 | self.assertModuleKeyValue('r.info', map='elevation', flags='gr',
|
|---|
| 147 | reference=dict(min=55.58, max=156.33),
|
|---|
| 148 | precision=0.01, sep='=')
|
|---|
| 149 |
|
|---|
| 150 | def test_parameters_parameter(self):
|
|---|
| 151 | """Test syntax with module parameters in one parameters dictionary"""
|
|---|
| 152 | self.assertModuleKeyValue(module='r.info',
|
|---|
| 153 | parameters=dict(map='elevation', flags='gr'),
|
|---|
| 154 | reference=dict(min=55.58, max=156.33),
|
|---|
| 155 | precision=0.01, sep='=')
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 | class TestRasterMapAssertions(TestCase):
|
|---|
| 159 | # pylint: disable=R0904
|
|---|
| 160 |
|
|---|
| 161 | @classmethod
|
|---|
| 162 | def setUpClass(cls):
|
|---|
| 163 | cls.use_temp_region()
|
|---|
| 164 | # TODO: here we should actually not call self.runModule but call_module
|
|---|
| 165 | cls.runModule(SimpleModule('g.region', raster='elevation'))
|
|---|
| 166 |
|
|---|
| 167 | @classmethod
|
|---|
| 168 | def tearDownClass(cls):
|
|---|
| 169 | cls.del_temp_region()
|
|---|
| 170 |
|
|---|
| 171 | def test_assertRasterFitsUnivar(self):
|
|---|
| 172 | self.assertRasterFitsUnivar('elevation', R_UNIVAR_ELEVATION_SUBSET,
|
|---|
| 173 | precision=0.01)
|
|---|
| 174 | self.assertRaises(self.failureException,
|
|---|
| 175 | self.assertRasterFitsUnivar,
|
|---|
| 176 | 'geology', R_UNIVAR_ELEVATION_SUBSET, precision=0.01)
|
|---|
| 177 | self.assertRaises(ValueError,
|
|---|
| 178 | self.assertRasterFitsUnivar,
|
|---|
| 179 | 'elevation', RANDOM_KEYVALUES)
|
|---|
| 180 |
|
|---|
| 181 | def test_assertRasterFitsInfo(self):
|
|---|
| 182 | self.assertRasterFitsInfo('elevation', R_INFO_ELEVATION_SUBSET)
|
|---|
| 183 | self.assertRaises(self.failureException,
|
|---|
| 184 | self.assertRasterFitsInfo,
|
|---|
| 185 | 'geology', R_INFO_ELEVATION_SUBSET)
|
|---|
| 186 | self.assertRaises(ValueError,
|
|---|
| 187 | self.assertRasterFitsInfo,
|
|---|
| 188 | 'elevation', RANDOM_KEYVALUES)
|
|---|
| 189 |
|
|---|
| 190 | def test_common_values_info_univar(self):
|
|---|
| 191 | self.assertRasterFitsUnivar('elevation',
|
|---|
| 192 | ELEVATION_MINMAX, precision=0.01)
|
|---|
| 193 | self.assertRasterFitsInfo('elevation',
|
|---|
| 194 | ELEVATION_MINMAX, precision=0.01)
|
|---|
| 195 |
|
|---|
| 196 | def test_dict_as_parameter(self):
|
|---|
| 197 | """This also tests if we are using r.info -e flag and that precision is
|
|---|
| 198 | not required for strings.
|
|---|
| 199 | """
|
|---|
| 200 | self.assertRasterFitsInfo('elevation', ELEVATION_MAPSET_DICT)
|
|---|
| 201 |
|
|---|
| 202 | def test_assertRastersNoDifference(self):
|
|---|
| 203 | """Test basic usage of assertRastersNoDifference"""
|
|---|
| 204 | self.assertRastersNoDifference(actual='elevation',
|
|---|
| 205 | reference='elevation',
|
|---|
| 206 | precision=0, # this might need to be increased
|
|---|
| 207 | msg="The same maps should have no difference")
|
|---|
| 208 | self.assertRaises(self.failureException,
|
|---|
| 209 | self.assertRastersNoDifference,
|
|---|
| 210 | actual='elevation',
|
|---|
| 211 | reference='geology',
|
|---|
| 212 | precision=1,
|
|---|
| 213 | msg="Different maps should have difference")
|
|---|
| 214 |
|
|---|
| 215 | def test_assertRastersNoDifference_mean(self):
|
|---|
| 216 | """Test usage of assertRastersNoDifference with mean"""
|
|---|
| 217 | self.assertRastersNoDifference(actual='elevation',
|
|---|
| 218 | reference='elevation',
|
|---|
| 219 | precision=0, # this might need to be increased
|
|---|
| 220 | statistics=dict(mean=0),
|
|---|
| 221 | msg="The difference of same maps should have small mean")
|
|---|
| 222 | self.assertRaises(self.failureException,
|
|---|
| 223 | self.assertRastersNoDifference,
|
|---|
| 224 | actual='elevation',
|
|---|
| 225 | reference='geology',
|
|---|
| 226 | precision=1,
|
|---|
| 227 | statistics=dict(mean=0),
|
|---|
| 228 | msg="The difference of different maps should have huge mean")
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 | class TestMapExistsAssertions(TestCase):
|
|---|
| 232 | # pylint: disable=R0904
|
|---|
| 233 |
|
|---|
| 234 | raster_cell = 'TestMapExistsAssertions_raster_cell'
|
|---|
| 235 | raster_dcell = 'TestMapExistsAssertions_raster_dcell'
|
|---|
| 236 | raster3d = 'TestMapExistsAssertions_raster3D'
|
|---|
| 237 | vector = 'TestMapExistsAssertions_vector'
|
|---|
| 238 |
|
|---|
| 239 | @classmethod
|
|---|
| 240 | def setUpClass(cls):
|
|---|
| 241 | cls.use_temp_region()
|
|---|
| 242 | cls.runModule('g.region', n=10, e=10, s=0, w=0, t=10, b=0, res=1)
|
|---|
| 243 | cls.runModule('r.mapcalc', expression=cls.raster_cell + ' = 1')
|
|---|
| 244 | cls.runModule('r.mapcalc', expression=cls.raster_dcell + ' = 1.0')
|
|---|
| 245 | cls.runModule('r3.mapcalc', expression=cls.raster3d + ' = 1.0')
|
|---|
| 246 | cls.runModule('v.edit', map=cls.vector, tool='create')
|
|---|
| 247 |
|
|---|
| 248 | @classmethod
|
|---|
| 249 | def tearDownClass(cls):
|
|---|
| 250 | cls.runModule('g.remove', flags='f',
|
|---|
| 251 | type=['raster', 'raster3d', 'vector'],
|
|---|
| 252 | name=[cls.raster_cell, cls.raster_dcell,
|
|---|
| 253 | cls.raster3d, cls.vector])
|
|---|
| 254 | cls.del_temp_region()
|
|---|
| 255 |
|
|---|
| 256 | def test_rast_cell_exists(self):
|
|---|
| 257 | self.assertRasterExists(self.raster_cell)
|
|---|
| 258 |
|
|---|
| 259 | def test_rast_dcell_exists(self):
|
|---|
| 260 | self.assertRasterExists(self.raster_dcell)
|
|---|
| 261 |
|
|---|
| 262 | def test_rast_does_not_exist(self):
|
|---|
| 263 | self.assertRaises(self.failureException,
|
|---|
| 264 | self.assertRasterExists,
|
|---|
| 265 | 'does_not_exists')
|
|---|
| 266 |
|
|---|
| 267 | def test_rast3d_exists(self):
|
|---|
| 268 | self.assertRaster3dExists(self.raster3d)
|
|---|
| 269 |
|
|---|
| 270 | def test_rast3d_does_not_exist(self):
|
|---|
| 271 | self.assertRaises(self.failureException,
|
|---|
| 272 | self.assertRaster3dExists,
|
|---|
| 273 | 'does_not_exists')
|
|---|
| 274 |
|
|---|
| 275 | def test_vect_exists(self):
|
|---|
| 276 | self.assertVectorExists(self.vector)
|
|---|
| 277 |
|
|---|
| 278 | def test_vect_does_not_exist(self):
|
|---|
| 279 | self.assertRaises(self.failureException,
|
|---|
| 280 | self.assertVectorExists,
|
|---|
| 281 | 'does_not_exists')
|
|---|
| 282 |
|
|---|
| 283 | def test_rast_does_not_exist_in_current_mapset(self):
|
|---|
| 284 | # expecting that there is elevation in PERMANENT
|
|---|
| 285 | # TODO: use skip decorator
|
|---|
| 286 | # TODO: add the same tests but for vect and rast3d
|
|---|
| 287 | self.assertRaises(self.failureException,
|
|---|
| 288 | self.assertRasterExists,
|
|---|
| 289 | 'elevation',
|
|---|
| 290 | msg="Rasters from different mapsets should be ignored")
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 | class TestFileAssertions(TestCase):
|
|---|
| 294 | # pylint: disable=R0904
|
|---|
| 295 |
|
|---|
| 296 | @classmethod
|
|---|
| 297 | def setUpClass(cls):
|
|---|
| 298 | # we expect WIND to be always present
|
|---|
| 299 | gisenv = gcore.gisenv()
|
|---|
| 300 | cls.existing_file = os.path.join(gisenv['GISDBASE'],
|
|---|
| 301 | gisenv['LOCATION_NAME'],
|
|---|
| 302 | 'PERMANENT', 'WIND')
|
|---|
| 303 | cls.emtpy_file = cls.__name__ + '_this_is_an_empty_file'
|
|---|
| 304 | open(cls.emtpy_file, 'w').close()
|
|---|
| 305 | cls.file_with_md5 = cls.__name__ + '_this_is_a_file_with_known_md5'
|
|---|
| 306 | file_content = 'Content of the file with known MD5.\n'
|
|---|
| 307 | with open(cls.file_with_md5, 'w') as f:
|
|---|
| 308 | f.write(file_content)
|
|---|
| 309 | # MD5 sum created using:
|
|---|
| 310 | # echo 'Content of the file with known MD5.' > some_file.txt
|
|---|
| 311 | # md5sum some_file.txt
|
|---|
| 312 | cls.file_md5 = '807bba4ffac4bb351bc3f27853009949'
|
|---|
| 313 |
|
|---|
| 314 | cls.file_with_same_content = cls.__name__ + '_file_with_same_content'
|
|---|
| 315 | with open(cls.file_with_same_content, 'w') as f:
|
|---|
| 316 | f.write(file_content)
|
|---|
| 317 |
|
|---|
| 318 | cls.file_with_different_content = cls.__name__ + '_file_with_different_content'
|
|---|
| 319 | with open(cls.file_with_different_content, 'w') as f:
|
|---|
| 320 | f.write(file_content + ' Something else here.')
|
|---|
| 321 |
|
|---|
| 322 | @classmethod
|
|---|
| 323 | def tearDownClass(cls):
|
|---|
| 324 | os.remove(cls.emtpy_file)
|
|---|
| 325 | os.remove(cls.file_with_md5)
|
|---|
| 326 | os.remove(cls.file_with_same_content)
|
|---|
| 327 | os.remove(cls.file_with_different_content)
|
|---|
| 328 |
|
|---|
| 329 | def test_assertFileExists(self):
|
|---|
| 330 | self.assertFileExists(filename=self.existing_file)
|
|---|
| 331 | self.assertRaises(self.failureException,
|
|---|
| 332 | self.assertFileExists,
|
|---|
| 333 | filename='this_one_does_not_exists')
|
|---|
| 334 |
|
|---|
| 335 | def test_assertFileExists_empty_file(self):
|
|---|
| 336 | self.assertFileExists(filename=self.emtpy_file, skip_size_check=True)
|
|---|
| 337 | self.assertRaises(self.failureException,
|
|---|
| 338 | self.assertFileExists,
|
|---|
| 339 | filename=self.emtpy_file)
|
|---|
| 340 |
|
|---|
| 341 | def test_assertFileMd5(self):
|
|---|
| 342 | self.assertFileMd5(filename=self.file_with_md5, md5=self.file_md5)
|
|---|
| 343 | self.assertRaises(self.failureException,
|
|---|
| 344 | self.assertFileMd5,
|
|---|
| 345 | filename=self.file_with_md5, md5='wrongmd5')
|
|---|
| 346 |
|
|---|
| 347 | def test_assertFilesEqualMd5(self):
|
|---|
| 348 | self.assertFilesEqualMd5(filename=self.file_with_md5,
|
|---|
| 349 | reference=self.file_with_same_content)
|
|---|
| 350 | self.assertRaises(self.failureException,
|
|---|
| 351 | self.assertFilesEqualMd5,
|
|---|
| 352 | filename=self.file_with_md5,
|
|---|
| 353 | reference=self.file_with_different_content)
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 | if __name__ == '__main__':
|
|---|
| 357 | test()
|
|---|