[GRASS-SVN] r62707 - in grass/trunk/lib/python/pygrass/vector: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Nov 11 07:43:54 PST 2014


Author: zarch
Date: 2014-11-11 07:43:54 -0800 (Tue, 11 Nov 2014)
New Revision: 62707

Modified:
   grass/trunk/lib/python/pygrass/vector/table.py
   grass/trunk/lib/python/pygrass/vector/testsuite/test_table.py
Log:
pygrass: Fix Table update and Column add methods, Fix test.

Modified: grass/trunk/lib/python/pygrass/vector/table.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/table.py	2014-11-11 15:23:43 UTC (rev 62706)
+++ grass/trunk/lib/python/pygrass/vector/table.py	2014-11-11 15:43:54 UTC (rev 62707)
@@ -398,7 +398,7 @@
         >>> remove('mycensus', 'vect')
 
         """
-        def check_col(col_type):
+        def check(col_type):
             """Check the column type if it is supported by GRASS
 
             :param col_type: the type of column
@@ -409,24 +409,16 @@
             if 'VARCHAR' in col_type or col_type.upper() not in valid_type:
                 str_err = "Type is not supported, supported types are: %s"
                 raise TypeError(str_err % ", ".join(valid_type))
+            return col_type
 
-        if isinstance(col_name, unicode):
-            check_col(col_type)
-        else:
-            if len(col_name) == len(col_type):
-                cvars = []
-                for name, ctype in zip(col_name, col_type):
-                    check_col(ctype)
-                    cvars.append('%s %s' % (name, ctype))
-                col_name = ''
-                col_type = ','.join(cvars)
-            else:
-                str_err = "The lenghts of the columns are different:\n%r\n%r"
-                raise TypeError(str_err % (col_name, col_type))
+        col_type = ([check(col_type), ] if isinstance(col_type, (str, unicode))
+                    else [check(col) for col in col_type])
+        col_name = ([col_name, ] if isinstance(col_name, (str, unicode))
+                    else col_name)
+        sqlcode = [sql.ADD_COL.format(tname=self.tname, cname=cn, ctype=ct)
+                   for cn, ct in zip(col_name, col_type)]
         cur = self.conn.cursor()
-        cur.execute(sql.ADD_COL.format(tname=self.tname,
-                                       cname=col_name,
-                                       ctype=col_type))
+        cur.executescript('\n'.join(sqlcode))
         self.conn.commit()
         cur.close()
         self.update_odict()
@@ -1022,7 +1014,7 @@
         cur = cursor if cursor else self.conn.cursor()
         if self.exist(cursor=cur):
             used = db_table_in_vector(self.name)
-            if len(used) > 0 and not force:
+            if used is not None and len(used) > 0 and not force:
                 print(_("Deleting table <%s> which is attached"
                         " to following map(s):") % self.name)
                 for vect in used:
@@ -1125,7 +1117,8 @@
         :type cursor: Cursor object
         """
         cur = cursor if cursor else self.conn.cursor()
-        return cur.execute(self.columns.update_str, values)
+        vals = list(values) + [key, ]
+        return cur.execute(self.columns.update_str, vals)
 
     def create(self, cols, name=None, overwrite=False, cursor=None):
         """Create a new table

Modified: grass/trunk/lib/python/pygrass/vector/testsuite/test_table.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/testsuite/test_table.py	2014-11-11 15:23:43 UTC (rev 62706)
+++ grass/trunk/lib/python/pygrass/vector/testsuite/test_table.py	2014-11-11 15:43:54 UTC (rev 62707)
@@ -195,10 +195,11 @@
 
     def test_update(self):
         """Test Table.update method"""
-        vals = (1111, 0.1111, 'test')
+        vals = (1122, 0.1122, 'test')
         cat = 1
         cur = self.connection.cursor()
         self.table.update(cat, list(vals), cursor=cur)
+        self.connection.commit()
         sqlquery = "SELECT cint, creal, ctxt FROM %s WHERE cat=%d"
         cur.execute(sqlquery % (self.tname, cat))
         self.assertTupleEqual(vals, cur.fetchone())



More information about the grass-commit mailing list