[migration/test] use numeric(precision, scale) instead of geometry
authorJulien Cristau <julien.cristau@logilab.fr>
Wed, 09 Dec 2015 17:44:18 +0100
changeset 11006 096adb786873
parent 11005 f8417bd135ed
child 11007 f9386e3bf3e8
[migration/test] use numeric(precision, scale) instead of geometry The latter requires the postgis extension which is overkill for this test.
server/test/data-migractions/cubes/fakecustomtype/__init__.py
server/test/data-migractions/cubes/fakecustomtype/__pkginfo__.py
server/test/data-migractions/cubes/fakecustomtype/schema.py
server/test/data-migractions/cubes/fakecustomtype/site_cubicweb.py
server/test/data-migractions/cubes/fakegis/__init__.py
server/test/data-migractions/cubes/fakegis/__pkginfo__.py
server/test/data-migractions/cubes/fakegis/schema.py
server/test/data-migractions/cubes/fakegis/site_cubicweb.py
server/test/unittest_migractions.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/test/data-migractions/cubes/fakecustomtype/__pkginfo__.py	Wed Dec 09 17:44:18 2015 +0100
@@ -0,0 +1,50 @@
+# pylint: disable-msg=W0622
+"""cubicweb-fakeemail packaging information"""
+
+modname = 'fakecustomtype'
+distname = "cubicweb-%s" % modname
+
+numversion = (1, 0, 0)
+version = '.'.join(str(num) for num in numversion)
+
+license = 'LGPL'
+author = "Logilab"
+author_email = "contact@logilab.fr"
+web = 'http://www.cubicweb.org/project/%s' % distname
+description = "whatever"
+classifiers = [
+           'Environment :: Web Environment',
+           'Framework :: CubicWeb',
+           'Programming Language :: Python',
+           'Programming Language :: JavaScript',
+]
+
+# used packages
+__depends__ = {'cubicweb': '>= 3.19.0',
+               }
+
+
+# packaging ###
+
+from os import listdir as _listdir
+from os.path import join, isdir
+from glob import glob
+
+THIS_CUBE_DIR = join('share', 'cubicweb', 'cubes', modname)
+
+def listdir(dirpath):
+    return [join(dirpath, fname) for fname in _listdir(dirpath)
+            if fname[0] != '.' and not fname.endswith('.pyc')
+            and not fname.endswith('~')
+            and not isdir(join(dirpath, fname))]
+
+data_files = [
+    # common files
+    [THIS_CUBE_DIR, [fname for fname in glob('*.py') if fname != 'setup.py']],
+    ]
+# check for possible extended cube layout
+for dirname in ('entities', 'views', 'sobjects', 'hooks', 'schema', 'data', 'i18n', 'migration', 'wdoc'):
+    if isdir(dirname):
+        data_files.append([join(THIS_CUBE_DIR, dirname), listdir(dirname)])
+# Note: here, you'll need to add subdirectories if you want
+# them to be included in the debian package
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/test/data-migractions/cubes/fakecustomtype/schema.py	Wed Dec 09 17:44:18 2015 +0100
@@ -0,0 +1,7 @@
+
+from yams.buildobjs import EntityType, make_type
+
+Numeric = make_type('Numeric')
+
+class Location(EntityType):
+    num = Numeric(scale=10, precision=18)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/test/data-migractions/cubes/fakecustomtype/site_cubicweb.py	Wed Dec 09 17:44:18 2015 +0100
@@ -0,0 +1,17 @@
+from yams import register_base_type
+from logilab.database import get_db_helper
+from logilab.database.sqlgen import SQLExpression
+
+_NUMERIC_PARAMETERS = {'scale': 0, 'precision': None}
+register_base_type('Numeric', _NUMERIC_PARAMETERS)
+
+# Add the datatype to the helper mapping
+pghelper = get_db_helper('postgres')
+
+
+def pg_numeric_sqltype(rdef):
+    """Return a PostgreSQL column type corresponding to rdef
+    """
+    return 'numeric(%s, %s)' % (rdef.precision, rdef.scale)
+
+pghelper.TYPE_MAPPING['Numeric'] = pg_numeric_sqltype
--- a/server/test/data-migractions/cubes/fakegis/__pkginfo__.py	Wed Dec 09 17:44:18 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,50 +0,0 @@
-# pylint: disable-msg=W0622
-"""cubicweb-fakeemail packaging information"""
-
-modname = 'fakegis'
-distname = "cubicweb-%s" % modname
-
-numversion = (1, 0, 0)
-version = '.'.join(str(num) for num in numversion)
-
-license = 'LGPL'
-author = "Logilab"
-author_email = "contact@logilab.fr"
-web = 'http://www.cubicweb.org/project/%s' % distname
-description = "whatever"
-classifiers = [
-           'Environment :: Web Environment',
-           'Framework :: CubicWeb',
-           'Programming Language :: Python',
-           'Programming Language :: JavaScript',
-]
-
-# used packages
-__depends__ = {'cubicweb': '>= 3.19.0',
-               }
-
-
-# packaging ###
-
-from os import listdir as _listdir
-from os.path import join, isdir
-from glob import glob
-
-THIS_CUBE_DIR = join('share', 'cubicweb', 'cubes', modname)
-
-def listdir(dirpath):
-    return [join(dirpath, fname) for fname in _listdir(dirpath)
-            if fname[0] != '.' and not fname.endswith('.pyc')
-            and not fname.endswith('~')
-            and not isdir(join(dirpath, fname))]
-
-data_files = [
-    # common files
-    [THIS_CUBE_DIR, [fname for fname in glob('*.py') if fname != 'setup.py']],
-    ]
-# check for possible extended cube layout
-for dirname in ('entities', 'views', 'sobjects', 'hooks', 'schema', 'data', 'i18n', 'migration', 'wdoc'):
-    if isdir(dirname):
-        data_files.append([join(THIS_CUBE_DIR, dirname), listdir(dirname)])
-# Note: here, you'll need to add subdirectories if you want
-# them to be included in the debian package
--- a/server/test/data-migractions/cubes/fakegis/schema.py	Wed Dec 09 17:44:18 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-
-from yams.buildobjs import EntityType, make_type
-
-Geometry = make_type('Geometry')
-
-class Location(EntityType):
-    geometry = Geometry(
-        geom_type='GEOMETRYCOLLECTION', srid=4326, coord_dimension=2,
-        description='Geospatial indication of the location')
--- a/server/test/data-migractions/cubes/fakegis/site_cubicweb.py	Wed Dec 09 17:44:18 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,39 +0,0 @@
-from yams import register_base_type
-from logilab.database import get_db_helper
-from logilab.database.sqlgen import SQLExpression
-
-_GEOM_PARAMETERS = ('srid', 'geom_type', 'coord_dimension')
-register_base_type('Geometry', _GEOM_PARAMETERS)
-
-# Add the datatype to the helper mapping
-pghelper = get_db_helper('postgres')
-
-
-def pg_geometry_sqltype(rdef):
-    """Return a PostgreSQL column type corresponding to rdef's geom_type, srid
-    and coord_dimension.
-    """
-    target_geom_type = rdef.geom_type
-    if rdef.coord_dimension >= 3:  # XXX: handle 2D+M
-        target_geom_type += 'Z'
-    if rdef.coord_dimension == 4:
-        target_geom_type += 'M'
-    assert target_geom_type
-    assert rdef.srid
-    return 'geometry(%s, %s)' % (target_geom_type, rdef.srid)
-
-
-pghelper.TYPE_MAPPING['Geometry'] = pg_geometry_sqltype
-
-
-# Add a converter for Geometry
-def convert_geom(x):
-    if isinstance(x, (tuple, list)):
-        # We give the (Geometry, SRID)
-        return SQLExpression('ST_GeomFromText(%(geo)s, %(srid)s)', geo=x[0], srid=x[1])
-    else:
-        # We just give the Geometry
-        return SQLExpression('ST_GeomFromText(%(geo)s, %(srid)s)', geo=x, srid=-1)
-
-# Add the converter function to the known SQL_CONVERTERS
-pghelper.TYPE_CONVERTERS['Geometry'] = convert_geom
--- a/server/test/unittest_migractions.py	Wed Dec 09 17:44:18 2015 +0100
+++ b/server/test/unittest_migractions.py	Wed Dec 09 17:44:18 2015 +0100
@@ -291,16 +291,14 @@
 
     def test_add_cube_with_custom_final_type(self):
         with self.mh() as (cnx, mh):
-            mh.cmd_add_cube('fakegis')
-            self.assertIn('Geometry', self.schema)
-            self.assertTrue(self.schema['Geometry'].final)
-            rdef = self.schema['geometry'].rdefs[('Location', 'Geometry')]
-            self.assertEqual(rdef.geom_type, 'GEOMETRYCOLLECTION')
-            self.assertEqual(rdef.coord_dimension, 2)
-            self.assertEqual(rdef.srid, 4326)
-            #self.assertEqual(mh.sqlexec('SELECT pg_typeof("cw_geometry") FROM cw_Location'), '')
+            mh.cmd_add_cube('fakecustomtype')
+            self.assertIn('Numeric', self.schema)
+            self.assertTrue(self.schema['Numeric'].final)
+            rdef = self.schema['num'].rdefs[('Location', 'Numeric')]
+            self.assertEqual(rdef.scale, 10)
+            self.assertEqual(rdef.precision, 18)
             fields = self.table_schema(mh, '%sLocation' % SQL_PREFIX)
-            self.assertEqual(fields['%sgeometry' % SQL_PREFIX], ('USER-DEFINED', None)) # XXX
+            self.assertEqual(fields['%snum' % SQL_PREFIX], ('numeric', None)) # XXX
 
     def test_add_drop_entity_type(self):
         with self.mh() as (cnx, mh):