Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-66-Convert-custom-parser-fields-into-sqla-based-fields c0fdc03f0 -> d05373b0e (forced update)
wip Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/d05373b0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/d05373b0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/d05373b0 Branch: refs/heads/ARIA-66-Convert-custom-parser-fields-into-sqla-based-fields Commit: d05373b0e101040e86609dcf9fd4409ff77b9a7e Parents: 64a393e Author: mxmrlv <mxm...@gmail.com> Authored: Mon Jan 30 17:05:14 2017 +0200 Committer: mxmrlv <mxm...@gmail.com> Committed: Mon Jan 30 17:37:47 2017 +0200 ---------------------------------------------------------------------- aria/storage/type.py | 125 +++++++++++++++++----------------- tests/storage/__init__.py | 19 +++--- tests/storage/test_structures.py | 26 +++---- 3 files changed, 85 insertions(+), 85 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/d05373b0/aria/storage/type.py ---------------------------------------------------------------------- diff --git a/aria/storage/type.py b/aria/storage/type.py index 1b28f33..6c8f241 100644 --- a/aria/storage/type.py +++ b/aria/storage/type.py @@ -12,6 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + import json from collections import namedtuple from types import NoneType @@ -21,10 +22,9 @@ from sqlalchemy import ( VARCHAR, event ) - from sqlalchemy.ext import mutable -from . import exceptions +from aria.storage import exceptions class _MutableType(TypeDecorator): @@ -64,10 +64,6 @@ class List(_MutableType): class _StrictDictMixin(object): - ANY_TYPE = 'any_type' - - _key_cls = ANY_TYPE - _value_cls = ANY_TYPE @classmethod def coerce(cls, key, value): @@ -95,26 +91,22 @@ class _StrictDictMixin(object): self._assert_strict_value(value) super(_StrictDictMixin, self).setdefault(key, value) - def update(self, *a, **kw): - if isinstance(a, dict): - for k, v in a.items(): - self._assert_strict_key(k) - self._assert_strict_value(v) - for k, v in kw.items(): + def update(self, *args, **kwargs): + for k, v in kwargs.items(): self._assert_strict_key(k) self._assert_strict_value(v) - super(_StrictDictMixin, self).update(*a, **kw) + super(_StrictDictMixin, self).update(*args, **kwargs) @classmethod def _assert_strict_key(cls, key): - if not isinstance(key, cls._key_cls): + if cls._key_cls is not None and not isinstance(key, cls._key_cls): raise exceptions.StorageError("Key type was set strictly to {0}, but was {1}".format( cls._key_cls, type(key) )) @classmethod def _assert_strict_value(cls, value): - if not isinstance(value, cls._value_cls): + if cls._value_cls is not None and not isinstance(value, cls._value_cls): raise exceptions.StorageError("Value type was set strictly to {0}, but was {1}".format( cls._value_cls, type(value) )) @@ -159,21 +151,21 @@ class _StrictListMixin(object): self._assert_item(value) super(_StrictListMixin, self).__setitem__(index, value) - def append(self, x): - self._assert_item(x) - super(_StrictListMixin, self).append(x) + def append(self, item): + self._assert_item(item) + super(_StrictListMixin, self).append(item) - def extend(self, x): - self._assert_item(x) - super(_StrictListMixin, self).extend(x) + def extend(self, item): + self._assert_item(item) + super(_StrictListMixin, self).extend(item) - def insert(self, i, x): - self._assert_item(x) - super(_StrictListMixin, self).insert(i, x) + def insert(self, index, item): + self._assert_item(item) + super(_StrictListMixin, self).insert(index, item) @classmethod def _assert_item(cls, item): - if not isinstance(item, cls._item_cls): + if cls._item_cls is not None and not isinstance(item, cls._item_cls): raise exceptions.StorageError("Key type was set strictly to {0}, but was {1}".format( cls._item_cls, type(item) )) @@ -197,43 +189,52 @@ class _MutableList(mutable.MutableList): except ValueError as e: raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e))) -_strict_dict_id = namedtuple('strict_dict_id', 'key_cls, value_cls') -_strict_map = {} - - -def StrictDict(key_cls, value_cls): - strict_dict_map_key = _strict_dict_id(key_cls=key_cls, value_cls=value_cls) - if strict_dict_map_key not in _strict_map: - strict_dict_cls = type( - 'StrictDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), - (Dict, ), - {} - ) - type( - 'StrictMutableDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), - (_StrictDictMixin, _MutableDict), - {'_key_cls': key_cls, '_value_cls': value_cls} - ).associate_with(strict_dict_cls) - _strict_map[strict_dict_map_key] = strict_dict_cls - - return _strict_map[strict_dict_map_key] - - -def StrictList(item_cls): - if item_cls not in _strict_map: - strict_list_cls = type( - 'StrictList_{0}'.format(item_cls.__name__), - (List, ), - {} - ) - type( - 'StrictMutableList_{0}'.format(item_cls.__name__), - (_StrictListMixin, _MutableList), - {'_item_cls': item_cls} - ).associate_with(strict_list_cls) - _strict_map[item_cls] = strict_list_cls - - return _strict_map[item_cls] +StrictDictID = namedtuple('strict_dict_id', 'key_cls, value_cls') + + +class _StrictDict(object): + _strict_map = {} + + def __call__(self, key_cls=None, value_cls=None, *args, **kwargs): + strict_dict_map_key = StrictDictID(key_cls=key_cls, value_cls=value_cls) + if strict_dict_map_key not in self._strict_map: + strict_dict_cls = type( + 'StrictDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), + (Dict, ), + {} + ) + type( + 'StrictMutableDict_{0}_{1}'.format(key_cls.__name__, value_cls.__name__), + (_StrictDictMixin, _MutableDict), + {'_key_cls': key_cls, '_value_cls': value_cls} + ).associate_with(strict_dict_cls) + self._strict_map[strict_dict_map_key] = strict_dict_cls + + return self._strict_map[strict_dict_map_key] + +StrictDict = _StrictDict() + + +class _StrictList(object): + _strict_map = {} + + def __call__(self, item_cls=None): + if item_cls not in self._strict_map: + strict_list_cls = type( + 'StrictList_{0}'.format(item_cls.__name__), + (List, ), + {} + ) + type( + 'StrictMutableList_{0}'.format(item_cls.__name__), + (_StrictListMixin, _MutableList), + {'_item_cls': item_cls} + ).associate_with(strict_list_cls) + self._strict_map[item_cls] = strict_list_cls + + return self._strict_map[item_cls] + +StrictList = _StrictList() def _mutable_association_listener(mapper, cls): for prop in mapper.column_attrs: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/d05373b0/tests/storage/__init__.py ---------------------------------------------------------------------- diff --git a/tests/storage/__init__.py b/tests/storage/__init__.py index c772acb..3b3715e 100644 --- a/tests/storage/__init__.py +++ b/tests/storage/__init__.py @@ -17,18 +17,21 @@ import platform from tempfile import mkdtemp from shutil import rmtree -from sqlalchemy import Column, Text, Integer +from sqlalchemy import ( + create_engine, + orm, + Column, + Text, + Integer, + pool +) + from aria.storage import ( model, structure, type as aria_type, ) -from sqlalchemy import ( - create_engine, - orm) -from sqlalchemy.orm import scoped_session -from sqlalchemy.pool import StaticPool class MockModel(model.DeclarativeBase, structure.ModelMixin): #pylint: disable=abstract-method @@ -68,11 +71,11 @@ def get_sqlite_api_kwargs(base_dir=None, filename='db.sqlite'): else: uri = 'sqlite:///:memory:' engine_kwargs = dict(connect_args={'check_same_thread': False}, - poolclass=StaticPool) + poolclass=pool.StaticPool) engine = create_engine(uri, **engine_kwargs) session_factory = orm.sessionmaker(bind=engine) - session = scoped_session(session_factory=session_factory) if base_dir else session_factory() + session = orm.scoped_session(session_factory=session_factory) if base_dir else session_factory() model.DeclarativeBase.metadata.create_all(bind=engine) return dict(engine=engine, session=session) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/d05373b0/tests/storage/test_structures.py ---------------------------------------------------------------------- diff --git a/tests/storage/test_structures.py b/tests/storage/test_structures.py index e6d2093..7e34b17 100644 --- a/tests/storage/test_structures.py +++ b/tests/storage/test_structures.py @@ -226,7 +226,7 @@ def test_relationship_model_ordering(context): class StrictClass(model.DeclarativeBase, structure.ModelMixin): - __tablename__ = 'class_with_strict_dict' + __tablename__ = 'strict_class' strict_dict = sqlalchemy.Column(type.StrictDict(basestring, basestring)) strict_list = sqlalchemy.Column(type.StrictList(basestring)) @@ -236,39 +236,35 @@ def test_strict_dict(): strict_class = StrictClass() - def assert_strict(sd, with_assigment=False): - if with_assigment: + def assert_strict(sd, after_assignment=False): + if after_assignment: with pytest.raises(exceptions.StorageError): sd.strict_dict['key'] = None + with pytest.raises(exceptions.StorageError): + sd.strict_dict[None] = 'value' + with pytest.raises(exceptions.StorageError): + sd.strict_dict[None] = None with pytest.raises(exceptions.StorageError): sd.strict_dict = {'key': None} - if with_assigment: - with pytest.raises(exceptions.StorageError): - sd.strict_dict[None] = 'value' - with pytest.raises(exceptions.StorageError): sd.strict_dict = {None: 'value'} - if with_assigment: - with pytest.raises(exceptions.StorageError): - sd.strict_dict[None] = None - with pytest.raises(exceptions.StorageError): sd.strict_dict = {None: None} assert_strict(strict_class) strict_class.strict_dict = {'key': 'value'} assert strict_class.strict_dict == {'key': 'value'} - assert_strict(strict_class, with_assigment=True) + assert_strict(strict_class, after_assignment=True) def test_strict_list(): strict_class = StrictClass() - def assert_strict(sd, with_assigment=False): - if with_assigment: + def assert_strict(sd, after_assignment=False): + if after_assignment: with pytest.raises(exceptions.StorageError): sd.strict_list[0] = None @@ -278,4 +274,4 @@ def test_strict_list(): assert_strict(strict_class) strict_class.strict_list = ['item'] assert strict_class.strict_list == ['item'] - assert_strict(strict_class, with_assigment=True) + assert_strict(strict_class, after_assignment=True)