[GRASS-SVN] r60731 - in sandbox/wenzeslaus/gunittest: . testsuite
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Jun 6 14:22:16 PDT 2014
Author: wenzeslaus
Date: 2014-06-06 14:22:15 -0700 (Fri, 06 Jun 2014)
New Revision: 60731
Modified:
sandbox/wenzeslaus/gunittest/checkers.py
sandbox/wenzeslaus/gunittest/testsuite/test_checkers.py
Log:
gunittest: improve the dictionary (keyvalue) functions, more general, specialized functions for projection (tests run, code coverage 86%)
Modified: sandbox/wenzeslaus/gunittest/checkers.py
===================================================================
--- sandbox/wenzeslaus/gunittest/checkers.py 2014-06-06 21:13:15 UTC (rev 60730)
+++ sandbox/wenzeslaus/gunittest/checkers.py 2014-06-06 21:22:15 UTC (rev 60731)
@@ -1,5 +1,18 @@
# -*- coding: utf-8 -*-
+"""!@package grass.gunittest.checkers
+
+ at brief 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.
+
+ at author Vaclav Petras
+ at author Soeren Gebbert
+"""
+
import sys
import re
import doctest
@@ -49,16 +62,28 @@
"""
# the lookup variable is a list of list, each list contains all the
# possible name for a units
- lookup = [['meter', 'metre'], ['meters', 'metres'], ['kilometer',
- 'kilometre'], ['kilometers', 'kilometres']]
+ lookup = [['meter', 'metre'], ['meters', 'metres'],
+ ['Meter', 'Metre'], ['Meters', 'Metres'],
+ ['kilometer', 'kilometre'], ['kilometers', 'kilometres'],
+ ['Kilometer', 'Kilometre'], ['Kilometers', 'Kilometres'],
+ ]
dic = dict(dic)
for l in lookup:
- for n in range(len(dic['unit'])):
- if dic['unit'][n] in l:
- dic['unit'][n] = l[0]
- for n in range(len(dic['units'])):
- if dic['units'][n] in l:
- dic['units'][n] = l[0]
+ import types
+ if not isinstance(dic['unit'], types.StringTypes):
+ for n in range(len(dic['unit'])):
+ if dic['unit'][n] in l:
+ dic['unit'][n] = l[0]
+ else:
+ if dic['unit'] in l:
+ dic['unit'] = l[0]
+ if not isinstance(dic['units'], types.StringTypes):
+ for n in range(len(dic['units'])):
+ if dic['units'][n] in l:
+ dic['units'][n] = l[0]
+ else:
+ if dic['units'] in l:
+ dic['units'] = l[0]
return dic
@@ -73,11 +98,12 @@
And empty string is a valid input because empty dictionary is a valid
dictionary.
- @param filename The name or name and path of the text file to convert
- @param sep The character that separates the keys and values, default is ":"
- @param val_sep The character that separates the values of a single key, default is ","
+ @param text string to convert
+ @param sep character that separates the keys and values, default is ":"
+ @param val_sep character that separates the values of a single key, default is ","
@param functions list of functions to apply on the resulting dictionary
- @param skip_invalid skips all lines which does not contain separator (including empty lines)
+ @param skip_invalid skip all lines which does not contain separator
+ @param skip_empty skip empty lines
@return The dictionary
@@ -217,13 +243,9 @@
return True
-def compare_keyvalue_texts(text_a, text_b, sep=":",
- val_sep=",", precision=0.000001,
- def_equal=values_equal,
- key_equal=None,
- polish_functions=None):
- """
- !Compare two key-value text files
+def compare_keyvalue(dict_a, dict_b, precision=0.000001,
+ def_equal=values_equal, key_equal=None):
+ """Compare two dictionaries
This method will print a warning in case keys that are present in the first
file are not present in the second one.
@@ -232,15 +254,14 @@
An example key-value text file may have this content:
-
- >>> compare_keyvalue_texts('''a: Hello
+ >>> compare_keyvalue(text_to_keyvalue('''a: Hello
... b: 1.0
... c: 1,2,3,4,5
- ... d: hello,8,0.1''',
- ... '''a: Hello
+ ... d: hello,8,0.1'''),
+ ... text_to_keyvalue('''a: Hello
... b: 1.1
... c: 1,22,3,4,5
- ... d: hello,8,0.1''', precision=0.1)
+ ... d: hello,8,0.1'''), precision=0.1)
False
@param filename_a name of the first key-value text file
@@ -253,8 +274,6 @@
@return True if full or almost identical, False if different
"""
- dict_a = text_to_keyvalue(text_a, sep, val_sep, functions=polish_functions)
- dict_b = text_to_keyvalue(text_b, sep, val_sep, functions=polish_functions)
key_equal = {} if key_equal is None else key_equal
if sorted(dict_a.keys()) != sorted(dict_b.keys()):
@@ -277,6 +296,42 @@
return True
+def proj_info_equals(text_a, text_b):
+ def compare_sums(list_a, list_b, precision):
+ # We compare the sum of the entries
+ if abs(sum(list_a) - sum(list_b)) > precision:
+ return False
+
+ sep = ':'
+ val_sep = ','
+ key_equal = {'+towgs84': compare_sums}
+ dict_a = text_to_keyvalue(text_a, sep=sep, val_sep=val_sep,
+ functions=[unify_projection])
+ dict_b = text_to_keyvalue(text_b, sep=sep, val_sep=val_sep,
+ functions=[unify_projection])
+ return compare_keyvalue(dict_a, dict_b,
+ precision=0.000001,
+ def_equal=values_equal,
+ key_equal=key_equal)
+
+
+def proj_units_equals(text_a, text_b):
+ def lowercase_equals(string_a, string_b, precision=None):
+ return string_a.lower() == string_b.lower()
+
+ sep = ':'
+ val_sep = ','
+ key_equal = {'unit': lowercase_equals, 'units': lowercase_equals}
+ dict_a = text_to_keyvalue(text_a, sep=sep, val_sep=val_sep,
+ functions=[unify_units])
+ dict_b = text_to_keyvalue(text_b, sep, val_sep,
+ functions=[unify_units])
+ return compare_keyvalue(dict_a, dict_b,
+ precision=0.000001,
+ def_equal=values_equal,
+ key_equal=key_equal)
+
+
# alternative names: looks like, correspond with/to
def check_text_ellipsis(reference, actual):
"""
@@ -371,10 +426,10 @@
optionflags=doctest.ELLIPSIS)
-def main():
+def main(): # pragma: no cover
ret = doctest.testmod()
return ret.failed
-if __name__ == '__main__':
+if __name__ == '__main__': # pragma: no cover
sys.exit(main())
Modified: sandbox/wenzeslaus/gunittest/testsuite/test_checkers.py
===================================================================
--- sandbox/wenzeslaus/gunittest/testsuite/test_checkers.py 2014-06-06 21:13:15 UTC (rev 60730)
+++ sandbox/wenzeslaus/gunittest/testsuite/test_checkers.py 2014-06-06 21:22:15 UTC (rev 60731)
@@ -1,62 +1,74 @@
# -*- coding: utf-8 -*-
"""
-Tests assertion methods.
+Tests checkers functions
+
+ at 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.
+
+ at author Vaclav Petras
"""
import sys
import os
+from grass.script.core import parse_key_val
+
# import gunittest as a package so that relative imports there works
sys.path.insert(0, os.path.split(os.path.split((os.path.dirname(__file__)))[0])[0])
from gunittest.case import GrassTestCase
-import gunittest.checkers as checkers
+from gunittest.checkers import (values_equal, text_to_keyvalue,
+ proj_info_equals, proj_units_equals)
class TestValuesEqual(GrassTestCase):
def test_floats(self):
- self.assertTrue(checkers.values_equal(5.0, 5.0))
- self.assertTrue(checkers.values_equal(5.1, 5.19, precision=0.1))
- self.assertTrue(checkers.values_equal(5.00005, 5.000059, precision=0.00001))
- self.assertFalse(checkers.values_equal(5.125, 5.280))
- self.assertFalse(checkers.values_equal(5.00005, 5.00006, precision=0.00001))
- self.assertFalse(checkers.values_equal(2.5, 15.5, precision=5))
+ 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(checkers.values_equal(5, 5, precision=0.01))
- self.assertFalse(checkers.values_equal(5, 6, precision=0.01))
- self.assertTrue(checkers.values_equal(5, 8, precision=3))
- self.assertFalse(checkers.values_equal(3600, 3623, precision=20))
- self.assertTrue(checkers.values_equal(5, 5))
- self.assertFalse(checkers.values_equal(5, 6))
+ 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(checkers.values_equal(5.1, 5, precision=0.2))
- self.assertFalse(checkers.values_equal(5.1, 5, precision=0.01))
+ 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(checkers.values_equal('hello', 'hello'))
- self.assertFalse(checkers.values_equal('Hello', 'hello'))
+ self.assertTrue(values_equal('hello', 'hello'))
+ self.assertFalse(values_equal('Hello', 'hello'))
def test_lists(self):
- self.assertTrue(checkers.values_equal([1, 2, 3], [1, 2, 3]))
- self.assertTrue(checkers.values_equal([1.1, 2.0, 3.9],
- [1.1, 1.95, 4.0],
- precision=0.2))
- self.assertFalse(checkers.values_equal([1, 2, 3, 4, 5],
- [1, 22, 3, 4, 5],
- precision=1))
+ 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(checkers.values_equal([1, 'abc', 8], [1, 'abc', 8.2],
- precision=0.5))
+ self.assertTrue(values_equal([1, 'abc', 8], [1, 'abc', 8.2],
+ precision=0.5))
def test_recursive_lists(self):
- self.assertTrue(checkers.values_equal([1, 'abc', [5, 9.6, 9.0]],
- [1, 'abc', [4.9, 9.2, 9.3]],
- precision=0.5))
+ 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!
@@ -65,10 +77,51 @@
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: Metres
+meters: 1
+"""
+# what about keys and lower/upper case letters
+
+
class TestTextToKeyValue(GrassTestCase):
def test_conversion(self):
- keyvals = checkers.text_to_keyvalue(KEYVAL_TEXT, sep=':', val_sep=',')
+ keyvals = text_to_keyvalue(KEYVAL_TEXT, sep=':', val_sep=',')
expected = {'s': 'Hello',
'str': 'Hello world!',
'f': 1.0,
@@ -78,62 +131,108 @@
self.assertDictEqual(expected, keyvals)
def test_single_values(self):
- keyvals = checkers.text_to_keyvalue("a: 1.5", sep=':')
+ keyvals = text_to_keyvalue("a: 1.5", sep=':')
self.assertDictEqual({'a': 1.5}, keyvals)
- keyvals = checkers.text_to_keyvalue("abc=1", sep='=')
+ keyvals = text_to_keyvalue("abc=1", sep='=')
self.assertDictEqual({'abc': 1}, keyvals)
- keyvals = checkers.text_to_keyvalue("abc=hello", sep='=')
+ keyvals = text_to_keyvalue("abc=hello", sep='=')
self.assertDictEqual({'abc': 'hello'}, keyvals)
def test_strip(self):
- keyvals = checkers.text_to_keyvalue("a: 2.8 ", sep=':')
+ keyvals = text_to_keyvalue("a: 2.8 ", sep=':')
self.assertDictEqual({'a': 2.8}, keyvals)
- keyvals = checkers.text_to_keyvalue("a: 2 ; 2.8 ; ab cd ", sep=':', val_sep=';')
+ keyvals = text_to_keyvalue("a: 2 ; 2.8 ; ab cd ",
+ sep=':', val_sep=';')
self.assertDictEqual({'a': [2, 2.8, 'ab cd']}, keyvals)
- keyvals = checkers.text_to_keyvalue("a : 2 ; 2.8", sep=':', val_sep=';')
+ keyvals = text_to_keyvalue("a : 2 ; 2.8", sep=':', val_sep=';')
self.assertDictEqual({'a': [2, 2.8]}, keyvals)
- keyvals = checkers.text_to_keyvalue("a : \t 2 ;\t2.8", sep=':', val_sep=';')
+ 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 = checkers.text_to_keyvalue("a: 1, ,5,,", sep=':', val_sep=',')
+ keyvals = text_to_keyvalue("a: 1, ,5,,", sep=':', val_sep=',')
self.assertDictEqual({'a': [1, '', 5, '', '']}, keyvals)
def test_empty_value(self):
- keyvals = checkers.text_to_keyvalue("a: ", sep=':')
+ keyvals = text_to_keyvalue("a: ", sep=':')
self.assertDictEqual({'a': ''}, keyvals)
- keyvals = checkers.text_to_keyvalue("a:", sep=':')
+ 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 = checkers.text_to_keyvalue("a", sep=':',
- skip_invalid=True, skip_empty=False)
+ keyvals = text_to_keyvalue("a", sep=':',
+ skip_invalid=True, skip_empty=False)
self.assertDictEqual({}, keyvals)
- self.assertRaises(ValueError, checkers.text_to_keyvalue, "a", sep=':',
- skip_invalid=False, skip_empty=False)
+ 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 = checkers.text_to_keyvalue("", sep=':',
- skip_invalid=False, skip_empty=False)
+ keyvals = text_to_keyvalue("", sep=':',
+ skip_invalid=False, skip_empty=False)
self.assertDictEqual({}, keyvals)
- self.assertRaises(ValueError, checkers.text_to_keyvalue, "\n", sep=':',
- skip_invalid=True, skip_empty=False)
+ self.assertRaises(ValueError, text_to_keyvalue, "\n", sep=':',
+ skip_invalid=True, skip_empty=False)
- keyvals = checkers.text_to_keyvalue("a\n\n", sep=':',
- skip_invalid=True, skip_empty=True)
+ keyvals = text_to_keyvalue("a\n\n", sep=':',
+ skip_invalid=True, skip_empty=True)
self.assertDictEqual({}, keyvals)
def test_separators(self):
- keyvals = checkers.text_to_keyvalue("a=a;b;c", sep='=', val_sep=';')
+ keyvals = text_to_keyvalue("a=a;b;c", sep='=', val_sep=';')
self.assertDictEqual({'a': ['a', 'b', 'c']}, keyvals)
- keyvals = checkers.text_to_keyvalue("a 1;2;3", sep=' ', val_sep=';')
+ 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 = checkers.text_to_keyvalue("a 1 2 3", sep=' ', val_sep=' ')
+ 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(GrassTestCase):
+
+ 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(GrassTestCase):
+
+ def test_shell_script_style(self):
+
+ self.assertDictEqual(parse_key_val(R_UNIVAR_KEYVAL_INT, val_type=int),
+ R_UNIVAR_KEYVAL_INT_DICT)
+
+
\ No newline at end of file
More information about the grass-commit
mailing list