Skip to content

Commit 5e4626e

Browse files
Add context manager tests
1 parent fb98615 commit 5e4626e

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lib/test/test_sqlite3/test_transactions.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,33 @@ def test_autocommit_explicit_then_disabled(self):
424424
cx.autocommit = False # should commit, then begin
425425
self.assertTrue(cx.in_transaction)
426426

427+
def test_autocommit_enabled_ctx_mgr(self):
428+
with memory_database(autocommit=True) as cx:
429+
# The context manager is a no-op if autocommit=True
430+
with self.check_stmt_trace(cx, []):
431+
with cx:
432+
self.assertFalse(cx.in_transaction)
433+
434+
def test_autocommit_false_ctx_mgr(self):
435+
expected = ["COMMIT", "BEGIN"]
436+
with memory_database(autocommit=False) as cx:
437+
with self.check_stmt_trace(cx, expected):
438+
with cx:
439+
self.assertTrue(cx.in_transaction)
440+
self.assertTrue(cx.in_transaction)
441+
442+
def test_autocommit_compat_ctx_mgr(self):
443+
expected = ["BEGIN ", "INSERT INTO T VALUES(1)", "COMMIT"]
444+
compat = sqlite.DEPRECATED_TRANSACTION_CONTROL
445+
with memory_database(autocommit=compat) as cx:
446+
cx.execute("create table t(t)")
447+
with self.check_stmt_trace(cx, expected):
448+
with cx:
449+
self.assertFalse(cx.in_transaction)
450+
cx.execute("INSERT INTO T VALUES(1)")
451+
self.assertTrue(cx.in_transaction)
452+
self.assertFalse(cx.in_transaction)
453+
427454

428455
if __name__ == "__main__":
429456
unittest.main()

0 commit comments

Comments
 (0)