[GRASS-SVN] r61019 - in sandbox/wenzeslaus/gunittest: . testsuite
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Jun 27 13:50:02 PDT 2014
Author: wenzeslaus
Date: 2014-06-27 13:50:02 -0700 (Fri, 27 Jun 2014)
New Revision: 61019
Modified:
sandbox/wenzeslaus/gunittest/case.py
sandbox/wenzeslaus/gunittest/checkers.py
sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py
Log:
gunittest: add precision handling to assert map properties (r.univar, ...) methods
Modified: sandbox/wenzeslaus/gunittest/case.py
===================================================================
--- sandbox/wenzeslaus/gunittest/case.py 2014-06-27 19:51:37 UTC (rev 61018)
+++ sandbox/wenzeslaus/gunittest/case.py 2014-06-27 20:50:02 UTC (rev 61019)
@@ -36,7 +36,6 @@
Always use keyword arguments for all parameters other than first two. For
the firt two, it is recommened to use keyword arguments but not required.
"""
-
def assertLooksLike(self, actual, reference, msg=None):
"""Test that ``actual`` text is the same as ``referece`` with ellipses.
@@ -52,31 +51,41 @@
reference)
self.fail(self._formatMessage(msg, standardMsg))
- # TODO: support precision for all functions
+ # TODO: decide if precision is mandatory
+ # (note that we don't need precision for strings and usually for integers)
# TODO: auto-determine precision based on the map type
# TODO: we can have also more general function without the subset reference
def assertCommandKeyValue(self, module, parameters, reference, sep,
- msg=None):
+ precision=None, msg=None):
if isinstance(reference, basestring):
reference = text_to_keyvalue(reference, sep=sep)
stdout = call_module(module, **parameters)
raster_univar = text_to_keyvalue(stdout, sep=sep)
if not compare_keyvalue(dict_a=reference, dict_b=raster_univar,
- a_is_subset=True):
+ a_is_subset=True, precision=precision):
unused, missing, mismatch = diff_keyvalue(dict_a=reference,
dict_b=raster_univar,
- a_is_subset=True)
+ a_is_subset=True,
+ precision=precision)
if missing:
raise ValueError("%s output does not contain"
" the following keys"
" provided in reference"
": %s\n" % (module, ", ".join(missing)))
- standardMsg = "%s difference:\n" % module
if mismatch:
+ standardMsg = "%s difference:\n" % module
standardMsg += "mismatch values: %s\n" % mismatch
+ else:
+ # we can probably remove this once we have more tests
+ # of compare_keyvalue and diff_keyvalue against each other
+ raise RuntimeError("compare_keyvalue() showed difference but"
+ " diff_keyvalue() did not. This can be"
+ " a bug in one of them or in the caller"
+ " (assertCommandKeyValue())")
self.fail(self._formatMessage(msg, standardMsg))
- def assertRasterFitsUnivar(self, raster, reference, msg=None):
+ def assertRasterFitsUnivar(self, raster, reference,
+ precision=None, msg=None):
r"""Test that raster map has the values obtained by r.univar module.
The function does not require all values from r.univar.
@@ -89,9 +98,6 @@
Use keyword arguments syntax for all function parameters.
- .. todo::
- support precision
-
Does not -e (extended statistics) flag, use `assertCommandKeyValue`
for the full interface of arbitrary module.
"""
@@ -99,9 +105,11 @@
parameters=dict(map=raster,
separator='=',
flags='g'),
- reference=reference, msg=msg, sep='=')
+ reference=reference, msg=msg, sep='=',
+ precision=precision)
- def assertRasterFitsInfo(self, raster, reference, msg=None):
+ def assertRasterFitsInfo(self, raster, reference,
+ precision=None, msg=None):
r"""Test that raster map has the values obtained by v.univar module.
The function does not require all values from v.univar.
@@ -113,18 +121,17 @@
Use keyword arguments syntax for all function parameters.
- .. todo::
- support precision
-
This function supports values obtainded -r (range) and
-e (extended metadata) flags.
"""
self.assertCommandKeyValue(module='r.info',
parameters=dict(map=raster, flags='gre'),
- reference=reference, msg=msg, sep='=')
+ reference=reference, msg=msg, sep='=',
+ precision=precision)
def assertVectorFitsUnivar(self, map, column, reference, msg=None,
- layer=None, type=None, where=None):
+ layer=None, type=None, where=None,
+ precision=None):
r"""Test that vector map has the values obtained by v.univar module.
The function does not require all values from v.univar.
@@ -137,9 +144,6 @@
Use keyword arguments syntax for all function parameters.
- .. todo::
- support precision
-
Does not support -d (geometry distances) flag, -e (extended statistics)
flag and few other, use `assertCommandKeyValue` for the full interface
of arbitrary module.
@@ -153,7 +157,8 @@
parameters.update(where=where)
self.assertCommandKeyValue(module='v.univar',
parameters=parameters,
- reference=reference, msg=msg, sep='=')
+ reference=reference, msg=msg, sep='=',
+ precision=precision)
def assertFileExists(self, filename, msg=None,
skip_size_check=False, skip_access_check=False):
Modified: sandbox/wenzeslaus/gunittest/checkers.py
===================================================================
--- sandbox/wenzeslaus/gunittest/checkers.py 2014-06-27 19:51:37 UTC (rev 61018)
+++ sandbox/wenzeslaus/gunittest/checkers.py 2014-06-27 20:50:02 UTC (rev 61019)
@@ -200,6 +200,8 @@
return kvdict
+# TODO: decide if there should be some default for precision
+# TODO: decide if None is valid, and use some default or no compare
def values_equal(value_a, value_b, precision=0.000001):
"""
>>> values_equal(1.022, 1.02, precision=0.01)
@@ -213,23 +215,34 @@
>>> values_equal('Hello', 'hello')
False
"""
- # each if need to handle only not equal state
+ # each if body needs to handle only not equal state
+
if isinstance(value_a, float) and isinstance(value_b, float):
# both values are float
+ # this could be also changed to is None and raise TypeError
+ # in Python 2 None is smaller than anything
+ # in Python 3 None < 3 raises TypeError
+ precision = float(precision)
if abs(value_a - value_b) > precision:
return False
+
elif (isinstance(value_a, float) and isinstance(value_b, int)) or \
(isinstance(value_b, float) and isinstance(value_a, int)):
# on is float the other is int
+ # don't accept None
+ precision = float(precision)
# we will apply precision to int-float comparison
# rather than converting both to integer
+ # (as in the original function from core)
if abs(value_a - value_b) > precision:
return False
+
elif isinstance(value_a, int) and isinstance(value_b, int) and \
- int(precision) > 0:
+ precision and int(precision) > 0:
# both int but precision applies for them
if abs(value_a - value_b) > precision:
return False
+
elif isinstance(value_a, list) and isinstance(value_b, list):
if len(value_a) != len(value_b):
return False
Modified: sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py
===================================================================
--- sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py 2014-06-27 19:51:37 UTC (rev 61018)
+++ sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py 2014-06-27 20:50:02 UTC (rev 61019)
@@ -84,13 +84,16 @@
mean=240.437
"""
+
class TestRasterMapAssertations(GrassTestCase):
# pylint: disable=R0904
+
def test_assertRasterFitsUnivar(self):
- self.assertRasterFitsUnivar('elevation', R_UNIVAR_ELEVATION_SUBSET)
+ self.assertRasterFitsUnivar('elevation', R_UNIVAR_ELEVATION_SUBSET,
+ precision=0.01)
self.assertRaises(self.failureException,
self.assertRasterFitsUnivar,
- 'aspect', R_UNIVAR_ELEVATION_SUBSET)
+ 'aspect', R_UNIVAR_ELEVATION_SUBSET, precision=0.01)
self.assertRaises(ValueError,
self.assertRasterFitsUnivar,
'elevation', RANDOM_KEYVALUES)
@@ -105,8 +108,10 @@
'elevation', RANDOM_KEYVALUES)
def test_common_values_info_univar(self):
- self.assertRasterFitsUnivar('elevation', ELEVATION_MINMAX)
- self.assertRasterFitsInfo('elevation', ELEVATION_MINMAX)
+ self.assertRasterFitsUnivar('elevation',
+ ELEVATION_MINMAX, precision=0.01)
+ self.assertRasterFitsInfo('elevation',
+ ELEVATION_MINMAX, precision=0.01)
def test_dict_as_parameter(self):
# this also tests if we are using r.info -e flag
@@ -117,11 +122,13 @@
# pylint: disable=R0904
def test_assertVectorFitsUnivar(self):
self.assertVectorFitsUnivar(map='bridges', column='WIDTH',
- reference=V_UNIVAR_BRIDGES_WIDTH_SUBSET)
+ reference=V_UNIVAR_BRIDGES_WIDTH_SUBSET,
+ precision=0.01)
self.assertRaises(self.failureException,
self.assertVectorFitsUnivar,
map='bridges', column='YEAR_BUILT',
- reference=V_UNIVAR_BRIDGES_WIDTH_SUBSET)
+ reference=V_UNIVAR_BRIDGES_WIDTH_SUBSET,
+ precision=0.01)
self.assertRaises(ValueError,
self.assertVectorFitsUnivar,
map='bridges', column='WIDTH',
More information about the grass-commit
mailing list