test_checkers.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests checkers functions
  4. @brief Test of GRASS Python testing framework checkers
  5. (C) 2014 by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. @author Vaclav Petras
  10. """
  11. from grass.script.utils import parse_key_val
  12. import grass.gunittest
  13. from grass.gunittest.checkers import (values_equal, text_to_keyvalue,
  14. keyvalue_equals, proj_info_equals, proj_units_equals)
  15. class TestValuesEqual(grass.gunittest.TestCase):
  16. def test_floats(self):
  17. self.assertTrue(values_equal(5.0, 5.0))
  18. self.assertTrue(values_equal(5.1, 5.19, precision=0.1))
  19. self.assertTrue(values_equal(5.00005, 5.000059, precision=0.00001))
  20. self.assertFalse(values_equal(5.125, 5.280))
  21. self.assertFalse(values_equal(5.00005, 5.00006, precision=0.00001))
  22. self.assertFalse(values_equal(2.5, 15.5, precision=5))
  23. def test_ints(self):
  24. self.assertTrue(values_equal(5, 5, precision=0.01))
  25. self.assertFalse(values_equal(5, 6, precision=0.01))
  26. self.assertTrue(values_equal(5, 8, precision=3))
  27. self.assertFalse(values_equal(3600, 3623, precision=20))
  28. self.assertTrue(values_equal(5, 5))
  29. self.assertFalse(values_equal(5, 6))
  30. def test_floats_and_ints(self):
  31. self.assertTrue(values_equal(5.1, 5, precision=0.2))
  32. self.assertFalse(values_equal(5.1, 5, precision=0.01))
  33. def test_strings(self):
  34. self.assertTrue(values_equal('hello', 'hello'))
  35. self.assertFalse(values_equal('Hello', 'hello'))
  36. def test_lists(self):
  37. self.assertTrue(values_equal([1, 2, 3], [1, 2, 3]))
  38. self.assertTrue(values_equal([1.1, 2.0, 3.9],
  39. [1.1, 1.95, 4.0],
  40. precision=0.2))
  41. self.assertFalse(values_equal([1, 2, 3, 4, 5],
  42. [1, 22, 3, 4, 5],
  43. precision=1))
  44. def test_mixed_lists(self):
  45. self.assertTrue(values_equal([1, 'abc', 8], [1, 'abc', 8.2],
  46. precision=0.5))
  47. def test_recursive_lists(self):
  48. self.assertTrue(values_equal([1, 'abc', [5, 9.6, 9.0]],
  49. [1, 'abc', [4.9, 9.2, 9.3]],
  50. precision=0.5))
  51. KEYVAL_TEXT = '''s: Hello
  52. str: Hello world!
  53. f: 1.0
  54. l: 1,2,3,4,5
  55. mixed: hello,8,-25,world!,4-1,5:2,0.1,-9.6
  56. '''
  57. # file location/PERMANENT/PROJ_INFO
  58. PROJ_INFO_TEXT_1 = """name: Lambert Conformal Conic
  59. proj: lcc
  60. datum: nad83
  61. a: 6378137.0
  62. es: 0.006694380022900787
  63. lat_1: 36.16666666666666
  64. lat_2: 34.33333333333334
  65. lat_0: 33.75
  66. lon_0: -79
  67. x_0: 609601.22
  68. y_0: 0
  69. no_defs: defined
  70. """
  71. # file location/PERMANENT/PROJ_UNITS
  72. PROJ_UNITS_TEXT_1 = """unit: Meter
  73. units: Meters
  74. meters: 1
  75. """
  76. PROJ_INFO_TEXT_2 = """name: Lambert Conformal Conic
  77. proj: lcc
  78. datum: nad83
  79. a: 6378137.0000000002
  80. es: 0.006694380022900787
  81. lat_1: 36.166666667
  82. lat_2: 34.333333333
  83. lat_0: 33.75
  84. lon_0: -79
  85. x_0: 609601.22
  86. y_0: 0
  87. no_defs: defined
  88. """
  89. PROJ_UNITS_TEXT_2 = """unit: Metre
  90. units: Metres
  91. meters: 1
  92. """
  93. # what about keys and lower/upper case letters
  94. class TestTextToKeyValue(grass.gunittest.TestCase):
  95. def test_conversion(self):
  96. keyvals = text_to_keyvalue(KEYVAL_TEXT, sep=':', val_sep=',')
  97. expected = {'s': 'Hello',
  98. 'str': 'Hello world!',
  99. 'f': 1.0,
  100. 'l': [1, 2, 3, 4, 5],
  101. 'mixed': ['hello', 8, -25, 'world!',
  102. '4-1', '5:2', 0.1, -9.6]}
  103. self.assertDictEqual(expected, keyvals)
  104. def test_single_values(self):
  105. keyvals = text_to_keyvalue("a: 1.5", sep=':')
  106. self.assertDictEqual({'a': 1.5}, keyvals)
  107. keyvals = text_to_keyvalue("abc=1", sep='=')
  108. self.assertDictEqual({'abc': 1}, keyvals)
  109. keyvals = text_to_keyvalue("abc=hello", sep='=')
  110. self.assertDictEqual({'abc': 'hello'}, keyvals)
  111. def test_strip(self):
  112. keyvals = text_to_keyvalue("a: 2.8 ", sep=':')
  113. self.assertDictEqual({'a': 2.8}, keyvals)
  114. keyvals = text_to_keyvalue("a: 2 ; 2.8 ; ab cd ",
  115. sep=':', val_sep=';')
  116. self.assertDictEqual({'a': [2, 2.8, 'ab cd']}, keyvals)
  117. keyvals = text_to_keyvalue("a : 2 ; 2.8", sep=':', val_sep=';')
  118. self.assertDictEqual({'a': [2, 2.8]}, keyvals)
  119. keyvals = text_to_keyvalue("a : \t 2 ;\t2.8", sep=':', val_sep=';')
  120. self.assertDictEqual({'a': [2, 2.8]}, keyvals)
  121. def test_empty_list_item(self):
  122. keyvals = text_to_keyvalue("a: 1, ,5,,", sep=':', val_sep=',')
  123. self.assertDictEqual({'a': [1, '', 5, '', '']}, keyvals)
  124. def test_empty_value(self):
  125. keyvals = text_to_keyvalue("a: ", sep=':')
  126. self.assertDictEqual({'a': ''}, keyvals)
  127. keyvals = text_to_keyvalue("a:", sep=':')
  128. self.assertDictEqual({'a': ''}, keyvals)
  129. def test_wrong_lines(self):
  130. # we consider no key-value separator as invalid line
  131. # and we silently ignore these
  132. keyvals = text_to_keyvalue("a", sep=':',
  133. skip_invalid=True, skip_empty=False)
  134. self.assertDictEqual({}, keyvals)
  135. self.assertRaises(ValueError, text_to_keyvalue, "a", sep=':',
  136. skip_invalid=False, skip_empty=False)
  137. # text_to_keyvalue considers the empty string as valid input
  138. keyvals = text_to_keyvalue("", sep=':',
  139. skip_invalid=False, skip_empty=False)
  140. self.assertDictEqual({}, keyvals)
  141. self.assertRaises(ValueError, text_to_keyvalue, "\n", sep=':',
  142. skip_invalid=True, skip_empty=False)
  143. keyvals = text_to_keyvalue("a\n\n", sep=':',
  144. skip_invalid=True, skip_empty=True)
  145. self.assertDictEqual({}, keyvals)
  146. def test_separators(self):
  147. keyvals = text_to_keyvalue("a=a;b;c", sep='=', val_sep=';')
  148. self.assertDictEqual({'a': ['a', 'b', 'c']}, keyvals)
  149. keyvals = text_to_keyvalue("a 1;2;3", sep=' ', val_sep=';')
  150. self.assertDictEqual({'a': [1, 2, 3]}, keyvals)
  151. # spaces as key-value separator and values separators
  152. # this should work (e.g. because of : in DMS),
  153. # although it does not support stripping (we don't merge separators)
  154. keyvals = text_to_keyvalue("a 1 2 3", sep=' ', val_sep=' ')
  155. self.assertDictEqual({'a': [1, 2, 3]}, keyvals)
  156. #def test_projection_files(self):
  157. # obtained by r.univar elevation -g
  158. # floats removed
  159. R_UNIVAR_KEYVAL = """n=2025000
  160. null_cells=57995100
  161. cells=60020100
  162. min=55.5787925720215
  163. max=156.329864501953
  164. range=100.751071929932
  165. mean=110.375440275606
  166. mean_of_abs=110.375440275606
  167. stddev=20.3153233205981
  168. variance=412.712361620436
  169. coeff_var=18.4056555243368
  170. sum=223510266.558102
  171. """
  172. # obtained by r.univar elevation -g
  173. # floats removed
  174. R_UNIVAR_KEYVAL_INT = """n=2025000
  175. null_cells=57995100
  176. cells=60020100
  177. """
  178. R_UNIVAR_KEYVAL_INT_DICT = {'n': 2025000,
  179. 'null_cells': 57995100, 'cells': 60020100}
  180. class TestComapreProjections(grass.gunittest.TestCase):
  181. def test_compare_proj_info(self):
  182. self.assertTrue(proj_info_equals(PROJ_INFO_TEXT_1, PROJ_INFO_TEXT_2))
  183. self.assertTrue(proj_units_equals(PROJ_UNITS_TEXT_1, PROJ_UNITS_TEXT_2))
  184. class TestParseKeyvalue(grass.gunittest.TestCase):
  185. def test_shell_script_style(self):
  186. self.assertDictEqual(parse_key_val(R_UNIVAR_KEYVAL_INT, val_type=int),
  187. R_UNIVAR_KEYVAL_INT_DICT)
  188. R_UNIVAR_ELEVATION = """n=2025000
  189. null_cells=57995100
  190. cells=60020100
  191. min=55.5787925720215
  192. max=156.329864501953
  193. range=100.751071929932
  194. mean=110.375440275606
  195. mean_of_abs=110.375440275606
  196. stddev=20.3153233205981
  197. variance=412.712361620436
  198. coeff_var=18.4056555243368
  199. sum=223510266.558102
  200. first_quartile=94.79
  201. median=108.88
  202. third_quartile=126.792
  203. percentile_90=138.66
  204. """
  205. R_UNIVAR_ELEVATION_ROUNDED = """n=2025000
  206. null_cells=57995100
  207. cells=60020100
  208. min=55.5788
  209. max=156.33
  210. range=100.751
  211. mean=110.375
  212. mean_of_abs=110.375
  213. stddev=20.3153
  214. variance=412.712
  215. coeff_var=18.4057
  216. sum=223510266.558
  217. first_quartile=94.79
  218. median=108.88
  219. third_quartile=126.792
  220. percentile_90=138.66
  221. """
  222. R_UNIVAR_ELEVATION_SUBSET = """n=2025000
  223. null_cells=57995100
  224. cells=60020100
  225. min=55.5787925720215
  226. max=156.329864501953
  227. """
  228. class TestRasterMapComparisons(grass.gunittest.TestCase):
  229. def test_compare_univars(self):
  230. self.assertTrue(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION,
  231. sep='='),
  232. text_to_keyvalue(R_UNIVAR_ELEVATION,
  233. sep='='),
  234. precision=0))
  235. self.assertFalse(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION,
  236. sep='='),
  237. text_to_keyvalue(R_UNIVAR_ELEVATION_SUBSET,
  238. sep='='),
  239. precision=0))
  240. def test_compare_univars_subset(self):
  241. self.assertTrue(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION_SUBSET,
  242. sep='='),
  243. text_to_keyvalue(R_UNIVAR_ELEVATION,
  244. sep='='),
  245. a_is_subset=True, precision=0))
  246. self.assertFalse(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION,
  247. sep='='),
  248. text_to_keyvalue(R_UNIVAR_ELEVATION_SUBSET,
  249. sep='='),
  250. a_is_subset=True, precision=0))
  251. def test_compare_univars_rounded(self):
  252. self.assertTrue(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION,
  253. sep='='),
  254. text_to_keyvalue(R_UNIVAR_ELEVATION_ROUNDED,
  255. sep='='),
  256. precision=0.001))
  257. if __name__ == '__main__':
  258. grass.gunittest.test()