Drop deprecated code in cubicweb.dataimport
We actually drop *most* of deprecated code in this subpackage. What's
left is the MetaGenerator class in (cubicweb/dataimport/stores.py) which
is tricky to remove as it's still internally used by cubicweb.
Changelog entry will come in a later changeset.
--- a/cubicweb/dataimport/__init__.py Thu Mar 14 14:43:18 2019 +0100
+++ b/cubicweb/dataimport/__init__.py Thu Mar 14 14:45:03 2019 +0100
@@ -32,4 +32,3 @@
from cubicweb.dataimport.stores import *
from cubicweb.dataimport.pgstore import *
from cubicweb.dataimport.csv import *
-from cubicweb.dataimport.deprecated import *
--- a/cubicweb/dataimport/csv.py Thu Mar 14 14:43:18 2019 +0100
+++ b/cubicweb/dataimport/csv.py Thu Mar 14 14:45:03 2019 +0100
@@ -20,7 +20,6 @@
import codecs
import csv as csvmod
-import warnings
from six import PY2, PY3, string_types
@@ -41,15 +40,8 @@
def ucsvreader_pb(stream_or_path, encoding='utf-8', delimiter=',', quotechar='"',
- skipfirst=False, withpb=True, skip_empty=True, separator=None,
- quote=None):
+ skipfirst=False, withpb=True, skip_empty=True):
"""same as :func:`ucsvreader` but a progress bar is displayed as we iter on rows"""
- if separator is not None:
- delimiter = separator
- warnings.warn("[3.20] 'separator' kwarg is deprecated, use 'delimiter' instead")
- if quote is not None:
- quotechar = quote
- warnings.warn("[3.20] 'quote' kwarg is deprecated, use 'quotechar' instead")
if isinstance(stream_or_path, string_types):
stream = open(stream_or_path, 'rb')
else:
@@ -68,8 +60,7 @@
def ucsvreader(stream, encoding='utf-8', delimiter=',', quotechar='"',
- skipfirst=False, ignore_errors=False, skip_empty=True,
- separator=None, quote=None):
+ skipfirst=False, ignore_errors=False, skip_empty=True):
"""A csv reader that accepts files with any encoding and outputs unicode
strings
@@ -79,12 +70,6 @@
"""
if PY3:
stream = codecs.getreader(encoding)(stream)
- if separator is not None:
- delimiter = separator
- warnings.warn("[3.20] 'separator' kwarg is deprecated, use 'delimiter' instead")
- if quote is not None:
- quotechar = quote
- warnings.warn("[3.20] 'quote' kwarg is deprecated, use 'quotechar' instead")
it = iter(csvmod.reader(stream, delimiter=delimiter, quotechar=quotechar))
if not ignore_errors:
if skipfirst:
--- a/cubicweb/dataimport/deprecated.py Thu Mar 14 14:43:18 2019 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,463 +0,0 @@
-# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
-# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
-#
-# This file is part of CubicWeb.
-#
-# CubicWeb is free software: you can redistribute it and/or modify it under the
-# terms of the GNU Lesser General Public License as published by the Free
-# Software Foundation, either version 2.1 of the License, or (at your option)
-# any later version.
-#
-# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
-# details.
-#
-# You should have received a copy of the GNU Lesser General Public License along
-# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
-"""Old and deprecated dataimport API that provides tools to import tabular data.
-
-
-Example of use (run this with `cubicweb-ctl shell instance import-script.py`):
-
-.. sourcecode:: python
-
- from cubicweb.dataimport import *
- # define data generators
- GENERATORS = []
-
- USERS = [('Prenom', 'firstname', ()),
- ('Nom', 'surname', ()),
- ('Identifiant', 'login', ()),
- ]
-
- def gen_users(ctl):
- for row in ctl.iter_and_commit('utilisateurs'):
- entity = mk_entity(row, USERS)
- entity['upassword'] = 'motdepasse'
- ctl.check('login', entity['login'], None)
- entity = ctl.store.prepare_insert_entity('CWUser', **entity)
- email = ctl.store.prepare_insert_entity('EmailAddress', address=row['email'])
- ctl.store.prepare_insert_relation(entity, 'use_email', email)
- ctl.store.rql('SET U in_group G WHERE G name "users", U eid %(x)s', {'x': entity})
-
- CHK = [('login', check_doubles, 'Utilisateurs Login',
- 'Deux utilisateurs ne devraient pas avoir le meme login.'),
- ]
-
- GENERATORS.append( (gen_users, CHK) )
-
- # create controller
- ctl = CWImportController(RQLObjectStore(cnx))
- ctl.askerror = 1
- ctl.generators = GENERATORS
- ctl.data['utilisateurs'] = lazytable(ucsvreader(open('users.csv')))
- # run
- ctl.run()
-
-.. BUG file with one column are not parsable
-.. TODO rollback() invocation is not possible yet
-"""
-from __future__ import print_function
-
-import sys
-import traceback
-from io import StringIO
-
-from six import add_metaclass
-
-from logilab.common import attrdict, shellutils
-from logilab.common.date import strptime
-from logilab.common.deprecation import deprecated, class_deprecated
-
-from cubicweb import QueryError
-from cubicweb.dataimport import callfunc_every
-
-
-@deprecated('[3.21] deprecated')
-def lazytable(reader):
- """The first row is taken to be the header of the table and
- used to output a dict for each row of data.
-
- >>> data = lazytable(ucsvreader(open(filename)))
- """
- header = next(reader)
- for row in reader:
- yield dict(zip(header, row))
-
-
-@deprecated('[3.21] deprecated')
-def lazydbtable(cu, table, headers, orderby=None):
- """return an iterator on rows of a sql table. On each row, fetch columns
- defined in headers and return values as a dictionary.
-
- >>> data = lazydbtable(cu, 'experimentation', ('id', 'nickname', 'gps'))
- """
- sql = 'SELECT %s FROM %s' % (','.join(headers), table,)
- if orderby:
- sql += ' ORDER BY %s' % ','.join(orderby)
- cu.execute(sql)
- while True:
- row = cu.fetchone()
- if row is None:
- break
- yield dict(zip(headers, row))
-
-
-@deprecated('[3.21] deprecated')
-def tell(msg):
- print(msg)
-
-
-@deprecated('[3.21] deprecated')
-def confirm(question):
- """A confirm function that asks for yes/no/abort and exits on abort."""
- answer = shellutils.ASK.ask(question, ('Y', 'n', 'abort'), 'Y')
- if answer == 'abort':
- sys.exit(1)
- return answer == 'Y'
-
-
-@add_metaclass(class_deprecated)
-class catch_error(object):
- """Helper for @contextmanager decorator."""
- __deprecation_warning__ = '[3.21] deprecated'
-
- def __init__(self, ctl, key='unexpected error', msg=None):
- self.ctl = ctl
- self.key = key
- self.msg = msg
-
- def __enter__(self):
- return self
-
- def __exit__(self, type, value, traceback):
- if type is not None:
- if issubclass(type, (KeyboardInterrupt, SystemExit)):
- return # re-raise
- if self.ctl.catcherrors:
- self.ctl.record_error(self.key, None, type, value, traceback)
- return True # silent
-
-@deprecated('[3.21] deprecated')
-def mk_entity(row, map):
- """Return a dict made from sanitized mapped values.
-
- ValueError can be raised on unexpected values found in checkers
-
- >>> row = {'myname': u'dupont'}
- >>> map = [('myname', u'name', (call_transform_method('title'),))]
- >>> mk_entity(row, map)
- {'name': u'Dupont'}
- >>> row = {'myname': u'dupont', 'optname': u''}
- >>> map = [('myname', u'name', (call_transform_method('title'),)),
- ... ('optname', u'MARKER', (optional,))]
- >>> mk_entity(row, map)
- {'name': u'Dupont', 'optname': None}
- """
- res = {}
- assert isinstance(row, dict)
- assert isinstance(map, list)
- for src, dest, funcs in map:
- try:
- res[dest] = row[src]
- except KeyError:
- continue
- try:
- for func in funcs:
- res[dest] = func(res[dest])
- if res[dest] is None:
- break
- except ValueError as err:
- exc = ValueError('error with %r field: %s' % (src, err))
- exc.__traceback__ = sys.exc_info()[-1]
- raise exc
- return res
-
-
-# base sanitizing/coercing functions ###########################################
-
-@deprecated('[3.21] deprecated')
-def optional(value):
- """checker to filter optional field
-
- If value is undefined (ex: empty string), return None that will
- break the checkers validation chain
-
- General use is to add 'optional' check in first condition to avoid
- ValueError by further checkers
-
- >>> MAPPER = [(u'value', 'value', (optional, int))]
- >>> row = {'value': u'XXX'}
- >>> mk_entity(row, MAPPER)
- {'value': None}
- >>> row = {'value': u'100'}
- >>> mk_entity(row, MAPPER)
- {'value': 100}
- """
- if value:
- return value
- return None
-
-
-@deprecated('[3.21] deprecated')
-def required(value):
- """raise ValueError if value is empty
-
- This check should be often found in last position in the chain.
- """
- if value:
- return value
- raise ValueError("required")
-
-
-@deprecated('[3.21] deprecated')
-def todatetime(format='%d/%m/%Y'):
- """return a transformation function to turn string input value into a
- `datetime.datetime` instance, using given format.
-
- Follow it by `todate` or `totime` functions from `logilab.common.date` if
- you want a `date`/`time` instance instead of `datetime`.
- """
- def coerce(value):
- return strptime(value, format)
- return coerce
-
-
-@deprecated('[3.21] deprecated')
-def call_transform_method(methodname, *args, **kwargs):
- """return value returned by calling the given method on input"""
- def coerce(value):
- return getattr(value, methodname)(*args, **kwargs)
- return coerce
-
-
-@deprecated('[3.21] deprecated')
-def call_check_method(methodname, *args, **kwargs):
- """check value returned by calling the given method on input is true,
- else raise ValueError
- """
- def check(value):
- if getattr(value, methodname)(*args, **kwargs):
- return value
- raise ValueError('%s not verified on %r' % (methodname, value))
- return check
-
-
-# base integrity checking functions ############################################
-
-@deprecated('[3.21] deprecated')
-def check_doubles(buckets):
- """Extract the keys that have more than one item in their bucket."""
- return [(k, len(v)) for k, v in buckets.items() if len(v) > 1]
-
-
-@deprecated('[3.21] deprecated')
-def check_doubles_not_none(buckets):
- """Extract the keys that have more than one item in their bucket."""
- return [(k, len(v)) for k, v in buckets.items()
- if k is not None and len(v) > 1]
-
-
-@add_metaclass(class_deprecated)
-class ObjectStore(object):
- """Store objects in memory for *faster* validation (development mode)
-
- But it will not enforce the constraints of the schema and hence will miss some problems
-
- >>> store = ObjectStore()
- >>> user = store.prepare_insert_entity('CWUser', login=u'johndoe')
- >>> group = store.prepare_insert_entity('CWUser', name=u'unknown')
- >>> store.prepare_insert_relation(user, 'in_group', group)
- """
- __deprecation_warning__ = '[3.21] use the new importer API'
-
- def __init__(self):
- self.items = []
- self.eids = {}
- self.types = {}
- self.relations = set()
- self.indexes = {}
-
- def prepare_insert_entity(self, etype, **data):
- """Given an entity type, attributes and inlined relations, return an eid for the entity that
- would be inserted with a real store.
- """
- data = attrdict(data)
- data['eid'] = eid = len(self.items)
- self.items.append(data)
- self.eids[eid] = data
- self.types.setdefault(etype, []).append(eid)
- return eid
-
- def prepare_update_entity(self, etype, eid, **kwargs):
- """Given an entity type and eid, updates the corresponding fake entity with specified
- attributes and inlined relations.
- """
- assert eid in self.types[etype], 'Trying to update with wrong type %s' % etype
- data = self.eids[eid]
- data.update(kwargs)
-
- def prepare_insert_relation(self, eid_from, rtype, eid_to, **kwargs):
- """Store into the `relations` attribute that a relation ``rtype`` exists between entities
- with eids ``eid_from`` and ``eid_to``.
- """
- relation = eid_from, rtype, eid_to
- self.relations.add(relation)
- return relation
-
- def flush(self):
- """Nothing to flush for this store."""
- pass
-
- def commit(self):
- """Nothing to commit for this store."""
- return
-
- def finish(self):
- """Nothing to do once import is terminated for this store."""
- pass
-
- @property
- def nb_inserted_entities(self):
- return len(self.eids)
-
- @property
- def nb_inserted_types(self):
- return len(self.types)
-
- @property
- def nb_inserted_relations(self):
- return len(self.relations)
-
- @deprecated('[3.21] use prepare_insert_entity instead')
- def create_entity(self, etype, **data):
- self.prepare_insert_entity(etype, **data)
- return attrdict(data)
-
- @deprecated('[3.21] use prepare_insert_relation instead')
- def relate(self, eid_from, rtype, eid_to, **kwargs):
- self.prepare_insert_relation(eid_from, rtype, eid_to, **kwargs)
-
-
-@add_metaclass(class_deprecated)
-class CWImportController(object):
- """Controller of the data import process.
-
- >>> ctl = CWImportController(store)
- >>> ctl.generators = list_of_data_generators
- >>> ctl.data = dict_of_data_tables
- >>> ctl.run()
- """
- __deprecation_warning__ = '[3.21] use the new importer API'
-
- def __init__(self, store, askerror=0, catcherrors=None, tell=tell,
- commitevery=50):
- self.store = store
- self.generators = None
- self.data = {}
- self.errors = None
- self.askerror = askerror
- if catcherrors is None:
- catcherrors = askerror
- self.catcherrors = catcherrors
- self.commitevery = commitevery # set to None to do a single commit
- self._tell = tell
-
- def check(self, type, key, value):
- self._checks.setdefault(type, {}).setdefault(key, []).append(value)
-
- def check_map(self, entity, key, map, default):
- try:
- entity[key] = map[entity[key]]
- except KeyError:
- self.check(key, entity[key], None)
- entity[key] = default
-
- def record_error(self, key, msg=None, type=None, value=None, tb=None):
- tmp = StringIO()
- if type is None:
- traceback.print_exc(file=tmp)
- else:
- traceback.print_exception(type, value, tb, file=tmp)
- # use a list to avoid counting a <nb lines> errors instead of one
- errorlog = self.errors.setdefault(key, [])
- if msg is None:
- errorlog.append(tmp.getvalue().splitlines())
- else:
- errorlog.append( (msg, tmp.getvalue().splitlines()) )
-
- def run(self):
- self.errors = {}
- if self.commitevery is None:
- self.tell('Will commit all or nothing.')
- else:
- self.tell('Will commit every %s iterations' % self.commitevery)
- for func, checks in self.generators:
- self._checks = {}
- func_name = func.__name__
- self.tell("Run import function '%s'..." % func_name)
- try:
- func(self)
- except Exception:
- if self.catcherrors:
- self.record_error(func_name, 'While calling %s' % func.__name__)
- else:
- self._print_stats()
- raise
- for key, func, title, help in checks:
- buckets = self._checks.get(key)
- if buckets:
- err = func(buckets)
- if err:
- self.errors[title] = (help, err)
- try:
- txuuid = self.store.commit()
- if txuuid is not None:
- self.tell('Transaction commited (txuuid: %s)' % txuuid)
- except QueryError as ex:
- self.tell('Transaction aborted: %s' % ex)
- self._print_stats()
- if self.errors:
- if self.askerror == 2 or (self.askerror and confirm('Display errors ?')):
- from pprint import pformat
- for errkey, error in self.errors.items():
- self.tell("\n%s (%s): %d\n" % (error[0], errkey, len(error[1])))
- self.tell(pformat(sorted(error[1])))
-
- def _print_stats(self):
- nberrors = sum(len(err) for err in self.errors.values())
- self.tell('\nImport statistics: %i entities, %i types, %i relations and %i errors'
- % (self.store.nb_inserted_entities,
- self.store.nb_inserted_types,
- self.store.nb_inserted_relations,
- nberrors))
-
- def get_data(self, key):
- return self.data.get(key)
-
- def index(self, name, key, value, unique=False):
- """create a new index
-
- If unique is set to True, only first occurence will be kept not the following ones
- """
- if unique:
- try:
- if value in self.store.indexes[name][key]:
- return
- except KeyError:
- # we're sure that one is the first occurence; so continue...
- pass
- self.store.indexes.setdefault(name, {}).setdefault(key, []).append(value)
-
- def tell(self, msg):
- self._tell(msg)
-
- def iter_and_commit(self, datakey):
- """iter rows, triggering commit every self.commitevery iterations"""
- if self.commitevery is None:
- return self.get_data(datakey)
- else:
- return callfunc_every(self.store.commit,
- self.commitevery,
- self.get_data(datakey))
--- a/cubicweb/dataimport/pgstore.py Thu Mar 14 14:43:18 2019 +0100
+++ b/cubicweb/dataimport/pgstore.py Thu Mar 14 14:45:03 2019 +0100
@@ -29,8 +29,6 @@
from six import string_types, integer_types, text_type, add_metaclass
from six.moves import cPickle as pickle, range
-from logilab.common.deprecation import class_deprecated
-
from cubicweb.utils import make_uid
from cubicweb.server.sqlutils import SQL_PREFIX
from cubicweb.dataimport.stores import NoHookRQLObjectStore
@@ -198,75 +196,6 @@
return StringIO('\n'.join(rows))
-@add_metaclass(class_deprecated)
-class SQLGenObjectStore(NoHookRQLObjectStore):
- """Controller of the data import process. This version is based
- on direct insertions throught SQL command (COPY FROM or execute many).
-
- >>> store = SQLGenObjectStore(cnx)
- >>> store.create_entity('Person', ...)
- >>> store.flush()
- """
- __deprecation_warning__ = '[3.23] this class is deprecated, use MassiveObjectStore instead'
-
- def __init__(self, cnx, dump_output_dir=None, nb_threads_statement=1):
- """
- Initialize a SQLGenObjectStore.
-
- Parameters:
-
- - cnx: connection on the cubicweb instance
- - dump_output_dir: a directory to dump failed statements
- for easier recovery. Default is None (no dump).
- """
- super(SQLGenObjectStore, self).__init__(cnx)
- ### hijack default source
- self._system_source = SQLGenSourceWrapper(
- self._system_source, cnx.vreg.schema,
- dump_output_dir=dump_output_dir)
- ### XXX This is done in super().__init__(), but should be
- ### redone here to link to the correct source
- self._add_relation = self._system_source.add_relation
- self.indexes_etypes = {}
- if nb_threads_statement != 1:
- warnings.warn('[3.21] SQLGenObjectStore is no longer threaded', DeprecationWarning)
-
- def flush(self):
- """Flush data to the database"""
- self._system_source.flush()
-
- def relate(self, subj_eid, rtype, obj_eid, **kwargs):
- if subj_eid is None or obj_eid is None:
- return
- # XXX Could subjtype be inferred ?
- self._add_relation(self._cnx, subj_eid, rtype, obj_eid,
- self.rschema(rtype).inlined, **kwargs)
- if self.rschema(rtype).symmetric:
- self._add_relation(self._cnx, obj_eid, rtype, subj_eid,
- self.rschema(rtype).inlined, **kwargs)
-
- def drop_indexes(self, etype):
- """Drop indexes for a given entity type"""
- if etype not in self.indexes_etypes:
- cu = self._cnx.cnxset.cu
- def index_to_attr(index):
- """turn an index name to (database) attribute name"""
- return index.replace(etype.lower(), '').replace('idx', '').strip('_')
- indices = [(index, index_to_attr(index))
- for index in self._system_source.dbhelper.list_indices(cu, etype)
- # Do not consider 'cw_etype_pkey' index
- if not index.endswith('key')]
- self.indexes_etypes[etype] = indices
- for index, attr in self.indexes_etypes[etype]:
- self._cnx.system_sql('DROP INDEX %s' % index)
-
- def create_indexes(self, etype):
- """Recreate indexes for a given entity type"""
- for index, attr in self.indexes_etypes.get(etype, []):
- sql = 'CREATE INDEX %s ON cw_%s(%s)' % (index, etype, attr)
- self._cnx.system_sql(sql)
-
-
###########################################################################
## SQL Source #############################################################
###########################################################################
--- a/cubicweb/dataimport/stores.py Thu Mar 14 14:43:18 2019 +0100
+++ b/cubicweb/dataimport/stores.py Thu Mar 14 14:45:03 2019 +0100
@@ -68,7 +68,7 @@
import pytz
from logilab.common.decorators import cached
-from logilab.common.deprecation import deprecated, class_deprecated
+from logilab.common.deprecation import class_deprecated
from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES
from cubicweb.server.edition import EditedEntity
@@ -149,23 +149,6 @@
def commit(self):
return self._commit()
- @deprecated("[3.19] use cnx.find(*args, **kwargs).entities() instead")
- def find_entities(self, *args, **kwargs):
- return self._cnx.find(*args, **kwargs).entities()
-
- @deprecated("[3.19] use cnx.find(*args, **kwargs).one() instead")
- def find_one_entity(self, *args, **kwargs):
- return self._cnx.find(*args, **kwargs).one()
-
- @deprecated('[3.21] use prepare_insert_entity instead')
- def create_entity(self, *args, **kwargs):
- eid = self.prepare_insert_entity(*args, **kwargs)
- return self._cnx.entity_from_eid(eid)
-
- @deprecated('[3.21] use prepare_insert_relation instead')
- def relate(self, eid_from, rtype, eid_to, **kwargs):
- self.prepare_insert_relation(eid_from, rtype, eid_to, **kwargs)
-
class NoHookRQLObjectStore(RQLObjectStore):
"""Store that works by accessing low-level CubicWeb's source API, with all hooks deactivated. It
@@ -241,21 +224,6 @@
self._add_relation(self._cnx, eid_to, rtype, eid_from, rschema.inlined)
self._nb_inserted_relations += 1
- @property
- @deprecated('[3.21] deprecated')
- def nb_inserted_entities(self):
- return self._nb_inserted_entities
-
- @property
- @deprecated('[3.21] deprecated')
- def nb_inserted_types(self):
- return self._nb_inserted_types
-
- @property
- @deprecated('[3.21] deprecated')
- def nb_inserted_relations(self):
- return self._nb_inserted_relations
-
class MetadataGenerator(object):
"""Class responsible for generating standard metadata for imported entities. You may want to
--- a/cubicweb/dataimport/test/test_pgstore.py Thu Mar 14 14:43:18 2019 +0100
+++ b/cubicweb/dataimport/test/test_pgstore.py Thu Mar 14 14:45:03 2019 +0100
@@ -24,7 +24,6 @@
from logilab.common.testlib import TestCase, unittest_main
from cubicweb.dataimport import pgstore
-from cubicweb.devtools import testlib
if PY3:
@@ -94,15 +93,5 @@
self.assertEqual(expected, results.getvalue())
-class SQLGenObjectStoreTC(testlib.CubicWebTC):
-
- def test_prepare_insert_entity(self):
- with self.admin_access.repo_cnx() as cnx:
- store = pgstore.SQLGenObjectStore(cnx)
- eid = store.prepare_insert_entity('CWUser', login=u'toto',
- upassword=u'pwd')
- self.assertIsNotNone(eid)
-
-
if __name__ == '__main__':
unittest_main()
--- a/cubicweb/dataimport/test/test_sqlgenstore.py Thu Mar 14 14:43:18 2019 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,122 +0,0 @@
-# -*- coding: utf-8 -*-
-# copyright 2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
-# contact http://www.logilab.fr -- mailto:contact@logilab.fr
-#
-# This program is free software: you can redistribute it and/or modify it under
-# the terms of the GNU Lesser General Public License as published by the Free
-# Software Foundation, either version 2.1 of the License, or (at your option)
-# any later version.
-#
-# This program is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
-# details.
-#
-# You should have received a copy of the GNU Lesser General Public License along
-# with this program. If not, see <http://www.gnu.org/licenses/>.
-"""SQL object store test case"""
-
-from cubicweb.dataimport import ucsvreader
-from cubicweb.devtools import testlib, PostgresApptestConfiguration
-from cubicweb.devtools import startpgcluster, stoppgcluster
-from cubicweb.dataimport.pgstore import SQLGenObjectStore
-
-
-def setUpModule():
- startpgcluster(__file__)
-
-
-def tearDownModule(*args):
- stoppgcluster(__file__)
-
-
-class SQLGenImportSimpleTC(testlib.CubicWebTC):
- configcls = PostgresApptestConfiguration
- appid = 'data-massimport'
-
- def cast(self, _type, value):
- try:
- return _type(value)
- except ValueError:
- return None
-
- def push_geonames_data(self, dumpname, store):
- # Push timezones
- cnx = store._cnx
- for code, gmt, dst, raw_offset in ucsvreader(open(self.datapath('timeZones.txt'), 'rb'),
- delimiter='\t'):
- cnx.create_entity('TimeZone', code=code, gmt=float(gmt),
- dst=float(dst), raw_offset=float(raw_offset))
- timezone_code = dict(cnx.execute('Any C, X WHERE X is TimeZone, X code C'))
- cnx.commit()
- # Push data
- for ind, infos in enumerate(ucsvreader(open(dumpname, 'rb'),
- delimiter='\t',
- ignore_errors=True)):
- if ind > 99:
- break
- latitude = self.cast(float, infos[4])
- longitude = self.cast(float, infos[5])
- population = self.cast(int, infos[14])
- elevation = self.cast(int, infos[15])
- gtopo = self.cast(int, infos[16])
- feature_class = infos[6]
- if len(infos[6]) != 1:
- feature_class = None
- entity = {'name': infos[1],
- 'asciiname': infos[2],
- 'alternatenames': infos[3],
- 'latitude': latitude, 'longitude': longitude,
- 'feature_class': feature_class,
- 'alternate_country_code': infos[9],
- 'admin_code_3': infos[12],
- 'admin_code_4': infos[13],
- 'population': population, 'elevation': elevation,
- 'gtopo30': gtopo, 'timezone': timezone_code.get(infos[17]),
- 'cwuri': u'http://sws.geonames.org/%s/' % int(infos[0]),
- 'geonameid': int(infos[0]),
- }
- store.prepare_insert_entity('Location', **entity)
-
- def test_autoflush_metadata(self):
- with self.admin_access.repo_cnx() as cnx:
- crs = cnx.system_sql('SELECT * FROM entities WHERE type=%(t)s',
- {'t': 'Location'})
- self.assertEqual(len(crs.fetchall()), 0)
- store = SQLGenObjectStore(cnx)
- store.prepare_insert_entity('Location', name=u'toto')
- store.flush()
- store.commit()
- cnx.commit()
- with self.admin_access.repo_cnx() as cnx:
- crs = cnx.system_sql('SELECT * FROM entities WHERE type=%(t)s',
- {'t': 'Location'})
- self.assertEqual(len(crs.fetchall()), 1)
-
- def test_sqlgenstore_etype_metadata(self):
- with self.admin_access.repo_cnx() as cnx:
- store = SQLGenObjectStore(cnx)
- timezone_eid = store.prepare_insert_entity('TimeZone', code=u'12')
- store.prepare_insert_entity('Location', timezone=timezone_eid)
- store.flush()
- store.commit()
- eid, etname = cnx.execute('Any X, TN WHERE X timezone TZ, X is T, '
- 'T name TN')[0]
- self.assertEqual(cnx.entity_from_eid(eid).cw_etype, etname)
-
- def test_simple_insert(self):
- with self.admin_access.repo_cnx() as cnx:
- store = SQLGenObjectStore(cnx)
- self.push_geonames_data(self.datapath('geonames.csv'), store)
- store.flush()
- store.commit()
- with self.admin_access.repo_cnx() as cnx:
- rset = cnx.execute('Any X WHERE X is Location')
- self.assertEqual(len(rset), 100)
- rset = cnx.execute('Any X WHERE X is Location, X timezone T')
- self.assertEqual(len(rset), 100)
-
-
-if __name__ == '__main__':
- import unittest
- unittest.main()
--- a/flake8-ok-files.txt Thu Mar 14 14:43:18 2019 +0100
+++ b/flake8-ok-files.txt Thu Mar 14 14:45:03 2019 +0100
@@ -9,7 +9,6 @@
cubicweb/dataimport/test/test_csv.py
cubicweb/dataimport/test/test_pgstore.py
cubicweb/dataimport/test/test_massive_store.py
-cubicweb/dataimport/test/test_sqlgenstore.py
cubicweb/dataimport/test/test_stores.py
cubicweb/dataimport/test/unittest_importer.py
cubicweb/devtools/test/data/cubes/i18ntestcube/__init__.py