@@ -48,3 +48,56 @@ def test_upsert():
4848 assert obj1 .cookies == 'choco'
4949 assert obj2 .title ['key1' ] == 'beer'
5050 assert obj2 .cookies == 'choco'
51+
52+
53+ def test_upsert_bulk ():
54+ """Tests whether bulk_upsert works properly."""
55+
56+ model = get_fake_model ({
57+ 'first_name' : models .CharField (max_length = 255 , null = True , unique = True ),
58+ 'last_name' : models .CharField (max_length = 255 , null = True )
59+ })
60+
61+ model .objects .bulk_upsert (
62+ conflict_target = ['first_name' ],
63+ rows = [
64+ dict (first_name = 'Swen' , last_name = 'Kooij' ),
65+ dict (first_name = 'Henk' , last_name = 'Test' )
66+ ]
67+ )
68+
69+ row_a = model .objects .get (first_name = 'Swen' )
70+ row_b = model .objects .get (first_name = 'Henk' )
71+
72+ model .objects .bulk_upsert (
73+ conflict_target = ['first_name' ],
74+ rows = [
75+ dict (first_name = 'Swen' , last_name = 'Test' ),
76+ dict (first_name = 'Henk' , last_name = 'Kooij' )
77+ ]
78+ )
79+
80+ row_a .refresh_from_db ()
81+ assert row_a .last_name == 'Test'
82+
83+ row_b .refresh_from_db ()
84+ assert row_b .last_name == 'Kooij'
85+
86+
87+ def test_upsert_bulk_no_rows ():
88+ """Tests whether bulk_upsert doesn't crash when specifying
89+ no rows or a falsy value."""
90+
91+ model = get_fake_model ({
92+ 'name' : models .CharField (max_length = 255 , null = True , unique = True )
93+ })
94+
95+ model .objects .bulk_upsert (
96+ conflict_target = ['name' ],
97+ rows = []
98+ )
99+
100+ model .objects .bulk_upsert (
101+ conflict_target = ['name' ],
102+ rows = None
103+ )
0 commit comments