[GRASS-SVN] r61202 - in grass/trunk/general: g.list/testsuite g.mlist/testsuite g.mremove/testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jul 8 14:37:37 PDT 2014


Author: wenzeslaus
Date: 2014-07-08 14:37:37 -0700 (Tue, 08 Jul 2014)
New Revision: 61202

Modified:
   grass/trunk/general/g.list/testsuite/test_g_list.py
   grass/trunk/general/g.mlist/testsuite/test_g_mlist.py
   grass/trunk/general/g.mremove/testsuite/test_g_mremove.py
Log:
testing: rewrite tests to use gunittest (foreing mapset not expected on the path and not added)

Modified: grass/trunk/general/g.list/testsuite/test_g_list.py
===================================================================
--- grass/trunk/general/g.list/testsuite/test_g_list.py	2014-07-08 21:34:46 UTC (rev 61201)
+++ grass/trunk/general/g.list/testsuite/test_g_list.py	2014-07-08 21:37:37 UTC (rev 61202)
@@ -1,5 +1,5 @@
 
-import unittest
+import gunittest
 from grass.script import read_command
 
 LIST_RASTERS = """----------------------------------------------
@@ -20,11 +20,6 @@
 elev_state_500m           lsat7_2002_61             zipcodes_dbl
 elevation                 lsat7_2002_62
 
-raster files available in mapset <landsat>:
-lsat5_1987_10   lsat5_1987_40   lsat5_1987_70   lsat7_2000_30   lsat7_2000_61
-lsat5_1987_20   lsat5_1987_50   lsat7_2000_10   lsat7_2000_40   lsat7_2000_70
-lsat5_1987_30   lsat5_1987_60   lsat7_2000_20   lsat7_2000_50   lsat7_2000_80
-
 """
 
 LIST_VECTORS = """----------------------------------------------
@@ -47,12 +42,9 @@
 censusblk_swwake          nc_state
 comm_colleges             overpasses
 
-
 """
 
 LIST_GROUPS = """----------------------------------------------
-
-
 imagery group files available in mapset <landsat>:
 lsat7_2000
 
@@ -111,23 +103,6 @@
 zipcodes           South West Wake: Zipcode areas derived from vector map
 zipcodes_dbl       South West Wake: Zipcode areas from vector map, fp
 
-raster files available in mapset <landsat>:
-lsat5_1987_10      LANDSAT-TM5 Band 1 Visible (0.45-0.52um) 30m
-lsat5_1987_20      LANDSAT-TM5 Band 2 Visible (0.52-0.60um) 30m
-lsat5_1987_30      LANDSAT-TM5 Band 3 Visible (0.63-0.69um) 30m
-lsat5_1987_40      LANDSAT-TM5 Band 4 Near Infrared (NIR) (0.76-0.90um) 30m
-lsat5_1987_50      LANDSAT-TM5 Band 5 Near Infrared (NIR) (1.55-1.75um) 30m
-lsat5_1987_60      LANDSAT-TM5 Band 6 Thermal (10.40-12.50um) 120m
-lsat5_1987_70      LANDSAT-TM5 Band 7 Mid Infrared (MIR) (2.08-2.35um) 30m
-lsat7_2000_10      LANDSAT-TM7 Band 1 Visible (0.45-0.52um) 30m
-lsat7_2000_20      LANDSAT-TM7 Band 2 Visible (0.52-0.60um) 30m
-lsat7_2000_30      LANDSAT-TM7 Band 3 Visible (0.63-0.69um) 30m
-lsat7_2000_40      LANDSAT-TM7 Band 4 Near Infrared (NIR) (0.76-0.90um) 30m
-lsat7_2000_50      LANDSAT-TM7 Band 5 Near Infrared (NIR) (1.55-1.75um) 30m
-lsat7_2000_61      LANDSAT-TM7 Band 6 Thermal (10.40-12.50um) 60m Low Gain
-lsat7_2000_70      LANDSAT-TM7 Band 7 Mid Infrared (MIR) (2.08-2.35um) 30m
-lsat7_2000_80      LANDSAT-TM7 Band 8 Panchromatic (PAN) (0.52-0.90um (15m)
-
 """
 
 LIST_RASTERS_TITLES_MAPSET = """----------------------------------------------
@@ -151,18 +126,14 @@
 """
 
 
-class GListTest(unittest.TestCase):
+class GListTest(gunittest.TestCase):
 
-    def setUp(self):
-        pass
-
     def test_list_rasters(self):
         """Test human readable list of rasters.
 
         Supposing we are in user1 of NC and have access
            to landsat (besides PERMANENT).
         """
-        self.maxDiff = None
         stdout = read_command('g.list', type='rast')
         self.assertMultiLineEqual(stdout, LIST_RASTERS)
 
@@ -172,18 +143,16 @@
         Supposing we are in user1 of NC and have access
            to landsat (besides PERMANENT).
         """
-        self.maxDiff = None
         stdout = read_command('g.list', type='vect')
         self.assertMultiLineEqual(stdout, LIST_VECTORS)
 
-    def test_list_groups(self):
-        """Test human readable list of imagery groups.
+    def test_list_groups_in_mapset(self):
+        """Test human readable list of imagery groups in a specific mapset.
 
         Supposing we are in user1 of NC and have access
         to landsat (besides PERMANENT).
         """
-        self.maxDiff = None
-        stdout = read_command('g.list', type='group')
+        stdout = read_command('g.list', type='group', mapset='landsat')
         self.assertMultiLineEqual(stdout, LIST_GROUPS)
 
     def test_list_rasters_in_mapset(self):
@@ -192,34 +161,23 @@
         Supposing we are in user1 of NC and have access
         to landsat (besides PERMANENT).
         """
-        self.maxDiff = None
         stdout = read_command('g.list', type='rast', mapset='landsat')
         self.assertMultiLineEqual(stdout, LIST_RASTERS_MAPSET)
 
     def test_list_rasters_titles(self):
         """Test human readable list of rasters with titles.
-
-        Supposing we are in user1 of NC and have access
-        to landsat (besides PERMANENT).
         """
-        self.maxDiff = None
         stdout = read_command('g.list', flags='f', type='rast')
         self.assertMultiLineEqual(stdout, LIST_RASTERS_TITLES)
 
     def test_list_rasters_titles_in_mapset(self):
         """Test human readable list of rasters with titles
         in a specific mapset.
-
-        Supposing we are in user1 of NC and have access
-        to landsat (besides PERMANENT).
         """
-        self.maxDiff = None
         stdout = read_command('g.list', flags='f', type='rast',
                               mapset='landsat')
         self.assertMultiLineEqual(stdout, LIST_RASTERS_TITLES_MAPSET)
 
-    def tearDown(self):
-        pass
 
 if __name__ == '__main__':
-    unittest.main()
+    gunittest.test()

Modified: grass/trunk/general/g.mlist/testsuite/test_g_mlist.py
===================================================================
--- grass/trunk/general/g.mlist/testsuite/test_g_mlist.py	2014-07-08 21:34:46 UTC (rev 61201)
+++ grass/trunk/general/g.mlist/testsuite/test_g_mlist.py	2014-07-08 21:37:37 UTC (rev 61202)
@@ -1,39 +1,55 @@
+"""g.mlist tests"""
 
-import unittest
-from grass.script import start_command
-import subprocess
+import gunittest
+from gunittest.gmodules import SimpleModule
 
-class GMlistWrongParamertersTest(unittest.TestCase):
 
-    def setUp(self):
-        pass
+class GMlistWrongParamertersTest(gunittest.TestCase):
+    """Test wrong input of parameters for g.mlist module"""
 
+    @classmethod
+    def setUpClass(cls):
+        """Create maps in a small region."""
+        cls.use_temp_region()
+        cls.runModule("g.region", s=0, n=5, w=0, e=5, res=1)
+
+    @classmethod
+    def tearDownClass(cls):
+        """Remove temporary region"""
+        cls.del_temp_region()
+
     def test_pt_flags(self):
-        self.maxDiff = None
-        p = start_command('g.mlist', flags='pt', type='rast', stderr=subprocess.PIPE)
-        stderr = p.communicate()[1]
-        self.assertEqual(stderr, "ERROR: -p/-f and -m/-t are mutually exclusive\n")
+        """Test that -p and -t flags are exclusive"""
+        module = SimpleModule('g.mlist', flags='pt', type='rast')
+        self.assertModuleFail(module)
+        stderr = module.outputs.stderr
+        self.assertIn('-p', stderr)
+        self.assertIn('-t', stderr)
 
     def test_ft_flags(self):
-        self.maxDiff = None
-        p = start_command('g.mlist', flags='ft', type='rast', stderr=subprocess.PIPE)
-        stderr = p.communicate()[1]
-        self.assertEqual(stderr, "ERROR: -p/-f and -m/-t are mutually exclusive\n")
+        """Test that -f and -t flags are exclusive"""
+        module = SimpleModule('g.mlist', flags='ft', type='rast')
+        self.assertModuleFail(module)
+        stderr = module.outputs.stderr
+        self.assertIn('-f', stderr)
+        self.assertIn('-t', stderr)
 
     def test_pf_flags(self):
-        self.maxDiff = None
-        p = start_command('g.mlist', flags='pf', type='rast', stderr=subprocess.PIPE)
-        stderr = p.communicate()[1]
-        self.assertEqual(stderr, "ERROR: -p and -f are mutually exclusive\n")
+        """Test that -p and -f flags are exclusive"""
+        module = SimpleModule('g.mlist', flags='pf', type='rast')
+        self.assertModuleFail(module)
+        stderr = module.outputs.stderr
+        self.assertIn('-p', stderr)
+        self.assertIn('-f', stderr)
 
     def test_re_flags(self):
-        self.maxDiff = None
-        p = start_command('g.mlist', flags='re', type='rast', stderr=subprocess.PIPE)
-        stderr = p.communicate()[1]
-        self.assertEqual(stderr, "ERROR: -r and -e are mutually exclusive\n")
+        """Test that -r and -e flags are exclusive"""
+        module = SimpleModule('g.mlist', flags='re', type='rast')
+        self.assertModuleFail(module)
+        stderr = module.outputs.stderr
+        self.assertIn('-r', stderr)
+        self.assertIn('-e', stderr)
 
-    def tearDown(self):
-        pass
 
 if __name__ == '__main__':
-    unittest.main()
+    gunittest.test()

Modified: grass/trunk/general/g.mremove/testsuite/test_g_mremove.py
===================================================================
--- grass/trunk/general/g.mremove/testsuite/test_g_mremove.py	2014-07-08 21:34:46 UTC (rev 61201)
+++ grass/trunk/general/g.mremove/testsuite/test_g_mremove.py	2014-07-08 21:37:37 UTC (rev 61202)
@@ -1,10 +1,13 @@
+"""Test of g.mremove module"""
 
-import unittest
-import subprocess
-from grass.script import start_command, read_command, run_command
-import grass.script.core as gcore
+# TODO: rmapcalc probably fatals, replace or add raise on error?
 from grass.script.raster import mapcalc as rmapcalc
 
+import gunittest
+from gunittest.gutils import get_curret_mapset
+from gunittest.gmodules import SimpleModule
+
+# when used user1 must be replaced by current mapset
 REMOVE_RASTERS = """rast/test_map_0 at user1
 rast/test_map_1 at user1
 rast/test_map_2 at user1
@@ -32,64 +35,75 @@
 """
 
 
-class GMremoveTest(unittest.TestCase):
+class GMRemoveTest(gunittest.TestCase):
+    """Test removing with g.mremove"""
 
-    def setUp(self):
-        gcore.set_raise_on_error(True)
-        gcore.use_temp_region()
-        ret = run_command("g.region", s=0, n=5, w=0, e=5, res=1)
-        if ret != 0:
-            gcore.fatal("g.region failed")
+    @classmethod
+    def setUpClass(cls):
+        """Set up small region for fast map creation."""
+        cls.use_temp_region()
+        cls.runModule("g.region", s=0, n=5, w=0, e=5, res=1)
 
+    @classmethod
+    def tearDownClass(cls):
+        """Remove temporary region"""
+        cls.del_temp_region()
+
     def test_remove_procedure(self):
-        self.maxDiff = None
-
+        """Test that maps are removed only with -f"""
         for i in range(0, 10):
             rmapcalc("test_map_%i = 100" % i)
         rmapcalc("test_two = 2")
-        p = start_command('g.mremove', rast='test_map_*,*two',
-                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-        stdout = p.communicate()[0]
-        self.assertMultiLineEqual(stdout, REMOVE_RASTERS)
-        p = start_command('g.mremove', rast='test_map_*,*two', flags='f',
-                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-        stdout, stderr = p.communicate()
-        self.assertMultiLineEqual(stdout, '')
-        self.assertMultiLineEqual(stderr, REMOVING_RASTERS_LOG)
 
-    #@unittest.skip("only for the new g.mremove with g.mlist interface")
-    def test_remove_procedure(self):
-        self.maxDiff = None
+        module = SimpleModule('g.mremove',
+                              type='rast', pattern='test_map_*,*two')
+        self.assertModule(module)
+        self.assertMultiLineEqual(module.outputs.stdout,
+                                  REMOVE_RASTERS.replace('user1',
+                                                         get_curret_mapset()))
 
+        module = SimpleModule('g.mremove', type='rast',
+                              pattern='test_map_*,*two', flags='f')
+        self.assertModule(module)
+        self.assertMultiLineEqual(module.outputs.stdout, '')
+        self.assertMultiLineEqual(module.outputs.stderr, REMOVING_RASTERS_LOG)
+
+    def test_remove_procedure_exclude(self):
+        """Test that exclude does not list excluded maps"""
         rmapcalc("test_apples = 100")
         rmapcalc("test_oranges = 200")
         rmapcalc("test_apples_big = 300")
         rmapcalc("test_apples_small = 300")
-        p = start_command('g.mremove', type='rast',
-                          pattern='test_{apples,oranges}*',
-                          exclude="*_small",
-                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-        stdout = p.communicate()[0]
-        self.assertMultiLineEqual(stdout,
+        module = SimpleModule('g.mremove', type='rast',
+                              pattern='test_{apples,oranges}*',
+                              exclude="*_small")
+        self.assertModule(module)
+        self.assertMultiLineEqual(module.outputs.stdout,
                                   'rast/test_apples at user1\n'
                                   'rast/test_apples_big at user1\n'
-                                  'rast/test_oranges at user1\n')
-        p = start_command('g.mremove', type='rast',
-                          pattern='test_{apples,oranges}{_small,_big,*}',
-                          flags='f',
-                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-        stdout, stderr = p.communicate()
-        self.assertMultiLineEqual(stdout, '')
-        self.assertRegexpMatches(stderr, "(.*<.+>[^\n]*\n){4}", "4 maps should be removed")
+                                  'rast/test_oranges at user1\n'.replace(
+                                      'user1', get_curret_mapset()))
+        module = SimpleModule('g.mremove', type='rast',
+                              pattern='test_{apples,oranges}{_small,_big,*}',
+                              flags='f')
+        self.assertModule(module)
+        self.assertMultiLineEqual(module.outputs.stdout, '')
+        self.assertRegexpMatches(module.outputs.stderr, "(.*<.+>[^\n]*\n){4}",
+                                 msg="4 maps should be removed")
 
+
+class GMRemoveWrongInputTest(gunittest.TestCase):
+    """Test wrong input of parameters for g.mlist module"""
+
     def test_re_flags(self):
-        p = start_command('g.mremove', flags='re', type='rast', pattern='xxxyyyzzz',
-                          stderr=subprocess.PIPE)
-        stderr = p.communicate()[1]
-        self.assertEqual(stderr, "ERROR: -r and -e are mutually exclusive\n")
+        """Test that -r and -e flags are exclusive"""
+        module = SimpleModule('g.mremove', flags='re',
+                              type='rast', pattern='xxxyyyzzz')
+        self.assertModuleFail(module)
+        stderr = module.outputs.stderr
+        self.assertIn('-r', stderr)
+        self.assertIn('-e', stderr)
 
-    def tearDown(self):
-        gcore.del_temp_region()
 
 if __name__ == '__main__':
-    unittest.main()
+    gunittest.test()



More information about the grass-commit mailing list