test_checkers.py 12 KB

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