1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
# -*- coding: utf-8 -*-
"""
Tests checkers functions
@brief Test of GRASS Python testing framework checkers
(C) 2014 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
@author Vaclav Petras
"""
from grass.script.utils import parse_key_val, try_remove
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
from grass.gunittest.checkers import (
values_equal, text_to_keyvalue,
keyvalue_equals, proj_info_equals, proj_units_equals,
file_md5, text_file_md5)
class TestValuesEqual(TestCase):
def test_floats(self):
self.assertTrue(values_equal(5.0, 5.0))
self.assertTrue(values_equal(5.1, 5.19, precision=0.1))
self.assertTrue(values_equal(5.00005, 5.000059, precision=0.00001))
self.assertFalse(values_equal(5.125, 5.280))
self.assertFalse(values_equal(5.00005, 5.00006, precision=0.00001))
self.assertFalse(values_equal(2.5, 15.5, precision=5))
def test_ints(self):
self.assertTrue(values_equal(5, 5, precision=0.01))
self.assertFalse(values_equal(5, 6, precision=0.01))
self.assertTrue(values_equal(5, 8, precision=3))
self.assertFalse(values_equal(3600, 3623, precision=20))
self.assertTrue(values_equal(5, 5))
self.assertFalse(values_equal(5, 6))
def test_floats_and_ints(self):
self.assertTrue(values_equal(5.1, 5, precision=0.2))
self.assertFalse(values_equal(5.1, 5, precision=0.01))
def test_strings(self):
self.assertTrue(values_equal('hello', 'hello'))
self.assertFalse(values_equal('Hello', 'hello'))
def test_lists(self):
self.assertTrue(values_equal([1, 2, 3], [1, 2, 3]))
self.assertTrue(values_equal([1.1, 2.0, 3.9],
[1.1, 1.95, 4.0],
precision=0.2))
self.assertFalse(values_equal([1, 2, 3, 4, 5],
[1, 22, 3, 4, 5],
precision=1))
def test_mixed_lists(self):
self.assertTrue(values_equal([1, 'abc', 8], [1, 'abc', 8.2],
precision=0.5))
def test_recursive_lists(self):
self.assertTrue(values_equal([1, 'abc', [5, 9.6, 9.0]],
[1, 'abc', [4.9, 9.2, 9.3]],
precision=0.5))
KEYVAL_TEXT = '''s: Hello
str: Hello world!
f: 1.0
l: 1,2,3,4,5
mixed: hello,8,-25,world!,4-1,5:2,0.1,-9.6
'''
# file location/PERMANENT/PROJ_INFO
PROJ_INFO_TEXT_1 = """name: Lambert Conformal Conic
proj: lcc
datum: nad83
a: 6378137.0
es: 0.006694380022900787
lat_1: 36.16666666666666
lat_2: 34.33333333333334
lat_0: 33.75
lon_0: -79
x_0: 609601.22
y_0: 0
no_defs: defined
"""
# file location/PERMANENT/PROJ_UNITS
PROJ_UNITS_TEXT_1 = """unit: Meter
units: Meters
meters: 1
"""
PROJ_INFO_TEXT_2 = """name: Lambert Conformal Conic
proj: lcc
datum: nad83
a: 6378137.0000000002
es: 0.006694380022900787
lat_1: 36.166666667
lat_2: 34.333333333
lat_0: 33.75
lon_0: -79
x_0: 609601.22
y_0: 0
no_defs: defined
"""
PROJ_UNITS_TEXT_2 = """unit: Metre
units: Meters
meters: 1
"""
# what about keys and lower/upper case letters
class TestTextToKeyValue(TestCase):
def test_conversion(self):
keyvals = text_to_keyvalue(KEYVAL_TEXT, sep=':', val_sep=',')
expected = {'s': 'Hello',
'str': 'Hello world!',
'f': 1.0,
'l': [1, 2, 3, 4, 5],
'mixed': ['hello', 8, -25, 'world!',
'4-1', '5:2', 0.1, -9.6]}
self.assertDictEqual(expected, keyvals)
def test_single_values(self):
keyvals = text_to_keyvalue("a: 1.5", sep=':')
self.assertDictEqual({'a': 1.5}, keyvals)
keyvals = text_to_keyvalue("abc=1", sep='=')
self.assertDictEqual({'abc': 1}, keyvals)
keyvals = text_to_keyvalue("abc=hello", sep='=')
self.assertDictEqual({'abc': 'hello'}, keyvals)
def test_strip(self):
keyvals = text_to_keyvalue("a: 2.8 ", sep=':')
self.assertDictEqual({'a': 2.8}, keyvals)
keyvals = text_to_keyvalue("a: 2 ; 2.8 ; ab cd ",
sep=':', val_sep=';')
self.assertDictEqual({'a': [2, 2.8, 'ab cd']}, keyvals)
keyvals = text_to_keyvalue("a : 2 ; 2.8", sep=':', val_sep=';')
self.assertDictEqual({'a': [2, 2.8]}, keyvals)
keyvals = text_to_keyvalue("a : \t 2 ;\t2.8", sep=':', val_sep=';')
self.assertDictEqual({'a': [2, 2.8]}, keyvals)
def test_empty_list_item(self):
keyvals = text_to_keyvalue("a: 1, ,5,,", sep=':', val_sep=',')
self.assertDictEqual({'a': [1, '', 5, '', '']}, keyvals)
def test_empty_value(self):
keyvals = text_to_keyvalue("a: ", sep=':')
self.assertDictEqual({'a': ''}, keyvals)
keyvals = text_to_keyvalue("a:", sep=':')
self.assertDictEqual({'a': ''}, keyvals)
def test_wrong_lines(self):
# we consider no key-value separator as invalid line
# and we silently ignore these
keyvals = text_to_keyvalue("a", sep=':',
skip_invalid=True, skip_empty=False)
self.assertDictEqual({}, keyvals)
self.assertRaises(ValueError, text_to_keyvalue, "a", sep=':',
skip_invalid=False, skip_empty=False)
# text_to_keyvalue considers the empty string as valid input
keyvals = text_to_keyvalue("", sep=':',
skip_invalid=False, skip_empty=False)
self.assertDictEqual({}, keyvals)
self.assertRaises(ValueError, text_to_keyvalue, "\n", sep=':',
skip_invalid=True, skip_empty=False)
keyvals = text_to_keyvalue("a\n\n", sep=':',
skip_invalid=True, skip_empty=True)
self.assertDictEqual({}, keyvals)
def test_separators(self):
keyvals = text_to_keyvalue("a=a;b;c", sep='=', val_sep=';')
self.assertDictEqual({'a': ['a', 'b', 'c']}, keyvals)
keyvals = text_to_keyvalue("a 1;2;3", sep=' ', val_sep=';')
self.assertDictEqual({'a': [1, 2, 3]}, keyvals)
# spaces as key-value separator and values separators
# this should work (e.g. because of : in DMS),
# although it does not support stripping (we don't merge separators)
keyvals = text_to_keyvalue("a 1 2 3", sep=' ', val_sep=' ')
self.assertDictEqual({'a': [1, 2, 3]}, keyvals)
#def test_projection_files(self):
# obtained by r.univar elevation -g
# floats removed
R_UNIVAR_KEYVAL = """n=2025000
null_cells=57995100
cells=60020100
min=55.5787925720215
max=156.329864501953
range=100.751071929932
mean=110.375440275606
mean_of_abs=110.375440275606
stddev=20.3153233205981
variance=412.712361620436
coeff_var=18.4056555243368
sum=223510266.558102
"""
# obtained by r.univar elevation -g
# floats removed
R_UNIVAR_KEYVAL_INT = """n=2025000
null_cells=57995100
cells=60020100
"""
R_UNIVAR_KEYVAL_INT_DICT = {'n': 2025000,
'null_cells': 57995100, 'cells': 60020100}
class TestComapreProjections(TestCase):
def test_compare_proj_info(self):
self.assertTrue(proj_info_equals(PROJ_INFO_TEXT_1, PROJ_INFO_TEXT_2))
self.assertTrue(proj_units_equals(PROJ_UNITS_TEXT_1, PROJ_UNITS_TEXT_2))
class TestParseKeyvalue(TestCase):
def test_shell_script_style(self):
self.assertDictEqual(parse_key_val(R_UNIVAR_KEYVAL_INT, val_type=int),
R_UNIVAR_KEYVAL_INT_DICT)
R_UNIVAR_ELEVATION = """n=2025000
null_cells=57995100
cells=60020100
min=55.5787925720215
max=156.329864501953
range=100.751071929932
mean=110.375440275606
mean_of_abs=110.375440275606
stddev=20.3153233205981
variance=412.712361620436
coeff_var=18.4056555243368
sum=223510266.558102
first_quartile=94.79
median=108.88
third_quartile=126.792
percentile_90=138.66
"""
R_UNIVAR_ELEVATION_ROUNDED = """n=2025000
null_cells=57995100
cells=60020100
min=55.5788
max=156.33
range=100.751
mean=110.375
mean_of_abs=110.375
stddev=20.3153
variance=412.712
coeff_var=18.4057
sum=223510266.558
first_quartile=94.79
median=108.88
third_quartile=126.792
percentile_90=138.66
"""
R_UNIVAR_ELEVATION_SUBSET = """n=2025000
null_cells=57995100
cells=60020100
min=55.5787925720215
max=156.329864501953
"""
class TestRasterMapComparisons(TestCase):
def test_compare_univars(self):
self.assertTrue(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION,
sep='='),
text_to_keyvalue(R_UNIVAR_ELEVATION,
sep='='),
precision=0))
self.assertFalse(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION,
sep='='),
text_to_keyvalue(R_UNIVAR_ELEVATION_SUBSET,
sep='='),
precision=0))
def test_compare_univars_subset(self):
self.assertTrue(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION_SUBSET,
sep='='),
text_to_keyvalue(R_UNIVAR_ELEVATION,
sep='='),
a_is_subset=True, precision=0))
self.assertFalse(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION,
sep='='),
text_to_keyvalue(R_UNIVAR_ELEVATION_SUBSET,
sep='='),
a_is_subset=True, precision=0))
def test_compare_univars_rounded(self):
self.assertTrue(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION,
sep='='),
text_to_keyvalue(R_UNIVAR_ELEVATION_ROUNDED,
sep='='),
precision=0.001))
CORRECT_LINES = [
"null_cells=57995100",
"cells=60020100",
"min=55.5787925720215",
"max=156.329864501953"
]
INCORRECT_LINES = [
"null_cells=579951",
"cells=60020100",
"min=5.5787925720215",
"max=156.329864501953"
]
class TestMd5Sums(TestCase):
r"""
To create MD5 which is used for testing use:
.. code: sh
$ cat > test.txt << EOF
null_cells=57995100
cells=60020100
min=55.5787925720215
max=156.329864501953
EOF
$ md5sum test.txt
9dd6c4bb9d2cf6051b12f4b5f9d70523 test.txt
"""
correct_md5sum = '9dd6c4bb9d2cf6051b12f4b5f9d70523'
correct_file_name_platform_nl = 'md5_sum_correct_file_platform_nl'
correct_file_name_unix_nl = 'md5_sum_correct_file_unix_nl'
wrong_file_name = 'md5_sum_wrong_file'
@classmethod
def setUpClass(cls):
with open(cls.correct_file_name_platform_nl, 'w') as f:
for line in CORRECT_LINES:
# \n should be converted to platform newline
f.write(line + '\n')
with open(cls.correct_file_name_unix_nl, 'wb') as f:
for line in CORRECT_LINES:
# binary mode will write pure \n
f.write(line + '\n')
with open(cls.wrong_file_name, 'w') as f:
for line in INCORRECT_LINES:
# \n should be converted to platform newline
f.write(line + '\n')
@classmethod
def tearDownClass(cls):
try_remove(cls.correct_file_name_platform_nl)
try_remove(cls.correct_file_name_unix_nl)
try_remove(cls.wrong_file_name)
def test_text_file_binary(self):
r"""File with ``\n`` (LF) newlines as binary (MD5 has ``\n``)."""
self.assertEqual(file_md5(self.correct_file_name_unix_nl),
self.correct_md5sum,
msg="MD5 sums different")
def test_text_file_platfrom(self):
r"""Text file with platform dependent newlines"""
self.assertEqual(text_file_md5(self.correct_file_name_platform_nl),
self.correct_md5sum,
msg="MD5 sums different")
def test_text_file_unix(self):
r"""Text file with ``\n`` (LF) newlines"""
self.assertEqual(text_file_md5(self.correct_file_name_unix_nl),
self.correct_md5sum,
msg="MD5 sums different")
def test_text_file_different(self):
r"""Text file with ``\n`` (LF) newlines"""
self.assertNotEqual(text_file_md5(self.wrong_file_name),
self.correct_md5sum,
msg="MD5 sums must be different")
if __name__ == '__main__':
test()
|