@@ -188,7 +188,10 @@ class CursorTests(unittest.TestCase):
188188 def setUp (self ):
189189 self .cx = sqlite .connect (":memory:" )
190190 self .cu = self .cx .cursor ()
191- self .cu .execute ("create table test(id integer primary key, name text, income number)" )
191+ self .cu .execute (
192+ "create table test(id integer primary key, name text, "
193+ "income number, unique_test text unique)"
194+ )
192195 self .cu .execute ("insert into test(name) values (?)" , ("foo" ,))
193196
194197 def tearDown (self ):
@@ -462,6 +465,44 @@ class Foo: pass
462465 with self .assertRaises (TypeError ):
463466 cur = sqlite .Cursor (foo )
464467
468+ def CheckLastRowIDOnReplace (self ):
469+ """
470+ INSERT OR REPLACE and REPLACE INTO should produce the same behavior.
471+ """
472+ sql = '{} INTO test(id, unique_test) VALUES (?, ?)'
473+ for statement in ('INSERT OR REPLACE' , 'REPLACE' ):
474+ with self .subTest (statement = statement ):
475+ self .cu .execute (sql .format (statement ), (1 , 'foo' ))
476+ self .assertEqual (self .cu .lastrowid , 1 )
477+
478+ def CheckLastRowIDOnIgnore (self ):
479+ self .cu .execute (
480+ "insert or ignore into test(unique_test) values (?)" ,
481+ ('test' ,))
482+ self .assertEqual (self .cu .lastrowid , 2 )
483+ self .cu .execute (
484+ "insert or ignore into test(unique_test) values (?)" ,
485+ ('test' ,))
486+ self .assertEqual (self .cu .lastrowid , 2 )
487+
488+ def CheckLastRowIDInsertOR (self ):
489+ results = []
490+ for statement in ('FAIL' , 'ABORT' , 'ROLLBACK' ):
491+ sql = 'INSERT OR {} INTO test(unique_test) VALUES (?)'
492+ with self .subTest (statement = 'INSERT OR {}' .format (statement )):
493+ self .cu .execute (sql .format (statement ), (statement ,))
494+ results .append ((statement , self .cu .lastrowid ))
495+ with self .assertRaises (sqlite .IntegrityError ):
496+ self .cu .execute (sql .format (statement ), (statement ,))
497+ results .append ((statement , self .cu .lastrowid ))
498+ expected = [
499+ ('FAIL' , 2 ), ('FAIL' , 2 ),
500+ ('ABORT' , 3 ), ('ABORT' , 3 ),
501+ ('ROLLBACK' , 4 ), ('ROLLBACK' , 4 ),
502+ ]
503+ self .assertEqual (results , expected )
504+
505+
465506@unittest .skipUnless (threading , 'This test requires threading.' )
466507class ThreadTests (unittest .TestCase ):
467508 def setUp (self ):
0 commit comments