def _test_baked_lazy_loading_relationship_flag(self, flag): User, Address = self._o2m_fixture(bake_queries=flag) sess = Session() u1 = sess.query(User).first() from sqlalchemy_1_3.orm import Query canary = mock.Mock() # I would think Mock can do this but apparently # it cannot (wrap / autospec don't work together) real_compile_context = Query._compile_context def _my_compile_context(*arg, **kw): if arg[0].column_descriptions[0]["entity"] is Address: canary() return real_compile_context(*arg, **kw) with mock.patch.object(Query, "_compile_context", _my_compile_context): u1.addresses sess.expire(u1) u1.addresses if flag: eq_(canary.call_count, 1) else: eq_(canary.call_count, 2)
def test_fetch_before_delete(self): User = self.classes.User sess = Session() john = sess.query(User).filter_by(name="john").one() sess.expire(john, ["age"]) sess.query(User).filter_by(name="john").filter_by(age=25).delete( synchronize_session="fetch") assert john not in sess
def test_evaluate_unmapped_col(self): User = self.classes.User s = Session() jill = s.query(User).get(3) s.query(User).update({column("name"): "moonbeam"}, synchronize_session="evaluate") eq_(jill.name, "jill") s.expire(jill) eq_(jill.name, "moonbeam")
def test_fetch_before_update(self): User = self.classes.User sess = Session() john = sess.query(User).filter_by(name="john").one() sess.expire(john, ["age"]) sess.query(User).filter_by(name="john").filter_by(age=25).update( { "name": "j2", "age": 40 }, synchronize_session="fetch") eq_(john.name, "j2") eq_(john.age, 40)
def test_update_w_fetch(self): A = self.classes.A s = Session() a1 = A(x=1, y=2) s.add(a1) s.commit() eq_(a1.id, 1) # force a load a1.x = 5 s.expire(a1, ["y"]) assert "y" not in a1.__dict__ s.bulk_save_objects([a1]) s.commit() eq_(a1.x, 5) eq_(a1.y, 2)
def test_remove_add_pending_backref(self): """test that pending doesn't remove an item that's not a net remove.""" Item, Keyword = (self.classes.Item, self.classes.Keyword) session = Session(autoflush=False) k1 = Keyword(name="k1") i1 = Item(description="i1", keywords=[k1]) session.add(i1) session.commit() session.expire(i1, ["keywords"]) k1.items.remove(i1) k1.items.append(i1) eq_(i1.keywords, [k1])
def test_cache_ok_for_event(self, modify_query_fixture): m1 = modify_query_fixture(True) User, Address = self._o2m_fixture() sess = Session() u1 = sess.query(User).filter(User.id == 7).first() u1.addresses eq_(m1.mock_calls, [mock.call(User), mock.call(Address)]) sess.expire(u1, ["addresses"]) u1.addresses eq_(m1.mock_calls, [mock.call(User), mock.call(Address)])
def test_evaluate_before_update(self): User = self.classes.User sess = Session() john = sess.query(User).filter_by(name="john").one() sess.expire(john, ["age"]) # eval must be before the update. otherwise # we eval john, age has been expired and doesn't # match the new value coming in sess.query(User).filter_by(name="john").filter_by(age=25).update( { "name": "j2", "age": 40 }, synchronize_session="evaluate") eq_(john.name, "j2") eq_(john.age, 40)
def test_duplicate_adds(self): Item, Keyword = (self.classes.Item, self.classes.Keyword) session = Session(testing.db, autoflush=False) k1 = Keyword(name="k1") i1 = Item(description="i1", keywords=[k1]) session.add(i1) session.commit() k1.items.append(i1) eq_(i1.keywords, [k1, k1]) session.expire(i1, ["keywords"]) k1.items.append(i1) eq_(i1.keywords, [k1, k1]) session.expire(i1, ["keywords"]) k1.items.append(i1) eq_(i1.keywords, [k1, k1]) eq_(k1.items, [i1, i1, i1, i1]) session.commit() eq_(k1.items, [i1])