def test_insert_w_fetch(self): A = self.classes.A s = Session() a1 = A(x=1) s.bulk_save_objects([a1]) s.commit()
def test_bulk_save_return_defaults(self): (User, ) = self.classes("User") s = Session() objects = [User(name="u1"), User(name="u2"), User(name="u3")] assert "id" not in objects[0].__dict__ with self.sql_execution_asserter() as asserter: s.bulk_save_objects(objects, return_defaults=True) asserter.assert_( CompiledSQL("INSERT INTO users (name) VALUES (:name)", [{ "name": "u1" }]), CompiledSQL("INSERT INTO users (name) VALUES (:name)", [{ "name": "u2" }]), CompiledSQL("INSERT INTO users (name) VALUES (:name)", [{ "name": "u3" }]), ) eq_(objects[0].__dict__["id"], 1)
def test_bulk_save_updated_include_unchanged(self): (User, ) = self.classes("User") s = Session(expire_on_commit=False) objects = [User(name="u1"), User(name="u2"), User(name="u3")] s.add_all(objects) s.commit() objects[0].name = "u1new" objects[2].name = "u3new" s = Session() with self.sql_execution_asserter() as asserter: s.bulk_save_objects(objects, update_changed_only=False) asserter.assert_( CompiledSQL( "UPDATE users SET name=:name WHERE " "users.id = :users_id", [ { "users_id": 1, "name": "u1new" }, { "users_id": 2, "name": "u2" }, { "users_id": 3, "name": "u3new" }, ], ))
def test_bulk_insert_via_save(self): Foo = self.classes.Foo s = Session() s.bulk_save_objects([Foo(value="value")]) eq_(s.query(Foo).all(), [Foo(version_id=1, value="value")])
def test_bulk_save_mappings_preserve_order(self): (User, ) = self.classes("User") s = Session() # commit some object into db user1 = User(name="i1") user2 = User(name="i2") s.add(user1) s.add(user2) s.commit() # make some changes user1.name = "u1" user3 = User(name="i3") s.add(user3) user2.name = "u2" objects = [user1, user3, user2] from sqlalchemy_1_3 import inspect def _bulk_save_mappings( mapper, mappings, isupdate, isstates, return_defaults, update_changed_only, render_nulls, ): mock_method(list(mappings), isupdate) mock_method = mock.Mock() with mock.patch.object(s, "_bulk_save_mappings", _bulk_save_mappings): s.bulk_save_objects(objects) eq_( mock_method.mock_calls, [ mock.call([inspect(user1)], True), mock.call([inspect(user3)], False), mock.call([inspect(user2)], True), ], ) mock_method = mock.Mock() with mock.patch.object(s, "_bulk_save_mappings", _bulk_save_mappings): s.bulk_save_objects(objects, preserve_order=False) eq_( mock_method.mock_calls, [ mock.call([inspect(user3)], False), mock.call([inspect(user1), inspect(user2)], True), ], )
def test_bulk_update_via_save(self): Foo = self.classes.Foo s = Session() s.add(Foo(value="value")) s.commit() f1 = s.query(Foo).first() f1.value = "new value" s.bulk_save_objects([f1]) s.expunge_all() eq_(s.query(Foo).all(), [Foo(version_id=2, value="new value")])
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_bulk_save_joined_inh_no_defaults(self): Person, Engineer, Manager, Boss = self.classes("Person", "Engineer", "Manager", "Boss") s = Session() with self.sql_execution_asserter() as asserter: s.bulk_save_objects([ Manager(person_id=1, name="m1", status="s1", manager_name="mn1"), Engineer( person_id=2, name="e1", status="s2", primary_language="l1", ), Engineer( person_id=3, name="e2", status="s3", primary_language="l2", ), Boss( person_id=4, boss_id=4, name="b1", status="s3", manager_name="mn2", golf_swing="g1", ), ]) # the only difference here is that common classes are grouped together. # at the moment it doesn't lump all the "people" tables from # different classes together. asserter.assert_( CompiledSQL( "INSERT INTO people (person_id, name, type) VALUES " "(:person_id, :name, :type)", [{ "person_id": 1, "type": "manager", "name": "m1" }], ), CompiledSQL( "INSERT INTO managers (person_id, status, manager_name) " "VALUES (:person_id, :status, :manager_name)", [{ "status": "s1", "person_id": 1, "manager_name": "mn1" }], ), CompiledSQL( "INSERT INTO people (person_id, name, type) VALUES " "(:person_id, :name, :type)", [ { "person_id": 2, "type": "engineer", "name": "e1" }, { "person_id": 3, "type": "engineer", "name": "e2" }, ], ), CompiledSQL( "INSERT INTO engineers (person_id, status, primary_language) " "VALUES (:person_id, :status, :primary_language)", [ { "person_id": 2, "status": "s2", "primary_language": "l1" }, { "person_id": 3, "status": "s3", "primary_language": "l2" }, ], ), CompiledSQL( "INSERT INTO people (person_id, name, type) VALUES " "(:person_id, :name, :type)", [{ "person_id": 4, "type": "boss", "name": "b1" }], ), CompiledSQL( "INSERT INTO managers (person_id, status, manager_name) " "VALUES (:person_id, :status, :manager_name)", [{ "status": "s3", "person_id": 4, "manager_name": "mn2" }], ), CompiledSQL( "INSERT INTO boss (boss_id, golf_swing) VALUES " "(:boss_id, :golf_swing)", [{ "boss_id": 4, "golf_swing": "g1" }], ), )
def test_bulk_save_joined_inh_return_defaults(self): Person, Engineer, Manager, Boss = self.classes("Person", "Engineer", "Manager", "Boss") s = Session() objects = [ Manager(name="m1", status="s1", manager_name="mn1"), Engineer(name="e1", status="s2", primary_language="l1"), Engineer(name="e2", status="s3", primary_language="l2"), Boss(name="b1", status="s3", manager_name="mn2", golf_swing="g1"), ] assert "person_id" not in objects[0].__dict__ with self.sql_execution_asserter() as asserter: s.bulk_save_objects(objects, return_defaults=True) asserter.assert_( CompiledSQL( "INSERT INTO people (name, type) VALUES (:name, :type)", [{ "type": "manager", "name": "m1" }], ), CompiledSQL( "INSERT INTO managers (person_id, status, manager_name) " "VALUES (:person_id, :status, :manager_name)", [{ "person_id": 1, "status": "s1", "manager_name": "mn1" }], ), CompiledSQL( "INSERT INTO people (name, type) VALUES (:name, :type)", [{ "type": "engineer", "name": "e1" }], ), CompiledSQL( "INSERT INTO people (name, type) VALUES (:name, :type)", [{ "type": "engineer", "name": "e2" }], ), CompiledSQL( "INSERT INTO engineers (person_id, status, primary_language) " "VALUES (:person_id, :status, :primary_language)", [ { "person_id": 2, "status": "s2", "primary_language": "l1" }, { "person_id": 3, "status": "s3", "primary_language": "l2" }, ], ), CompiledSQL( "INSERT INTO people (name, type) VALUES (:name, :type)", [{ "type": "boss", "name": "b1" }], ), CompiledSQL( "INSERT INTO managers (person_id, status, manager_name) " "VALUES (:person_id, :status, :manager_name)", [{ "person_id": 4, "status": "s3", "manager_name": "mn2" }], ), CompiledSQL( "INSERT INTO boss (boss_id, golf_swing) VALUES " "(:boss_id, :golf_swing)", [{ "boss_id": 4, "golf_swing": "g1" }], ), ) eq_(objects[0].__dict__["person_id"], 1) eq_(objects[3].__dict__["person_id"], 4) eq_(objects[3].__dict__["boss_id"], 4)