MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_bulk_update

Method test_bulk_update

test/orm/dml/test_bulk.py:982–1041  ·  view source on GitHub ↗
(self, statement_type)

Source from the content-addressed store, hash-verified

980
981 @testing.combinations("update_mappings", "update_stmt")
982 def test_bulk_update(self, statement_type):
983 Person, Engineer, Manager, Boss = self.classes(
984 "Person", "Engineer", "Manager", "Boss"
985 )
986
987 s = fixture_session()
988
989 b1, b2, b3 = (
990 Boss(name="b1", status="s1", manager_name="mn1", golf_swing="g1"),
991 Boss(name="b2", status="s2", manager_name="mn2", golf_swing="g2"),
992 Boss(name="b3", status="s3", manager_name="mn3", golf_swing="g3"),
993 )
994 s.add_all([b1, b2, b3])
995 s.commit()
996
997 # slight non-convenient thing. we have to fill in boss_id here
998 # for update, this is not sent along automatically. this is not a
999 # new behavior in bulk
1000 new_data = [
1001 {
1002 "person_id": b1.person_id,
1003 "boss_id": b1.boss_id,
1004 "name": "b1_updated",
1005 "manager_name": "mn1_updated",
1006 },
1007 {
1008 "person_id": b3.person_id,
1009 "boss_id": b3.boss_id,
1010 "manager_name": "mn2_updated",
1011 "golf_swing": "g1_updated",
1012 },
1013 ]
1014
1015 if statement_type == "update_mappings":
1016 with self.sql_execution_asserter() as asserter:
1017 s.bulk_update_mappings(Boss, new_data)
1018 elif statement_type == "update_stmt":
1019 with self.sql_execution_asserter() as asserter:
1020 s.execute(update(Boss), new_data)
1021
1022 asserter.assert_(
1023 CompiledSQL(
1024 "UPDATE people SET name=:name WHERE "
1025 "people.person_id = :people_person_id",
1026 [{"name": "b1_updated", "people_person_id": 1}],
1027 ),
1028 CompiledSQL(
1029 "UPDATE managers SET manager_name=:manager_name WHERE "
1030 "managers.person_id = :managers_person_id",
1031 [
1032 {"manager_name": "mn1_updated", "managers_person_id": 1},
1033 {"manager_name": "mn2_updated", "managers_person_id": 3},
1034 ],
1035 ),
1036 CompiledSQL(
1037 "UPDATE boss SET golf_swing=:golf_swing WHERE "
1038 "boss.boss_id = :boss_boss_id",
1039 [{"golf_swing": "g1_updated", "boss_boss_id": 3}],

Callers

nothing calls this directly

Calls 10

fixture_sessionFunction · 0.90
updateFunction · 0.90
CompiledSQLClass · 0.90
BossClass · 0.70
add_allMethod · 0.45
commitMethod · 0.45
bulk_update_mappingsMethod · 0.45
executeMethod · 0.45
assert_Method · 0.45

Tested by

no test coverage detected