當前位置: 首頁>>代碼示例>>Python>>正文


Python attributes.instance_state方法代碼示例

本文整理匯總了Python中sqlalchemy.orm.attributes.instance_state方法的典型用法代碼示例。如果您正苦於以下問題:Python attributes.instance_state方法的具體用法?Python attributes.instance_state怎麽用?Python attributes.instance_state使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sqlalchemy.orm.attributes的用法示例。


在下文中一共展示了attributes.instance_state方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_no_instance_key

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_no_instance_key(self):
        User, users = self.classes.User, self.tables.users

        # this tests an artificial condition such that
        # an instance is pending, but has expired attributes.  this
        # is actually part of a larger behavior when postfetch needs to
        # occur during a flush() on an instance that was just inserted
        mapper(User, users)
        sess = create_session()
        u = sess.query(User).get(7)

        sess.expire(u, attribute_names=["name"])
        sess.expunge(u)
        attributes.instance_state(u).key = None
        assert "name" not in u.__dict__
        sess.add(u)
        assert u.name == "jack" 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:19,代碼來源:test_expire.py

示例2: test_expire_committed

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_expire_committed(self):
        """test that the committed state of the attribute receives the most
        recent DB data"""

        orders, Order = self.tables.orders, self.classes.Order

        mapper(Order, orders)

        sess = create_session()
        o = sess.query(Order).get(3)
        sess.expire(o)

        orders.update().execute(description="order 3 modified")
        assert o.isopen == 1
        assert (
            attributes.instance_state(o).dict["description"]
            == "order 3 modified"
        )

        def go():
            sess.flush()

        self.assert_sql_count(testing.db, go, 0) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:25,代碼來源:test_expire.py

示例3: test_scalar_obj_pop_invalid

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_scalar_obj_pop_invalid(self):
        A, B = self._scalar_obj_fixture()

        a1 = A()
        b1 = B()
        b2 = B()

        A.b.impl.append(
            attributes.instance_state(a1),
            attributes.instance_dict(a1),
            b1,
            None,
        )

        assert a1.b is b1

        A.b.impl.pop(
            attributes.instance_state(a1),
            attributes.instance_dict(a1),
            b2,
            None,
        )
        assert a1.b is b1 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:25,代碼來源:test_attributes.py

示例4: test_scalar_obj_pop_valid

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_scalar_obj_pop_valid(self):
        A, B = self._scalar_obj_fixture()

        a1 = A()
        b1 = B()

        A.b.impl.append(
            attributes.instance_state(a1),
            attributes.instance_dict(a1),
            b1,
            None,
        )

        assert a1.b is b1

        A.b.impl.pop(
            attributes.instance_state(a1),
            attributes.instance_dict(a1),
            b1,
            None,
        )
        assert a1.b is None 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:24,代碼來源:test_attributes.py

示例5: test_collection_obj_remove_invalid

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_collection_obj_remove_invalid(self):
        A, B = self._collection_obj_fixture()

        a1 = A()
        b1 = B()
        b2 = B()

        A.b.impl.append(
            attributes.instance_state(a1),
            attributes.instance_dict(a1),
            b1,
            None,
        )

        assert a1.b == [b1]

        assert_raises_message(
            ValueError,
            r"list.remove\(.*?\): .* not in list",
            A.b.impl.remove,
            attributes.instance_state(a1),
            attributes.instance_dict(a1),
            b2,
            None,
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:27,代碼來源:test_attributes.py

示例6: test_collection_obj_pop_invalid

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_collection_obj_pop_invalid(self):
        A, B = self._collection_obj_fixture()

        a1 = A()
        b1 = B()
        b2 = B()

        A.b.impl.append(
            attributes.instance_state(a1),
            attributes.instance_dict(a1),
            b1,
            None,
        )

        assert a1.b == [b1]

        A.b.impl.pop(
            attributes.instance_state(a1),
            attributes.instance_dict(a1),
            b2,
            None,
        )
        assert a1.b == [b1] 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:25,代碼來源:test_attributes.py

示例7: test_collection_obj_pop_valid

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_collection_obj_pop_valid(self):
        A, B = self._collection_obj_fixture()

        a1 = A()
        b1 = B()

        A.b.impl.append(
            attributes.instance_state(a1),
            attributes.instance_dict(a1),
            b1,
            None,
        )

        assert a1.b == [b1]

        A.b.impl.pop(
            attributes.instance_state(a1),
            attributes.instance_dict(a1),
            b1,
            None,
        )
        assert a1.b == [] 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:24,代碼來源:test_attributes.py

示例8: test_state_gc

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_state_gc(self):
        """test that InstanceState always has a dict, even after host
        object gc'ed."""

        class Foo(object):
            pass

        instrumentation.register_class(Foo)
        f = Foo()
        state = attributes.instance_state(f)
        f.bar = "foo"
        eq_(state.dict, {"bar": "foo", state.manager.STATE_ATTR: state})
        del f
        gc_collect()
        assert state.obj() is None
        assert state.dict == {} 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:18,代碼來源:test_attributes.py

示例9: test_no_double_state

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_no_double_state(self):
        states = set()

        class Foo(object):
            def __init__(self):
                states.add(attributes.instance_state(self))

        class Bar(Foo):
            def __init__(self):
                states.add(attributes.instance_state(self))
                Foo.__init__(self)

        instrumentation.register_class(Foo)
        instrumentation.register_class(Bar)

        b = Bar()
        eq_(len(states), 1)
        eq_(list(states)[0].obj(), b) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:20,代碼來源:test_attributes.py

示例10: test_lazy_history_collection

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_lazy_history_collection(self):
        Post, Blog, lazy_posts = self._fixture()

        p1, p2, p3 = Post("post 1"), Post("post 2"), Post("post 3")
        lazy_posts.return_value = [p1, p2, p3]

        b = Blog("blog 1")
        p = Post("post 4")
        p.blog = b

        p4 = Post("post 5")
        p4.blog = b

        eq_(lazy_posts.call_count, 1)

        eq_(
            attributes.instance_state(b).get_history(
                "posts", attributes.PASSIVE_OFF
            ),
            ([p, p4], [p1, p2, p3], []),
        )
        eq_(lazy_posts.call_count, 1) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:24,代碼來源:test_attributes.py

示例11: test_commit_removes_pending

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_commit_removes_pending(self):
        Post, Blog, lazy_posts = self._fixture()

        p1 = Post("post 1")

        lazy_posts.return_value = attributes.PASSIVE_NO_RESULT
        b = Blog("blog 1")
        p1.blog = b

        b_state = attributes.instance_state(b)
        p1_state = attributes.instance_state(p1)
        b_state._commit_all(attributes.instance_dict(b))
        p1_state._commit_all(attributes.instance_dict(p1))
        lazy_posts.return_value = [p1]
        eq_(b.posts, [Post("post 1")])
        eq_(
            lazy_posts.mock_calls,
            [
                call(b_state, attributes.PASSIVE_NO_FETCH),
                call(b_state, attributes.PASSIVE_OFF),
            ],
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:24,代碼來源:test_attributes.py

示例12: test_object_del_expired

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_object_del_expired(self):
        Foo, Bar = self._two_obj_fixture(uselist=False)
        f = Foo()
        b1 = Bar()
        f.someattr = b1
        self._commit_someattr(f)

        # the "delete" handler checks if the object
        # is db-loaded when testing if an empty "del" is valid,
        # because there's nothing else to look at for a related
        # object, there's no "expired" status
        attributes.instance_state(f).key = ("foo",)
        attributes.instance_state(f)._expire_attributes(
            attributes.instance_dict(f), ["someattr"]
        )

        del f.someattr
        eq_(self._someattr_history(f), ([None], (), ())) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:20,代碼來源:test_attributes.py

示例13: test_scalar_del

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_scalar_del(self):
        # note - compare:
        # test_scalar_set_None,
        # test_scalar_get_first_set_None,
        # test_use_object_set_None,
        # test_use_object_get_first_set_None
        Foo = self._fixture(
            uselist=False, useobject=False, active_history=False
        )
        f = Foo()
        f.someattr = 5
        attributes.instance_state(f).key = ("foo",)
        self._commit_someattr(f)

        del f.someattr
        eq_(self._someattr_history(f), ((), (), [5])) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:18,代碼來源:test_attributes.py

示例14: test_scalar_del_expired

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_scalar_del_expired(self):
        # note - compare:
        # test_scalar_set_None,
        # test_scalar_get_first_set_None,
        # test_use_object_set_None,
        # test_use_object_get_first_set_None
        Foo = self._fixture(
            uselist=False, useobject=False, active_history=False
        )
        f = Foo()
        f.someattr = 5
        self._commit_someattr(f)

        attributes.instance_state(f)._expire_attributes(
            attributes.instance_dict(f), ["someattr"]
        )
        del f.someattr
        eq_(self._someattr_history(f), ([None], (), ())) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:20,代碼來源:test_attributes.py

示例15: test_scalar_passive_flag

# 需要導入模塊: from sqlalchemy.orm import attributes [as 別名]
# 或者: from sqlalchemy.orm.attributes import instance_state [as 別名]
def test_scalar_passive_flag(self):
        Foo = self._fixture(
            uselist=False, useobject=False, active_history=True
        )
        f = Foo()
        f.someattr = "one"
        eq_(self._someattr_history(f), (["one"], (), ()))

        self._commit_someattr(f)

        state = attributes.instance_state(f)
        # do the same thing that
        # populators.expire.append((self.key, True))
        # does in loading.py
        state.dict.pop("someattr", None)
        state.expired_attributes.add("someattr")

        def scalar_loader(state, toload, passive):
            state.dict["someattr"] = "one"

        state.manager.expired_attribute_loader = scalar_loader

        eq_(self._someattr_history(f), ((), ["one"], ())) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:25,代碼來源:test_attributes.py


注:本文中的sqlalchemy.orm.attributes.instance_state方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。