@@ -185,3 +185,90 @@ def test_upsert_compound_primary_key(fresh_db):
185185 # .upsert_all() with a single item should set .last_pk
186186 table .upsert_all ([{"species" : "cat" , "id" : 1 , "age" : 5 }], pk = ("species" , "id" ))
187187 assert ("cat" , 1 ) == table .last_pk
188+
189+
190+ @pytest .mark .parametrize ("use_old_upsert" , (False , True ))
191+ def test_upsert_omitted_not_null_column_on_existing_row (use_old_upsert ):
192+ # https://github.com/simonw/sqlite-utils/issues/878
193+ db = Database (memory = True , use_old_upsert = use_old_upsert )
194+ table = db .table ("dogs" )
195+ table .insert (
196+ {"id" : 1 , "name" : "Cleo" , "color" : "brown" },
197+ pk = "id" ,
198+ not_null = {"name" },
199+ )
200+
201+ # Upserting with omitted NOT NULL column on an existing row must succeed
202+ table .upsert ({"id" : 1 , "color" : "black" })
203+ assert table .get (1 ) == {"id" : 1 , "name" : "Cleo" , "color" : "black" }
204+
205+
206+ @pytest .mark .parametrize ("use_old_upsert" , (False , True ))
207+ @pytest .mark .parametrize ("batch_size" , (1 , 2 ))
208+ def test_upsert_all_omitted_not_null_compound_pk (use_old_upsert , batch_size ):
209+ db = Database (memory = True , use_old_upsert = use_old_upsert )
210+ table = db .table ("pets" )
211+ table .insert_all (
212+ [
213+ {"species" : "dog" , "id" : 1 , "name" : "Cleo" , "breed" : "Lab" , "age" : 4 },
214+ {"species" : "cat" , "id" : 1 , "name" : "Nixie" , "breed" : "Siamese" , "age" : 5 },
215+ ],
216+ pk = ("species" , "id" ),
217+ not_null = {"name" , "breed" },
218+ )
219+
220+ table .upsert_all (
221+ [
222+ {"species" : "dog" , "id" : 1 , "age" : 5 },
223+ {"species" : "cat" , "id" : 1 , "age" : 6 },
224+ ],
225+ batch_size = batch_size ,
226+ )
227+
228+ assert table .get (("dog" , 1 )) == {
229+ "species" : "dog" ,
230+ "id" : 1 ,
231+ "name" : "Cleo" ,
232+ "breed" : "Lab" ,
233+ "age" : 5 ,
234+ }
235+ assert table .get (("cat" , 1 )) == {
236+ "species" : "cat" ,
237+ "id" : 1 ,
238+ "name" : "Nixie" ,
239+ "breed" : "Siamese" ,
240+ "age" : 6 ,
241+ }
242+
243+
244+ @pytest .mark .parametrize ("use_old_upsert" , (False , True ))
245+ def test_upsert_omitted_not_null_with_conversions_and_explicit_not_null (use_old_upsert ):
246+ db = Database (memory = True , use_old_upsert = use_old_upsert )
247+ table = db .table ("dogs" )
248+ table .insert (
249+ {"id" : 1 , "name" : "Cleo" , "color" : "brown" },
250+ pk = "id" ,
251+ )
252+ # Upsert with explicit not_null argument and conversion
253+ table .upsert (
254+ {"id" : 1 , "color" : "black" },
255+ not_null = ["name" ],
256+ conversions = {"color" : "upper(?)" },
257+ )
258+ assert table .get (1 ) == {"id" : 1 , "name" : "Cleo" , "color" : "BLACK" }
259+
260+
261+ @pytest .mark .parametrize ("use_old_upsert" , (False , True ))
262+ def test_upsert_omitted_not_null_with_check_constraint (use_old_upsert ):
263+ db = Database (memory = True , use_old_upsert = use_old_upsert )
264+ db .execute ("""
265+ CREATE TABLE dogs (
266+ id INTEGER PRIMARY KEY,
267+ name TEXT NOT NULL CHECK(length(name) >= 3),
268+ color TEXT
269+ )
270+ """ )
271+ table = db .table ("dogs" )
272+ table .insert ({"id" : 1 , "name" : "Cleo" , "color" : "brown" })
273+ table .upsert ({"id" : 1 , "color" : "black" })
274+ assert table .get (1 ) == {"id" : 1 , "name" : "Cleo" , "color" : "black" }
0 commit comments