Browse Source

testing: rewrite tests to use gunittest (foreing mapset not expected on the path and not added)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@61202 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 10 years ago
parent
commit
2c87abc75c

+ 6 - 48
general/g.list/testsuite/test_g_list.py

@@ -1,5 +1,5 @@
 
 
-import unittest
+import gunittest
 from grass.script import read_command
 from grass.script import read_command
 
 
 LIST_RASTERS = """----------------------------------------------
 LIST_RASTERS = """----------------------------------------------
@@ -20,11 +20,6 @@ elev_srtm_30m             lsat7_2002_50             zipcodes
 elev_state_500m           lsat7_2002_61             zipcodes_dbl
 elev_state_500m           lsat7_2002_61             zipcodes_dbl
 elevation                 lsat7_2002_62
 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 = """----------------------------------------------
 LIST_VECTORS = """----------------------------------------------
@@ -47,12 +42,9 @@ census_wake2000           lakes                     zipcodes_wake
 censusblk_swwake          nc_state
 censusblk_swwake          nc_state
 comm_colleges             overpasses
 comm_colleges             overpasses
 
 
-
 """
 """
 
 
 LIST_GROUPS = """----------------------------------------------
 LIST_GROUPS = """----------------------------------------------
-
-
 imagery group files available in mapset <landsat>:
 imagery group files available in mapset <landsat>:
 lsat7_2000
 lsat7_2000
 
 
@@ -111,23 +103,6 @@ urban              South West Wake: Urban areas derived from vector map
 zipcodes           South West Wake: Zipcode areas derived from vector map
 zipcodes           South West Wake: Zipcode areas derived from vector map
 zipcodes_dbl       South West Wake: Zipcode areas from vector map, fp
 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 = """----------------------------------------------
 LIST_RASTERS_TITLES_MAPSET = """----------------------------------------------
@@ -151,10 +126,7 @@ lsat7_2000_80      LANDSAT-TM7 Band 8 Panchromatic (PAN) (0.52-0.90um (15m)
 """
 """
 
 
 
 
-class GListTest(unittest.TestCase):
-
-    def setUp(self):
-        pass
+class GListTest(gunittest.TestCase):
 
 
     def test_list_rasters(self):
     def test_list_rasters(self):
         """Test human readable list of rasters.
         """Test human readable list of rasters.
@@ -162,7 +134,6 @@ class GListTest(unittest.TestCase):
         Supposing we are in user1 of NC and have access
         Supposing we are in user1 of NC and have access
            to landsat (besides PERMANENT).
            to landsat (besides PERMANENT).
         """
         """
-        self.maxDiff = None
         stdout = read_command('g.list', type='rast')
         stdout = read_command('g.list', type='rast')
         self.assertMultiLineEqual(stdout, LIST_RASTERS)
         self.assertMultiLineEqual(stdout, LIST_RASTERS)
 
 
@@ -172,18 +143,16 @@ class GListTest(unittest.TestCase):
         Supposing we are in user1 of NC and have access
         Supposing we are in user1 of NC and have access
            to landsat (besides PERMANENT).
            to landsat (besides PERMANENT).
         """
         """
-        self.maxDiff = None
         stdout = read_command('g.list', type='vect')
         stdout = read_command('g.list', type='vect')
         self.assertMultiLineEqual(stdout, LIST_VECTORS)
         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
         Supposing we are in user1 of NC and have access
         to landsat (besides PERMANENT).
         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)
         self.assertMultiLineEqual(stdout, LIST_GROUPS)
 
 
     def test_list_rasters_in_mapset(self):
     def test_list_rasters_in_mapset(self):
@@ -192,34 +161,23 @@ class GListTest(unittest.TestCase):
         Supposing we are in user1 of NC and have access
         Supposing we are in user1 of NC and have access
         to landsat (besides PERMANENT).
         to landsat (besides PERMANENT).
         """
         """
-        self.maxDiff = None
         stdout = read_command('g.list', type='rast', mapset='landsat')
         stdout = read_command('g.list', type='rast', mapset='landsat')
         self.assertMultiLineEqual(stdout, LIST_RASTERS_MAPSET)
         self.assertMultiLineEqual(stdout, LIST_RASTERS_MAPSET)
 
 
     def test_list_rasters_titles(self):
     def test_list_rasters_titles(self):
         """Test human readable list of rasters with titles.
         """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')
         stdout = read_command('g.list', flags='f', type='rast')
         self.assertMultiLineEqual(stdout, LIST_RASTERS_TITLES)
         self.assertMultiLineEqual(stdout, LIST_RASTERS_TITLES)
 
 
     def test_list_rasters_titles_in_mapset(self):
     def test_list_rasters_titles_in_mapset(self):
         """Test human readable list of rasters with titles
         """Test human readable list of rasters with titles
         in a specific mapset.
         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',
         stdout = read_command('g.list', flags='f', type='rast',
                               mapset='landsat')
                               mapset='landsat')
         self.assertMultiLineEqual(stdout, LIST_RASTERS_TITLES_MAPSET)
         self.assertMultiLineEqual(stdout, LIST_RASTERS_TITLES_MAPSET)
 
 
-    def tearDown(self):
-        pass
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    unittest.main()
+    gunittest.test()

+ 41 - 25
general/g.mlist/testsuite/test_g_mlist.py

@@ -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):
     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):
     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):
     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):
     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__':
 if __name__ == '__main__':
-    unittest.main()
+    gunittest.test()

+ 61 - 47
general/g.mremove/testsuite/test_g_mremove.py

@@ -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
 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@user1
 REMOVE_RASTERS = """rast/test_map_0@user1
 rast/test_map_1@user1
 rast/test_map_1@user1
 rast/test_map_2@user1
 rast/test_map_2@user1
@@ -32,64 +35,75 @@ Removing raster <test_two>
 """
 """
 
 
 
 
-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)
 
 
-    def test_remove_procedure(self):
-        self.maxDiff = None
+    @classmethod
+    def tearDownClass(cls):
+        """Remove temporary region"""
+        cls.del_temp_region()
 
 
+    def test_remove_procedure(self):
+        """Test that maps are removed only with -f"""
         for i in range(0, 10):
         for i in range(0, 10):
             rmapcalc("test_map_%i = 100" % i)
             rmapcalc("test_map_%i = 100" % i)
         rmapcalc("test_two = 2")
         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_apples = 100")
         rmapcalc("test_oranges = 200")
         rmapcalc("test_oranges = 200")
         rmapcalc("test_apples_big = 300")
         rmapcalc("test_apples_big = 300")
         rmapcalc("test_apples_small = 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@user1\n'
                                   'rast/test_apples@user1\n'
                                   'rast/test_apples_big@user1\n'
                                   'rast/test_apples_big@user1\n'
-                                  'rast/test_oranges@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@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):
     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__':
 if __name__ == '__main__':
-    unittest.main()
+    gunittest.test()