def test_load_many_to_one_self_refencing(self):
        User = self.classes.User
        session = fixture_session()

        users = (
            session.query(User)
                .filter(self.tables.users.c.id.in_([8, 9, 10]))
                .order_by(self.tables.users.c.id.asc())
                .all()
        )
        self.queries = []

        # make sure no relations are loaded
        for user in users:
            model_dict = attributes.instance_dict(user)
            assert "parent" not in model_dict

        # trigger a lazy load on the first user
        users[0].parent

        # only 1 query should have been generated to load all the child relationships
        assert len(self.queries) == 1
        self.queries = []

        user1_dict = attributes.instance_dict(users[0])
        user2_dict = attributes.instance_dict(users[1])
        user3_dict = attributes.instance_dict(users[2])

        assert User(id=7, name="jack", parent_id=None) == user1_dict["parent"]
        assert User(id=7, name="jack", parent_id=None) == user2_dict["parent"]
        assert User(id=8, name="jack jr", parent_id=7) == user3_dict["parent"]

        # no new queries should have been generated
        assert len(self.queries) == 0
示例#2
0
    def test_deferred(self):
        for base in (object, MyBaseClass, MyClass):

            class Foo(base):
                pass

            data = {'a': 'this is a', 'b': 12}

            def loader(state, keys):
                for k in keys:
                    state.dict[k] = data[k]
                return attributes.ATTR_WAS_SET

            manager = register_class(Foo)
            manager.deferred_scalar_loader = loader
            attributes.register_attribute(Foo,
                                          'a',
                                          uselist=False,
                                          useobject=False)
            attributes.register_attribute(Foo,
                                          'b',
                                          uselist=False,
                                          useobject=False)

            if base is object:
                assert Foo not in instrumentation._instrumentation_factory._state_finders
            else:
                assert Foo in instrumentation._instrumentation_factory._state_finders

            f = Foo()
            attributes.instance_state(f)._expire(attributes.instance_dict(f),
                                                 set())
            eq_(f.a, "this is a")
            eq_(f.b, 12)

            f.a = "this is some new a"
            attributes.instance_state(f)._expire(attributes.instance_dict(f),
                                                 set())
            eq_(f.a, "this is a")
            eq_(f.b, 12)

            attributes.instance_state(f)._expire(attributes.instance_dict(f),
                                                 set())
            f.a = "this is another new a"
            eq_(f.a, "this is another new a")
            eq_(f.b, 12)

            attributes.instance_state(f)._expire(attributes.instance_dict(f),
                                                 set())
            eq_(f.a, "this is a")
            eq_(f.b, 12)

            del f.a
            eq_(f.a, None)
            eq_(f.b, 12)

            attributes.instance_state(f)._commit_all(
                attributes.instance_dict(f))
            eq_(f.a, None)
            eq_(f.b, 12)
    def test_deferred(self):
        for base in (object, MyBaseClass, MyClass):
            class Foo(base):
                pass

            data = {'a': 'this is a', 'b': 12}

            def loader(state, keys):
                for k in keys:
                    state.dict[k] = data[k]
                return attributes.ATTR_WAS_SET

            manager = register_class(Foo)
            manager.deferred_scalar_loader = loader
            attributes.register_attribute(
                Foo, 'a', uselist=False, useobject=False)
            attributes.register_attribute(
                Foo, 'b', uselist=False, useobject=False)

            if base is object:
                assert Foo not in \
                    instrumentation._instrumentation_factory._state_finders
            else:
                assert Foo in \
                    instrumentation._instrumentation_factory._state_finders

            f = Foo()
            attributes.instance_state(f)._expire(
                attributes.instance_dict(f), set())
            eq_(f.a, "this is a")
            eq_(f.b, 12)

            f.a = "this is some new a"
            attributes.instance_state(f)._expire(
                attributes.instance_dict(f), set())
            eq_(f.a, "this is a")
            eq_(f.b, 12)

            attributes.instance_state(f)._expire(
                attributes.instance_dict(f), set())
            f.a = "this is another new a"
            eq_(f.a, "this is another new a")
            eq_(f.b, 12)

            attributes.instance_state(f)._expire(
                attributes.instance_dict(f), set())
            eq_(f.a, "this is a")
            eq_(f.b, 12)

            del f.a
            eq_(f.a, None)
            eq_(f.b, 12)

            attributes.instance_state(f)._commit_all(
                attributes.instance_dict(f))
            eq_(f.a, None)
            eq_(f.b, 12)
示例#4
0
 def _get_similar_unpopulated_models(self, model, session):
     """
     This finds all other models of this class in the session that also need to have this relationship lazyloaded
     """
     model_class = model.__class__
     dict_ = attributes.instance_dict(model)
     similar_models = []
     for possible_model in session.identity_map.values():
         is_same_class = isinstance(possible_model, model_class)
         is_not_new = possible_model not in session.new
         if is_same_class and is_not_new and self.key not in attributes.instance_dict(
                 possible_model):
             similar_models.append(possible_model)
     return similar_models
    def test_load_one_to_many(self):
        User = self.classes.User
        Address = self.classes.Address
        session = fixture_session()

        users = session.query(User).order_by(self.tables.users.c.id.asc()).all()
        self.queries = []

        # make sure no relations are loaded
        for user in users:
            model_dict = attributes.instance_dict(user)
            assert "addresses" not in model_dict

        # trigger a lazy load on the first user
        users[0].addresses

        # only 1 query should have been generated to load all the child relationships
        assert len(self.queries) == 1
        self.queries = []

        user1_dict = attributes.instance_dict(users[0])
        user2_dict = attributes.instance_dict(users[1])
        user3_dict = attributes.instance_dict(users[2])
        user4_dict = attributes.instance_dict(users[3])

        assert [
                   Address(id=1, email_address="*****@*****.**", user_id=7),
               ] == user1_dict["addresses"]
        assert [  # order is by email_address DESC
                   Address(id=2, email_address="*****@*****.**", user_id=8),
                   Address(id=4, email_address="*****@*****.**", user_id=8),
                   Address(id=3, email_address="*****@*****.**", user_id=8),
               ] == user2_dict["addresses"]
        assert [
                   Address(id=5, email_address="*****@*****.**", user_id=9),
               ] == user3_dict["addresses"]
        assert [] == user4_dict["addresses"]

        # backrefs should also not trigger loading
        for address in users[0].addresses:
            assert address.user == users[0]
        for address in users[1].addresses:
            assert address.user == users[1]
        for address in users[2].addresses:
            assert address.user == users[2]

        # no new queries should have been generated
        assert len(self.queries) == 0
    def test_basic(self):
        for base in (object, MyBaseClass, MyClass):
            class User(base):
                pass

            register_class(User)
            attributes.register_attribute(
                User, 'user_id', uselist=False, useobject=False)
            attributes.register_attribute(
                User, 'user_name', uselist=False, useobject=False)
            attributes.register_attribute(
                User, 'email_address', uselist=False, useobject=False)

            u = User()
            u.user_id = 7
            u.user_name = 'john'
            u.email_address = '*****@*****.**'

            eq_(u.user_id, 7)
            eq_(u.user_name, "john")
            eq_(u.email_address, "*****@*****.**")
            attributes.instance_state(u)._commit_all(
                attributes.instance_dict(u))
            eq_(u.user_id, 7)
            eq_(u.user_name, "john")
            eq_(u.email_address, "*****@*****.**")

            u.user_name = 'heythere'
            u.email_address = '*****@*****.**'
            eq_(u.user_id, 7)
            eq_(u.user_name, "heythere")
            eq_(u.email_address, "*****@*****.**")
示例#7
0
 def test_get_populated_passive_return_never_set(self):
     User, Address, sess, a1 = self._u_ad_fixture(True)
     eq_(
         Address.user.impl.get(attributes.instance_state(a1),
                               attributes.instance_dict(a1),
                               passive=attributes.PASSIVE_RETURN_NEVER_SET),
         User(name='ed'))
示例#8
0
def replicate_relation(source, target, attr, target_attr, cache=None):
    if attr.property.cascade.delete_orphan:
        process_scalar = replicate_no_merge
        process_list = replicate_filter
    else:
        process_scalar = reflect
        process_list = reflect_filter
    value = getattr(source, attr.key)
    target_attr_model = target_attr.property.mapper.class_
    if attr.property.uselist:
        adapter = collection_adapter(value)
        if adapter:
            # Convert any collection to flat iterable
            value = adapter.adapt_like_to_iterable(value)
        reflection = process_list(value, target_attr_model, cache=cache)
        impl = instance_state(target).get_impl(attr.key)
        # Set any collection value from flat list
        impl._set_iterable(instance_state(target),
                           instance_dict(target),
                           reflection)
    else:
        reflection = process_scalar(value, target_attr_model, cache=cache)
        setattr(target, attr.key, reflection)
        if (reflection is None and
                attr.property.direction is MANYTOONE and
                any(col.primary_key and not col.nullable
                    for col in attr.property.local_columns)):
            raise _PrimaryKeyIsNull()
示例#9
0
def replicate_relation(source, target, attr, target_attr, cache=None):
    if attr.property.cascade.delete_orphan:
        process_scalar = replicate_no_merge
        process_list = replicate_filter
    else:
        process_scalar = reflect
        process_list = reflect_filter
    value = getattr(source, attr.key)
    target_attr_model = target_attr.property.mapper.class_
    if attr.property.uselist:
        adapter = collection_adapter(value)
        if adapter:
            # XXX The magic passes below are adapted from logic in
            # CollectionAttributeImpl.set() method without proper
            # understanding.  The `elif` branch isn't even coverered by tests.
            if hasattr(value, '_sa_iterator'):
                value = value._sa_iterator()
            elif duck_type_collection(value) is dict:
                value = value.values()
        reflection = process_list(value, target_attr_model, cache=cache)
        impl = instance_state(target).get_impl(attr.key)
        impl.set(instance_state(target), instance_dict(target), reflection,
                 # XXX We either have to convert reflection back to original
                 # collection type or use this private parameter.
                 _adapt=False)
    else:
        reflection = process_scalar(value, target_attr_model, cache=cache)
        setattr(target, attr.key, reflection)
        if (reflection is None and
                attr.property.direction is MANYTOONE and
                any(col.primary_key and not col.nullable
                    for col in attr.property.local_columns)):
            raise _PrimaryKeyIsNull()
示例#10
0
文件: replication.py 项目: ods/iktomi
def replicate_relation(source, target, attr, target_attr, cache=None):
    if attr.property.cascade.delete_orphan:
        process_scalar = replicate_no_merge
        process_list = replicate_filter
    else:
        process_scalar = reflect
        process_list = reflect_filter
    value = getattr(source, attr.key)
    target_attr_model = target_attr.property.mapper.class_
    if attr.property.uselist:
        adapter = collection_adapter(value)
        if adapter:
            # XXX The magic passes below are adapted from logic in
            # CollectionAttributeImpl.set() method without proper
            # understanding.  The `elif` branch isn't even coverered by tests.
            if hasattr(value, '_sa_iterator'):
                value = value._sa_iterator()
            elif duck_type_collection(value) is dict:
                value = value.values()
        reflection = process_list(value, target_attr_model, cache=cache)
        impl = instance_state(target).get_impl(attr.key)
        impl.set(
            instance_state(target),
            instance_dict(target),
            reflection,
            # XXX We either have to convert reflection back to original
            # collection type or use this private parameter.
            _adapt=False)
    else:
        reflection = process_scalar(value, target_attr_model, cache=cache)
        setattr(target, attr.key, reflection)
        if (reflection is None and attr.property.direction is MANYTOONE
                and any(col.primary_key and not col.nullable
                        for col in attr.property.local_columns)):
            raise _PrimaryKeyIsNull()
示例#11
0
 def getMessageByHash(self, message_hash):
     """Get a message for a given hash."""
     try:
         result = self.session.query(Messages).filter(Messages.message_hash == message_hash).one()
         return instance_dict(result)
     except NoResultFound, e:
         return None
示例#12
0
文件: strategies.py 项目: samucc/kuma
    def lazy_clause(self,
                    state,
                    reverse_direction=False,
                    alias_secondary=False,
                    adapt_source=None,
                    detect_transient_pending=False):
        if state is None:
            return self._lazy_none_clause(reverse_direction,
                                          adapt_source=adapt_source)

        if not reverse_direction:
            criterion, bind_to_col, rev = \
                                            self.__lazywhere, \
                                            self.__bind_to_col, \
                                            self._equated_columns
        else:
            criterion, bind_to_col, rev = \
                                LazyLoader._create_lazy_clause(
                                        self.parent_property,
                                        reverse_direction=reverse_direction)

        if reverse_direction:
            mapper = self.parent_property.mapper
        else:
            mapper = self.parent_property.parent

        o = state.obj()  # strong ref
        dict_ = attributes.instance_dict(o)

        def visit_bindparam(bindparam):
            if bindparam.key in bind_to_col:
                # using a flag to enable "detect transient pending" so that
                # the slightly different usage paradigm of "dynamic" loaders
                # continue to work as expected, i.e. that all pending objects
                # should use the "post flush" attributes, and to limit this
                # newer behavior to the query.with_parent() method.
                # It would be nice to do away with this flag.

                if detect_transient_pending and \
                    (not state.key or not state.session_id):
                    bindparam.value = mapper._get_state_attr_by_column(
                        state, dict_, bind_to_col[bindparam.key])
                else:
                    # send value as a lambda so that the value is
                    # acquired after any autoflush occurs.
                    bindparam.value = \
                                lambda: mapper._get_committed_state_attr_by_column(
                                        state, dict_, bind_to_col[bindparam.key])

        if self.parent_property.secondary is not None and alias_secondary:
            criterion = sql_util.ClauseAdapter(
                                self.parent_property.secondary.alias()).\
                                traverse(criterion)

        criterion = visitors.cloned_traverse(criterion, {},
                                             {'bindparam': visit_bindparam})
        if adapt_source:
            criterion = adapt_source(criterion)
        return criterion
示例#13
0
 def fdel(instance):
     state = attributes.instance_state(instance)
     dict_ = attributes.instance_dict(instance)
     previous = dict_.pop(self.key, attributes.NO_VALUE)
     attr = state.manager[self.key]
     attr.dispatch.remove(state, previous, attr.impl)
     for key in self._attribute_keys:
         setattr(instance, key, None)
 def test_history_populated_passive_return_never_set(self):
     User, Address, sess, a1 = self._u_ad_fixture(True)
     eq_(
         Address.user.impl.get_history(
             attributes.instance_state(a1), attributes.instance_dict(a1), passive=attributes.PASSIVE_RETURN_NEVER_SET
         ),
         ((), [User(name="ed")], ()),
     )
示例#15
0
 def fdel(instance):
     state = attributes.instance_state(instance)
     dict_ = attributes.instance_dict(instance)
     previous = dict_.pop(self.key, attributes.NO_VALUE)
     attr = state.manager[self.key]
     attr.dispatch.remove(state, previous, attr.impl)
     for key in self._attribute_keys:
         setattr(instance, key, None)
示例#16
0
 def cache_detail_key(self, data=None):
     if not hasattr(self._meta, 'cache_detail_keys'):
         raise Exception('Class meta has no cache_detail_keys')
     if not has_identity(self):
         raise Exception('Cannot generate detail cache key for instance ' \
             'with no identity')
     data = data or instance_dict(self)
     raw_key, attrs = self._meta.cache_detail_keys[0]
     return self.format_key(raw_key % data)
示例#17
0
 def cache_list_version_key(self, data=None):
     if not self._meta.cache_list_keys:
         raise Exception('Class._meta has no cache_list_keys')
     if not has_identity(self):
         raise Exception('Cannot generate list cache key for instance ' \
             'with no identity')
     data = data or instance_dict(self)
     raw_key, attrs = self._meta.cache_list_keys[0]
     return raw_key % data
示例#18
0
 def test_history_empty_passive_return_never_set(self):
     User, Address, sess, a1 = self._u_ad_fixture(False)
     eq_(
         Address.user.impl.get_history(
             attributes.instance_state(a1),
             attributes.instance_dict(a1),
             passive=attributes.PASSIVE_RETURN_NEVER_SET), ((), (), ()))
     assert 'user_id' not in a1.__dict__
     assert 'user' not in a1.__dict__
示例#19
0
 def test_get_populated_passive_no_initialize(self):
     User, Address, sess, a1 = self._u_ad_fixture(True)
     eq_(
         Address.user.impl.get(attributes.instance_state(a1),
                               attributes.instance_dict(a1),
                               passive=attributes.PASSIVE_NO_INITIALIZE),
         attributes.PASSIVE_NO_RESULT)
     assert 'user_id' not in a1.__dict__
     assert 'user' not in a1.__dict__
示例#20
0
    def lazy_clause(self,
                    state,
                    reverse_direction=False,
                    alias_secondary=False,
                    adapt_source=None):
        if state is None:
            return self._lazy_none_clause(reverse_direction,
                                          adapt_source=adapt_source)

        if not reverse_direction:
            criterion, bind_to_col, rev = \
                                            self.__lazywhere, \
                                            self.__bind_to_col, \
                                            self._equated_columns
        else:
            criterion, bind_to_col, rev = \
                                LazyLoader._create_lazy_clause(
                                        self.parent_property,
                                        reverse_direction=reverse_direction)

        if reverse_direction:
            mapper = self.parent_property.mapper
        else:
            mapper = self.parent_property.parent

        o = state.obj()  # strong ref
        dict_ = attributes.instance_dict(o)

        # use the "committed state" only if we're in a flush
        # for this state.

        sess = sessionlib._state_session(state)
        if sess is not None and sess._flushing:

            def visit_bindparam(bindparam):
                if bindparam.key in bind_to_col:
                    bindparam.callable = \
                                lambda: mapper._get_committed_state_attr_by_column(
                                        state, dict_, bind_to_col[bindparam.key])
        else:

            def visit_bindparam(bindparam):
                if bindparam.key in bind_to_col:
                    bindparam.callable = lambda: mapper._get_state_attr_by_column(
                        state, dict_, bind_to_col[bindparam.key])

        if self.parent_property.secondary is not None and alias_secondary:
            criterion = sql_util.ClauseAdapter(
                                self.parent_property.secondary.alias()).\
                                traverse(criterion)

        criterion = visitors.cloned_traverse(criterion, {},
                                             {'bindparam': visit_bindparam})

        if adapt_source:
            criterion = adapt_source(criterion)
        return criterion
    def test_load_one_to_many_self_refencing(self):
        User = self.classes.User
        session = fixture_session()

        users = (
            session.query(User)
                .filter(self.tables.users.c.id.in_([7, 8]))
                .order_by(self.tables.users.c.id.asc())
                .all()
        )
        self.queries = []

        # make sure no relations are loaded
        for user in users:
            model_dict = attributes.instance_dict(user)
            assert "children" not in model_dict

        # trigger a lazy load on the first user
        users[0].children

        # only 1 query should have been generated to load all the child relationships
        assert len(self.queries) == 1
        self.queries = []

        user1_dict = attributes.instance_dict(users[0])
        user2_dict = attributes.instance_dict(users[1])

        assert [
                   User(id=8, name="jack jr", parent_id=7),
                   User(id=9, name="fred", parent_id=7),
               ] == user1_dict["children"]
        assert [
                   User(id=10, name="jack jr jr", parent_id=8),
               ] == user2_dict["children"]

        # backrefs should also not trigger loading
        for child in users[0].children:
            assert child.parent == users[0]
        for child in users[1].children:
            assert child.parent == users[1]

        # no new queries should have been generated
        assert len(self.queries) == 0
示例#22
0
文件: mixins.py 项目: devhub/baph
 def cache_pointers(self, data=None, columns=[]):
   if not hasattr(self._meta, 'cache_pointers'):
     return {}
   data = data or instance_dict(self)
   keys = {}
   for raw_key, attrs, name in self._meta.cache_pointers:
     if columns and not any(c in attrs for c in columns):
       continue
     keys[name] = raw_key % data
   return keys
示例#23
0
 def cache_pointers(self, data=None, columns=[]):
     if not hasattr(self._meta, 'cache_pointers'):
         return {}
     data = data or instance_dict(self)
     keys = {}
     for raw_key, attrs, name in self._meta.cache_pointers:
         if columns and not any(c in attrs for c in columns):
             continue
         keys[name] = raw_key % data
     return keys
示例#24
0
 def test_get_populated_passive_return_never_set(self):
     User, Address, sess, a1 = self._u_ad_fixture(True)
     eq_(
         Address.user.impl.get(
             attributes.instance_state(a1),
             attributes.instance_dict(a1),
             passive=attributes.PASSIVE_RETURN_NO_VALUE,
         ),
         User(name="ed"),
     )
示例#25
0
 def test_history_empty_passive_no_initialize(self):
     User, Address, sess, a1 = self._u_ad_fixture(False)
     eq_(
         Address.user.impl.get_history(
             attributes.instance_state(a1),
             attributes.instance_dict(a1),
             passive=attributes.PASSIVE_NO_INITIALIZE),
         attributes.HISTORY_BLANK)
     assert 'user_id' not in a1.__dict__
     assert 'user' not in a1.__dict__
 def test_get_empty_passive_no_initialize(self):
     User, Address, sess, a1 = self._u_ad_fixture(False)
     eq_(
         Address.user.impl.get(
             attributes.instance_state(a1), attributes.instance_dict(a1), passive=attributes.PASSIVE_NO_INITIALIZE
         ),
         attributes.PASSIVE_NO_RESULT,
     )
     assert "user_id" not in a1.__dict__
     assert "user" not in a1.__dict__
 def test_history_populated_passive_no_initialize(self):
     User, Address, sess, a1 = self._u_ad_fixture(True)
     eq_(
         Address.user.impl.get_history(
             attributes.instance_state(a1), attributes.instance_dict(a1), passive=attributes.PASSIVE_NO_INITIALIZE
         ),
         attributes.HISTORY_BLANK,
     )
     assert "user_id" not in a1.__dict__
     assert "user" not in a1.__dict__
 def test_history_populated_passive_return_never_set(self):
     User, Address, sess, a1 = self._u_ad_fixture(True)
     eq_(
         Address.user.impl.get_history(
             attributes.instance_state(a1),
             attributes.instance_dict(a1),
             passive=attributes.PASSIVE_RETURN_NO_VALUE,
         ),
         ((), [User(name="ed")], ()),
     )
 def test_history_empty_passive_return_never_set(self):
     User, Address, sess, a1 = self._u_ad_fixture(False)
     eq_(
         Address.user.impl.get_history(
             attributes.instance_state(a1), attributes.instance_dict(a1), passive=attributes.PASSIVE_RETURN_NEVER_SET
         ),
         ((), (), ()),
     )
     assert "user_id" not in a1.__dict__
     assert "user" not in a1.__dict__
示例#30
0
    def lazy_clause(self, state, reverse_direction=False, 
                                alias_secondary=False, 
                                adapt_source=None):
        if state is None:
            return self._lazy_none_clause(
                                        reverse_direction, 
                                        adapt_source=adapt_source)

        if not reverse_direction:
            criterion, bind_to_col, rev = \
                                            self.__lazywhere, \
                                            self.__bind_to_col, \
                                            self._equated_columns
        else:
            criterion, bind_to_col, rev = \
                                LazyLoader._create_lazy_clause(
                                        self.parent_property,
                                        reverse_direction=reverse_direction)

        if reverse_direction:
            mapper = self.parent_property.mapper
        else:
            mapper = self.parent_property.parent

        o = state.obj() # strong ref
        dict_ = attributes.instance_dict(o)

        # use the "committed state" only if we're in a flush
        # for this state.

        sess = sessionlib._state_session(state)
        if sess is not None and sess._flushing:
            def visit_bindparam(bindparam):
                if bindparam.key in bind_to_col:
                    bindparam.callable = \
                                lambda: mapper._get_committed_state_attr_by_column(
                                        state, dict_, bind_to_col[bindparam.key])
        else:
            def visit_bindparam(bindparam):
                if bindparam.key in bind_to_col:
                    bindparam.callable = lambda: mapper._get_state_attr_by_column(
                                            state, dict_, bind_to_col[bindparam.key])


        if self.parent_property.secondary is not None and alias_secondary:
            criterion = sql_util.ClauseAdapter(
                                self.parent_property.secondary.alias()).\
                                traverse(criterion)

        criterion = visitors.cloned_traverse(
                                criterion, {}, {'bindparam':visit_bindparam})

        if adapt_source:
            criterion = adapt_source(criterion)
        return criterion
    def test_load_one_to_one(self):
        User = self.classes.User
        UserInfo = self.classes.UserInfo

        session = fixture_session()
        users = session.query(User).order_by(self.tables.users.c.id.asc()).all()
        self.queries = []

        # make sure no relations are loaded
        for user in users:
            model_dict = attributes.instance_dict(user)
            assert "user_info" not in model_dict

        # trigger a lazy load on the first user
        users[0].user_info

        # only 1 query should have been generated to load all the child relationships
        assert len(self.queries) == 1
        self.queries = []

        user1_dict = attributes.instance_dict(users[0])
        user2_dict = attributes.instance_dict(users[1])
        user3_dict = attributes.instance_dict(users[2])
        user4_dict = attributes.instance_dict(users[3])

        assert UserInfo(id=1, details="is cool", user_id=7) == user1_dict["user_info"]
        assert (
                UserInfo(id=2, details="is not cool", user_id=8) == user2_dict["user_info"]
        )
        assert user3_dict["user_info"] is None
        assert (
                UserInfo(id=3, details="is moderately cool", user_id=10)
                == user4_dict["user_info"]
        )

        # backrefs should also not trigger loading
        assert users[0].user_info.user == users[0]
        assert users[1].user_info.user == users[1]
        assert users[3].user_info.user == users[3]

        # no new queries should have been generated
        assert len(self.queries) == 0
示例#32
0
 def test_get_populated_passive_no_initialize(self):
     User, Address, sess, a1 = self._u_ad_fixture(True)
     eq_(
         Address.user.impl.get(
             attributes.instance_state(a1),
             attributes.instance_dict(a1),
             passive=attributes.PASSIVE_NO_INITIALIZE),
         attributes.PASSIVE_NO_RESULT
     )
     assert 'user_id' not in a1.__dict__
     assert 'user' not in a1.__dict__
    def test_load_many_to_many(self):
        User = self.classes.User
        Thing = self.classes.Thing
        session = fixture_session()

        users = session.query(User).order_by(self.tables.users.c.id.asc()).all()
        self.queries = []

        # make sure no relations are loaded
        for user in users:
            model_dict = attributes.instance_dict(user)
            assert "things" not in model_dict

        # trigger a lazy load on the first user
        users[0].things

        # only 1 query should have been generated to load all the child relationships
        assert len(self.queries) == 1
        self.queries = []

        user1_dict = attributes.instance_dict(users[0])
        user2_dict = attributes.instance_dict(users[1])
        user3_dict = attributes.instance_dict(users[2])
        user4_dict = attributes.instance_dict(users[3])

        assert [
                   Thing(id=1, name="dog"),
               ] == user1_dict["things"]
        assert [
                   Thing(id=1, name="dog"),
               ] == user2_dict["things"]
        assert [
                   Thing(id=2, name="lamp"),
               ] == user3_dict["things"]
        assert [
                   Thing(id=2, name="lamp"),
                   Thing(id=3, name="chair"),
               ] == user4_dict["things"]

        # no new queries should have been generated
        assert len(self.queries) == 0
 def test_get_empty_passive_no_initialize(self):
     User, Address, sess, a1 = self._u_ad_fixture(False)
     eq_(
         Address.user.impl.get(
             attributes.instance_state(a1),
             attributes.instance_dict(a1),
             passive=attributes.PASSIVE_NO_INITIALIZE,
         ),
         attributes.PASSIVE_NO_RESULT,
     )
     assert "user_id" not in a1.__dict__
     assert "user" not in a1.__dict__
 def test_history_populated_passive_no_initialize(self):
     User, Address, sess, a1 = self._u_ad_fixture(True)
     eq_(
         Address.user.impl.get_history(
             attributes.instance_state(a1),
             attributes.instance_dict(a1),
             passive=attributes.PASSIVE_NO_INITIALIZE,
         ),
         attributes.HISTORY_BLANK,
     )
     assert "user_id" not in a1.__dict__
     assert "user" not in a1.__dict__
 def test_get_empty_passive_return_never_set(self):
     User, Address, sess, a1 = self._u_ad_fixture(False)
     eq_(
         Address.user.impl.get(
             attributes.instance_state(a1),
             attributes.instance_dict(a1),
             passive=attributes.PASSIVE_RETURN_NO_VALUE,
         ),
         attributes.NO_VALUE,
     )
     assert "user_id" not in a1.__dict__
     assert "user" not in a1.__dict__
示例#37
0
    def blacklist(self, limit = None):
        """Return blacklisted items in the database,  with optional limit."""
        if (limit is not None):
            rows = self.session.query(Messages).filter(Messages.message_blacklist == True).order_by(desc(Messages.message_priority)).order_by(desc(Messages.message_received_timestamp)).all()[0:limit]
        else:
            rows = self.session.query(Messages).filter(Messages.message_blacklist == True).order_by(desc(Messages.message_priority)).order_by(desc(Messages.message_received_timestamp)).all()

        results = []
        for row in rows:
            results.append(instance_dict(row))

        return results 
    def test_load_many_to_one(self):
        User = self.classes.User
        Address = self.classes.Address
        session = create_session()

        addresses = session.query(Address).order_by(self.tables.addresses.c.id.asc()).all()
        self.queries = []

        # make sure no relations are loaded
        for address in addresses:
            model_dict = attributes.instance_dict(address)
            assert 'user' not in model_dict

        # trigger a lazy load on the first user
        addresses[0].user

        # only 1 query should have been generated to load all the child relationships
        assert len(self.queries) == 1
        self.queries = []

        address1_dict = attributes.instance_dict(addresses[0])
        address2_dict = attributes.instance_dict(addresses[1])
        address3_dict = attributes.instance_dict(addresses[2])
        address4_dict = attributes.instance_dict(addresses[3])
        address5_dict = attributes.instance_dict(addresses[4])

        assert User(id=7, name='jack', parent_id=None) == address1_dict['user']
        assert User(id=8, name='jack jr', parent_id=7) == address2_dict['user']
        assert User(id=8, name='jack jr', parent_id=7) == address3_dict['user']
        assert User(id=8, name='jack jr', parent_id=7) == address4_dict['user']
        assert User(id=9, name='fred', parent_id=7) == address5_dict['user']

        # no new queries should have been generated
        assert len(self.queries) == 0
    def _get_model_value(self, model, mapper, col, passive):
        state = inspect(model)
        dict_ = attributes.instance_dict(model)

        # not sure what this means, taken from LazyLoader#_generate_lazy_clause
        if passive & attributes.INIT_OK:
            passive ^= attributes.INIT_OK

        # adapted from LazyLoader#_generate_lazy_clause
        if passive and passive & attributes.LOAD_AGAINST_COMMITTED:
            return mapper._get_committed_state_attr_by_column(
                state, dict_, col, passive)
        return mapper._get_state_attr_by_column(state, dict_, col, passive)
示例#40
0
    def test_history(self):
        for base in (object, MyBaseClass, MyClass):
            class Foo(base):
                pass
            class Bar(base):
                pass

            attributes.register_class(Foo)
            attributes.register_class(Bar)
            attributes.register_attribute(Foo, "name", uselist=False, useobject=False)
            attributes.register_attribute(Foo, "bars", uselist=True, trackparent=True, useobject=True)
            attributes.register_attribute(Bar, "name", uselist=False, useobject=False)


            f1 = Foo()
            f1.name = 'f1'

            eq_(attributes.get_history(attributes.instance_state(f1), 'name'), (['f1'], (), ()))

            b1 = Bar()
            b1.name = 'b1'
            f1.bars.append(b1)
            eq_(attributes.get_history(attributes.instance_state(f1), 'bars'), ([b1], [], []))

            attributes.instance_state(f1).commit_all(attributes.instance_dict(f1))
            attributes.instance_state(b1).commit_all(attributes.instance_dict(b1))

            eq_(attributes.get_history(attributes.instance_state(f1), 'name'), ((), ['f1'], ()))
            eq_(attributes.get_history(attributes.instance_state(f1), 'bars'), ((), [b1], ()))

            f1.name = 'f1mod'
            b2 = Bar()
            b2.name = 'b2'
            f1.bars.append(b2)
            eq_(attributes.get_history(attributes.instance_state(f1), 'name'), (['f1mod'], (), ['f1']))
            eq_(attributes.get_history(attributes.instance_state(f1), 'bars'), ([b2], [b1], []))
            f1.bars.remove(b1)
            eq_(attributes.get_history(attributes.instance_state(f1), 'bars'), ([b2], [], [b1]))
示例#41
0
    def test_history(self):
        for base in (object, MyBaseClass, MyClass):
            class Foo(base):
                pass
            class Bar(base):
                pass

            attributes.register_class(Foo)
            attributes.register_class(Bar)
            attributes.register_attribute(Foo, "name", uselist=False, useobject=False)
            attributes.register_attribute(Foo, "bars", uselist=True, trackparent=True, useobject=True)
            attributes.register_attribute(Bar, "name", uselist=False, useobject=False)


            f1 = Foo()
            f1.name = 'f1'

            eq_(attributes.get_state_history(attributes.instance_state(f1), 'name'), (['f1'], (), ()))

            b1 = Bar()
            b1.name = 'b1'
            f1.bars.append(b1)
            eq_(attributes.get_state_history(attributes.instance_state(f1), 'bars'), ([b1], [], []))

            attributes.instance_state(f1).commit_all(attributes.instance_dict(f1))
            attributes.instance_state(b1).commit_all(attributes.instance_dict(b1))

            eq_(attributes.get_state_history(attributes.instance_state(f1), 'name'), ((), ['f1'], ()))
            eq_(attributes.get_state_history(attributes.instance_state(f1), 'bars'), ((), [b1], ()))

            f1.name = 'f1mod'
            b2 = Bar()
            b2.name = 'b2'
            f1.bars.append(b2)
            eq_(attributes.get_state_history(attributes.instance_state(f1), 'name'), (['f1mod'], (), ['f1']))
            eq_(attributes.get_state_history(attributes.instance_state(f1), 'bars'), ([b2], [b1], []))
            f1.bars.remove(b1)
            eq_(attributes.get_state_history(attributes.instance_state(f1), 'bars'), ([b2], [], [b1]))
示例#42
0
 def fset(instance, value):
     dict_ = attributes.instance_dict(instance)
     state = attributes.instance_state(instance)
     attr = state.manager[self.key]
     previous = dict_.get(self.key, attributes.NO_VALUE)
     for fn in attr.dispatch.set:
         value = fn(state, value, previous, attr.impl)
     dict_[self.key] = value
     if value is None:
         for key in self._attribute_keys:
             setattr(instance, key, None)
     else:
         for key, value in zip(self._attribute_keys,
                               value.__composite_values__()):
             setattr(instance, key, value)
示例#43
0
 def fset(instance, value):
     dict_ = attributes.instance_dict(instance)
     state = attributes.instance_state(instance)
     attr = state.manager[self.key]
     previous = dict_.get(self.key, attributes.NO_VALUE)
     for fn in attr.dispatch.set:
         value = fn(state, value, previous, attr.impl)
     dict_[self.key] = value
     if value is None:
         for key in self._attribute_keys:
             setattr(instance, key, None)
     else:
         for key, value in zip(
                 self._attribute_keys, 
                 value.__composite_values__()):
             setattr(instance, key, value)
示例#44
0
        def fget(instance):
            dict_ = attributes.instance_dict(instance)

            if self.key not in dict_:
                # key not present.  Iterate through related
                # attributes, retrieve their values.  This
                # ensures they all load.
                values = [getattr(instance, key) for key in self._attribute_keys]

                # usually, the load() event will have loaded our key
                # at this point, unless we only loaded relationship()
                # attributes above.  Populate here if that's the case.
                if self.key not in dict_ and not _none_set.issuperset(values):
                    dict_[self.key] = self.composite_class(*values)

            return dict_.get(self.key, None)
示例#45
0
    def lazy_clause(
        self, state, reverse_direction=False, alias_secondary=False, adapt_source=None, detect_transient_pending=False
    ):
        if state is None:
            return self._lazy_none_clause(reverse_direction, adapt_source=adapt_source)

        if not reverse_direction:
            criterion, bind_to_col, rev = self.__lazywhere, self.__bind_to_col, self._equated_columns
        else:
            criterion, bind_to_col, rev = LazyLoader._create_lazy_clause(
                self.parent_property, reverse_direction=reverse_direction
            )

        if reverse_direction:
            mapper = self.parent_property.mapper
        else:
            mapper = self.parent_property.parent

        o = state.obj()  # strong ref
        dict_ = attributes.instance_dict(o)

        def visit_bindparam(bindparam):
            if bindparam.key in bind_to_col:
                # using a flag to enable "detect transient pending" so that
                # the slightly different usage paradigm of "dynamic" loaders
                # continue to work as expected, i.e. that all pending objects
                # should use the "post flush" attributes, and to limit this
                # newer behavior to the query.with_parent() method.
                # It would be nice to do away with this flag.

                if detect_transient_pending and (not state.key or not state.session_id):
                    bindparam.value = mapper._get_state_attr_by_column(state, dict_, bind_to_col[bindparam.key])
                else:
                    # send value as a lambda so that the value is
                    # acquired after any autoflush occurs.
                    bindparam.value = lambda: mapper._get_committed_state_attr_by_column(
                        state, dict_, bind_to_col[bindparam.key]
                    )

        if self.parent_property.secondary is not None and alias_secondary:
            criterion = sql_util.ClauseAdapter(self.parent_property.secondary.alias()).traverse(criterion)

        criterion = visitors.cloned_traverse(criterion, {}, {"bindparam": visit_bindparam})
        if adapt_source:
            criterion = adapt_source(criterion)
        return criterion
示例#46
0
    def highPriority(self, limit = None, includeBlacklist = False):
        """Return all of the items in the database,  with optional limit."""
        if (limit is not None):
            if (includeBlacklist):
                rows = self.session.query(Messages).filter(Messages.message_priority == 1).order_by(desc(Messages.message_received_timestamp)).all()[0:limit]
            else:
                rows = self.session.query(Messages).filter(Messages.message_priority == 1).filter(Messages.message_blacklist == False).order_by(desc(Messages.message_received_timestamp)).all()[0:limit]
        else:
            if (includeBlacklist):
                rows = self.session.query(Messages).filter(Messages.message_priority == 1).order_by(desc(Messages.message_received_timestamp)).all()
            else:
                rows = self.session.query(Messages).filter(Messages.message_priority == 1).filter(Messages.message_blacklist == False).order_by(desc(Messages.message_received_timestamp)).all()

        results = []
        for row in rows:
            results.append(instance_dict(row))

        return results 
示例#47
0
        def fget(instance):
            dict_ = attributes.instance_dict(instance)

            if self.key not in dict_:
                # key not present.  Iterate through related
                # attributes, retrieve their values.  This
                # ensures they all load.
                values = [
                    getattr(instance, key) for key in self._attribute_keys
                ]

                # usually, the load() event will have loaded our key
                # at this point, unless we only loaded relationship()
                # attributes above.  Populate here if that's the case.
                if self.key not in dict_ and not _none_set.issuperset(values):
                    dict_[self.key] = self.composite_class(*values)

            return dict_.get(self.key, None)
示例#48
0
    def test_basic(self):
        for base in (object, MyBaseClass, MyClass):
            class User(base):
                pass

            attributes.register_class(User)
            attributes.register_attribute(User, 'user_id', uselist = False, useobject=False)
            attributes.register_attribute(User, 'user_name', uselist = False, useobject=False)
            attributes.register_attribute(User, 'email_address', uselist = False, useobject=False)

            u = User()
            u.user_id = 7
            u.user_name = 'john'
            u.email_address = '*****@*****.**'

            self.assert_(u.user_id == 7 and u.user_name == 'john' and u.email_address == '*****@*****.**')
            attributes.instance_state(u).commit_all(attributes.instance_dict(u))
            self.assert_(u.user_id == 7 and u.user_name == 'john' and u.email_address == '*****@*****.**')

            u.user_name = 'heythere'
            u.email_address = '*****@*****.**'
            self.assert_(u.user_id == 7 and u.user_name == 'heythere' and u.email_address == '*****@*****.**')
示例#49
0
    def test_basic(self):
        for base in (object, MyBaseClass, MyClass):

            class User(base):
                pass

            register_class(User)
            attributes.register_attribute(User,
                                          "user_id",
                                          uselist=False,
                                          useobject=False)
            attributes.register_attribute(User,
                                          "user_name",
                                          uselist=False,
                                          useobject=False)
            attributes.register_attribute(User,
                                          "email_address",
                                          uselist=False,
                                          useobject=False)

            u = User()
            u.user_id = 7
            u.user_name = "john"
            u.email_address = "*****@*****.**"

            eq_(u.user_id, 7)
            eq_(u.user_name, "john")
            eq_(u.email_address, "*****@*****.**")
            attributes.instance_state(u)._commit_all(
                attributes.instance_dict(u))
            eq_(u.user_id, 7)
            eq_(u.user_name, "john")
            eq_(u.email_address, "*****@*****.**")

            u.user_name = "heythere"
            u.email_address = "*****@*****.**"
            eq_(u.user_id, 7)
            eq_(u.user_name, "heythere")
            eq_(u.email_address, "*****@*****.**")
示例#50
0
    def test_basic(self):
        for base in (object, MyBaseClass, MyClass):
            class User(base):
                pass

            attributes.register_class(User)
            attributes.register_attribute(User, 'user_id', uselist = False, useobject=False)
            attributes.register_attribute(User, 'user_name', uselist = False, useobject=False)
            attributes.register_attribute(User, 'email_address', uselist = False, useobject=False)

            u = User()
            u.user_id = 7
            u.user_name = 'john'
            u.email_address = '*****@*****.**'

            self.assert_(u.user_id == 7 and u.user_name == 'john' and u.email_address == '*****@*****.**')
            attributes.instance_state(u).commit_all(attributes.instance_dict(u))
            self.assert_(u.user_id == 7 and u.user_name == 'john' and u.email_address == '*****@*****.**')

            u.user_name = 'heythere'
            u.email_address = '*****@*****.**'
            self.assert_(u.user_id == 7 and u.user_name == 'heythere' and u.email_address == '*****@*****.**')
示例#51
0
        def fget(instance):
            dict_ = attributes.instance_dict(instance)
            state = attributes.instance_state(instance)

            if self.key not in dict_:
                # key not present.  Iterate through related
                # attributes, retrieve their values.  This
                # ensures they all load.
                values = [getattr(instance, key) for key in self._attribute_keys]

                # current expected behavior here is that the composite is
                # created on access if the object is persistent or if 
                # col attributes have non-None.  This would be better 
                # if the composite were created unconditionally,
                # but that would be a behavioral change.
                if self.key not in dict_ and (
                    state.key is not None or 
                    not _none_set.issuperset(values)
                ):
                    dict_[self.key] = self.composite_class(*values)
                    state.manager.dispatch.refresh(state, None, [self.key])

            return dict_.get(self.key, None)
示例#52
0
    def test_history(self):
        for base in (object, MyBaseClass, MyClass):

            class Foo(base):
                pass

            class Bar(base):
                pass

            register_class(Foo)
            register_class(Bar)
            attributes.register_attribute(
                Foo, "name", uselist=False, useobject=False
            )
            attributes.register_attribute(
                Foo, "bars", uselist=True, trackparent=True, useobject=True
            )
            attributes.register_attribute(
                Bar, "name", uselist=False, useobject=False
            )

            f1 = Foo()
            f1.name = "f1"

            eq_(
                attributes.get_state_history(
                    attributes.instance_state(f1), "name"
                ),
                (["f1"], (), ()),
            )

            b1 = Bar()
            b1.name = "b1"
            f1.bars.append(b1)
            eq_(
                attributes.get_state_history(
                    attributes.instance_state(f1), "bars"
                ),
                ([b1], [], []),
            )

            attributes.instance_state(f1)._commit_all(
                attributes.instance_dict(f1)
            )
            attributes.instance_state(b1)._commit_all(
                attributes.instance_dict(b1)
            )

            eq_(
                attributes.get_state_history(
                    attributes.instance_state(f1), "name"
                ),
                ((), ["f1"], ()),
            )
            eq_(
                attributes.get_state_history(
                    attributes.instance_state(f1), "bars"
                ),
                ((), [b1], ()),
            )

            f1.name = "f1mod"
            b2 = Bar()
            b2.name = "b2"
            f1.bars.append(b2)
            eq_(
                attributes.get_state_history(
                    attributes.instance_state(f1), "name"
                ),
                (["f1mod"], (), ["f1"]),
            )
            eq_(
                attributes.get_state_history(
                    attributes.instance_state(f1), "bars"
                ),
                ([b2], [b1], []),
            )
            f1.bars.remove(b1)
            eq_(
                attributes.get_state_history(
                    attributes.instance_state(f1), "bars"
                ),
                ([b2], [], [b1]),
            )
示例#53
0
 def as_dict(self):
     return instance_dict(self)
示例#54
0
 def flag_modified(instance, key):
     state, dict_ = instance_state(instance), instance_dict(instance)
     impl = state.manager[key].impl
     state.modified_event(dict_, impl, True, NO_VALUE)
示例#55
0
文件: dynamic.py 项目: dreamwave/rad
 def remove(self, item):
     self.attr.remove(
         attributes.instance_state(self.instance), 
         attributes.instance_dict(self.instance), item, None)
示例#56
0
文件: dynamic.py 项目: dreamwave/rad
 def append(self, item):
     self.attr.append(
         attributes.instance_state(self.instance), 
         attributes.instance_dict(self.instance), item, None)
示例#57
0
 def dict(self):
     o = self.obj()
     if o is not None:
         return attributes.instance_dict(o)
     else:
         return {}