[GRASS-SVN] r54668 - in grass-addons/grass7/raster/r.agent: libagent tests

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jan 16 05:09:41 PST 2013


Author: mic
Date: 2013-01-16 05:09:40 -0800 (Wed, 16 Jan 2013)
New Revision: 54668

Modified:
   grass-addons/grass7/raster/r.agent/libagent/grassland.py
   grass-addons/grass7/raster/r.agent/libagent/playground.py
   grass-addons/grass7/raster/r.agent/tests/test_grassland.py
   grass-addons/grass7/raster/r.agent/tests/test_playground.py
Log:
add tests and move methods to upper class

Modified: grass-addons/grass7/raster/r.agent/libagent/grassland.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/grassland.py	2013-01-16 11:08:17 UTC (rev 54667)
+++ grass-addons/grass7/raster/r.agent/libagent/grassland.py	2013-01-16 13:09:40 UTC (rev 54668)
@@ -15,6 +15,7 @@
 
 class GrassLand(playground.Playground):
     """A GrassLand is a Playground and the interface to GRASS."""
+
     def __init__(self):
         """Create a Playground with all the relevant info by GRASS"""
         self.layers = dict()
@@ -23,25 +24,7 @@
         if self.region['ewres'] != self.region['nsres']:
             raise error.DataError("r.agent::libagent.playground.Playground()",
                                     "Only square raster cells make sense.")
-    def getbound(self, bound):
-        """
-        Return the requested bound, takes: 'n', 's', 'w', or 'e'
-        @param string bound
-        @return float the outermost coordinate
-        """
-        if bound == "n" or bound == "s" or bound == "w" or bound == "e":
-            return self.region[bound]
-    def setlayer(self, layername, layer, force=False):
-        """
-        Put an existing map layer to the layer collection
-        @param string name of the layer
-        @param list a map layer
-        @param boolean optional, whether to overwrite values if key exists
-        """
-        if not force and self.layers.has_key(layername):
-            raise error.Error("r.agent::libagent.playground.Playground()",
-                                    "May not overwrite existing layer.")
-        self.layers[layername] = layer
+
     def setgrasslayer(self, layername, grassmapname, force=False):
         """
         Put an existing map from GRASS to the layer collection
@@ -55,6 +38,7 @@
             layer.read(grassmapname)
             self.grassmapnames[layername] = grassmapname
         self.setlayer(layername, layer, force)
+
     def createlayer(self, layername, force=False):
         """
         Create a new layer and add it to the layer collection
@@ -62,6 +46,7 @@
         @param string name of a GRASS map layer or False if layer is only local
         """
         self.setgrasslayer(layername, False, force)
+
     def getlayer(self, layername):
         """
         Return a layer from the collection by its name
@@ -72,6 +57,7 @@
         if self.layers.has_key(layername):
             retval = self.layers[layername]
         return retval
+
     def removelayer(self, layername):
         """
         Remove (forget about) the layer named from the layer collection
@@ -79,12 +65,14 @@
         """
         if self.layers.has_key(layername):
             self.layers.pop(layername)
+
     def writelayer(self, layername, force=False):
         if self.layers.has_key(layername) and \
           self.grassmapnames.has_key(layername):
             grassmapname = self.grassmapnames[layername]
             layer = self.layers[layername]
             layer.write(grassmapname, overwrite=force)
+
     def writelayerasnew(self, layername, grassmapname):
         if self.layers.has_key(layername):
             layer = self.layers[layername]

Modified: grass-addons/grass7/raster/r.agent/libagent/playground.py
===================================================================
--- grass-addons/grass7/raster/r.agent/libagent/playground.py	2013-01-16 11:08:17 UTC (rev 54667)
+++ grass-addons/grass7/raster/r.agent/libagent/playground.py	2013-01-16 13:09:40 UTC (rev 54668)
@@ -9,28 +9,53 @@
               for details.
 """
 
+import error
+
 class Playground(object):
     """A Playground is a major component of a World, defining
        and organizing space."""
+
     def __init__(self):
         self.layers = dict()
         self.region = dict(n=0,s=0,w=0,e=0)
+
     def getregion(self):
         """
         Return the region information
         @return dict region
         """
         return self.region
+
     def getbound(self, bound):
-        pass
+        """
+        Return the requested bound, takes: 'n', 's', 'w', or 'e'
+        @param string bound
+        @return float the outermost coordinate
+        """
+        if bound == "n" or bound == "s" or bound == "w" or bound == "e":
+            return self.region[bound]
+
     def setlayer(self, layername, layer, force=False):
-        pass
+        """
+        Put an existing map layer to the layer collection
+        @param string name of the layer
+        @param list a map layer
+        @param boolean optional, whether to overwrite values if key exists
+        """
+        if not force and self.layers.has_key(layername):
+            raise error.Error("r.agent::libagent.playground.Playground()",
+                                    "May not overwrite existing layer.")
+        self.layers[layername] = layer
+
     def createlayer(self, layername, grassmap=False):
         pass
+
     def getlayer(self, layername):
         return []
+
     def removelayer(self, layername):
         pass
+
     def writelayer(self, layername):
         pass
 

Modified: grass-addons/grass7/raster/r.agent/tests/test_grassland.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_grassland.py	2013-01-16 11:08:17 UTC (rev 54667)
+++ grass-addons/grass7/raster/r.agent/tests/test_grassland.py	2013-01-16 13:09:40 UTC (rev 54668)
@@ -9,8 +9,17 @@
         self.pg = grassland.GrassLand()
 
     def test_getregion(self):
-        self.pg.getregion()
+        self.assertIsNotNone(self.pg.getregion())
 
+    def test_getbound(self):
+        self.assertIsNotNone(self.pg.getbound("n"))
+        self.assertIsNotNone(self.pg.getbound("s"))
+        self.assertIsNotNone(self.pg.getbound("w"))
+        self.assertIsNotNone(self.pg.getbound("e"))
+
+#    def test_setlayer(self):
+        # Enought if tested from Playground and e.g. setgrasslayer below..
+
 #    def tearDown(self):
 
 

Modified: grass-addons/grass7/raster/r.agent/tests/test_playground.py
===================================================================
--- grass-addons/grass7/raster/r.agent/tests/test_playground.py	2013-01-16 11:08:17 UTC (rev 54667)
+++ grass-addons/grass7/raster/r.agent/tests/test_playground.py	2013-01-16 13:09:40 UTC (rev 54668)
@@ -9,8 +9,24 @@
         self.pg = playground.Playground()
 
     def test_getregion(self):
-        self.pg.getregion()
+        self.assertIsNotNone(self.pg.getregion())
 
+    def test_getbound(self):
+        self.assertIsNotNone(self.pg.getbound("n"))
+        self.assertIsNotNone(self.pg.getbound("s"))
+        self.assertIsNotNone(self.pg.getbound("w"))
+        self.assertIsNotNone(self.pg.getbound("e"))
+
+    def test_setlayer(self):
+        layer = [0]
+        key = "foo"
+        self.pg.setlayer(key, layer)
+        self.assertIs(self.pg.layers[key], layer)
+        self.assertRaises(Exception,  self.pg.setlayer, (key, layer))
+        layer = [0]
+        self.assertIsNot(self.pg.layers[key], layer)
+        self.pg.setlayer(key, layer, True)
+        self.assertIs(self.pg.layers[key], layer)
+
 #    def tearDown(self):
 
-



More information about the grass-commit mailing list