本文整理汇总了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"
示例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)
示例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
示例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
示例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,
)
示例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]
示例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 == []
示例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 == {}
示例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)
示例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)
示例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),
],
)
示例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], (), ()))
示例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]))
示例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], (), ()))
示例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"], ()))