[GRASS-SVN] r61123 - in sandbox/wenzeslaus/gunittest: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jul 2 11:33:32 PDT 2014


Author: wenzeslaus
Date: 2014-07-02 11:33:32 -0700 (Wed, 02 Jul 2014)
New Revision: 61123

Modified:
   sandbox/wenzeslaus/gunittest/case.py
   sandbox/wenzeslaus/gunittest/main.py
   sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py
Log:
gunittest: handling of region in the way that test case must call temporary region functions in set up and tear down

Modified: sandbox/wenzeslaus/gunittest/case.py
===================================================================
--- sandbox/wenzeslaus/gunittest/case.py	2014-07-02 18:22:56 UTC (rev 61122)
+++ sandbox/wenzeslaus/gunittest/case.py	2014-07-02 18:33:32 UTC (rev 61123)
@@ -32,15 +32,57 @@
     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 __init__(self, methodName):
+        super(GrassTestCase, self).__init__(methodName)
+        self._temp_region = None
+
+    def use_temp_region(self):
+        """Use temporary region instead of the standard one for this process.
+
+        If you use this method, you have to call it in `setUp()` and call
+        `del_temp_region()` in `tearDown()`.
+
+        Copies the current region to a temporary region with
+        "g.region save=", then sets WIND_OVERRIDE to refer to that region.
+        """
+        # this should be unique for each object because method name is used
+        name = "tmp.%s" % (self.id())
+        call_module("g.region", save=name, overwrite=True)
+        os.environ['WIND_OVERRIDE'] = name
+        self._temp_region = name
+
+    def del_temp_region(self):
+        """Remove the temporary region.
+
+        Unsets WIND_OVERRIDE and removes any region named by it.
+        """
+        assert self._temp_region
+        name = os.environ.pop('WIND_OVERRIDE')
+        if name != self._temp_region:
+            # be strict about usage of region
+            raise RuntimeError("Inconsistent use of"
+                               " GrassTestCase.use_temp_region, WIND_OVERRIDE"
+                               " or temporary region in general\n"
+                               "Region to which should be now deleted ({n})"
+                               " by GrassTestCase class"
+                               "does not corresond to currently set"
+                               " WIND_OVERRIDE ({c})",
+                               n=self._temp_region, c=name)
+        call_module("g.remove", quiet=True, region=name)
+        # TODO: we don't know if user calls this
+        # so perhaps some decorator which would use with statemet
+        # but we have zero chance of infuencing another test
+        # since we use method-specific name for region
+
     def assertLooksLike(self, actual, reference, msg=None):
         """Test that ``actual`` text is the same as ``referece`` with ellipses.
 
         See :func:`check_text_ellipsis` for details of behavior.
         """
-        self.assert_(isinstance(actual, basestring), (
-                     'actual argument is not a string'))
-        self.assert_(isinstance(reference, basestring), (
-                     'reference argument is not a string'))
+        self.assertTrue(isinstance(actual, basestring), (
+                        'actual argument is not a string'))
+        self.assertTrue(isinstance(reference, basestring), (
+                        'reference argument is not a string'))
         if not check_text_ellipsis(actual=actual, reference=reference):
             # TODO: add support for multiline (first line general, others with details)
             standardMsg = '"%s" does not correspond with "%s"' % (actual,
@@ -70,7 +112,8 @@
                                  ": %s\n" % (module, ", ".join(missing)))
             if mismatch:
                 standardMsg = "%s difference:\n" % module
-                standardMsg += "mismatch values: %s\n" % mismatch
+                standardMsg += "mismatch values (key, reference, actual): %s\n" % mismatch
+                standardMsg += 'command: %s %s' % (module, parameters)
             else:
                 # we can probably remove this once we have more tests
                 # of compare_keyvalue and diff_keyvalue against each other

Modified: sandbox/wenzeslaus/gunittest/main.py
===================================================================
--- sandbox/wenzeslaus/gunittest/main.py	2014-07-02 18:22:56 UTC (rev 61122)
+++ sandbox/wenzeslaus/gunittest/main.py	2014-07-02 18:33:32 UTC (rev 61123)
@@ -126,6 +126,7 @@
         mapset_dir = os.path.join(gisdbase, location, mapset)
         # TODO: perhaps remove dir if exists
         os.mkdir(mapset_dir)
+        # TODO: default region in mapset will be what?
         # copy WIND file from PERMANENT
         # TODO: this should be a function in grass.script (used also in gis_set.py, PyGRASS alos has its way with Mapset)
         # TODO: are premisions an issue here?
@@ -135,8 +136,8 @@
         # TODO: we don't do any HTML escaping, use txt file
         stdout = open(os.path.join(cwd, 'stdout.html'), 'w')
         stderr = open(os.path.join(cwd, 'stderr.html'), 'w')
-        stdout.write('<html><body><pre>')
-        stderr.write('<html><body><pre>')
+        stdout.write('<html><body><h1>%s</h1><pre>' % (module.name + ' stdout'))
+        stderr.write('<html><body><h1>%s</h1><pre>' % (module.name + ' stderr'))
         stdout.flush()  # we need to flush to write our changes before stdout
         stderr.flush()
 

Modified: sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py
===================================================================
--- sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py	2014-07-02 18:22:56 UTC (rev 61122)
+++ sandbox/wenzeslaus/gunittest/testsuite/test_assertions.py	2014-07-02 18:33:32 UTC (rev 61123)
@@ -9,12 +9,13 @@
 import os
 
 import grass.script.core as gcore
+from grass.pygrass.modules import Module
 
 # import gunittest as a package so that relative imports there works
 sys.path.insert(0, os.path.split(os.path.split((os.path.dirname(os.path.abspath(__file__))))[0])[0])
 from gunittest.case import GrassTestCase
-import gunittest
 
+
 class TestTextAssertions(GrassTestCase):
     # pylint: disable=R0904
     def test_assertLooksLike(self):
@@ -88,6 +89,14 @@
 class TestRasterMapAssertations(GrassTestCase):
     # pylint: disable=R0904
 
+    def setUp(self):
+        self.use_temp_region()
+        # TODO: here we should actually not call self.runModule but call_module
+        self.runModule(Module('g.region', rast='elevation', run_=False))
+
+    def tearDown(self):
+        self.del_temp_region()
+
     def test_assertRasterFitsUnivar(self):
         self.assertRasterFitsUnivar('elevation', R_UNIVAR_ELEVATION_SUBSET,
                                     precision=0.01)



More information about the grass-commit mailing list