Skip to content

Commit d1bb010

Browse files
committed
test coverage for connections
1 parent f25c1f4 commit d1bb010

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ def test_execute_use_after_close_with_bind_parameters(self):
892892
# Internally, the connection's state is checked after bind_parameters().
893893
# Without this check, we would only be aware of the closed connection
894894
# by calling an sqlite3 function afterwards. However, it is important
895-
# that we report the error before leaving executemany() call.
895+
# that we report the error before leaving the execute() call.
896896
#
897897
# Regression test for https://github.com/python/cpython/issues/143198.
898898

@@ -1813,6 +1813,28 @@ def test_connection_execute(self):
18131813
result = self.con.execute("select 5").fetchone()[0]
18141814
self.assertEqual(result, 5, "Basic test of Connection.execute")
18151815

1816+
def test_connection_execute_use_after_close_with_bind_parameters(self):
1817+
# See CursorTests.test_execute_use_after_close_with_bind_parameters().
1818+
1819+
class PT(CxWrapper):
1820+
def __init__(self, cx, value):
1821+
super().__init__(cx)
1822+
self.value = value
1823+
1824+
def __getitem__(self, i):
1825+
self.side_effect()
1826+
return self.value
1827+
1828+
def __len__(self):
1829+
return 1
1830+
1831+
cx = sqlite.connect(":memory:")
1832+
cx.execute("create table tmp(a number)")
1833+
self.addCleanup(cx.close)
1834+
msg = r"Cannot operate on a closed database\."
1835+
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
1836+
cx.execute("insert into tmp(a) values (?)", PT(cx, 1))
1837+
18161838
def test_connection_executemany(self):
18171839
con = self.con
18181840
con.execute("create table test(foo)")
@@ -1832,6 +1854,31 @@ def test_connection_executemany_use_after_close(self, params_class):
18321854
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
18331855
cx.executemany("insert into tmp(a) values (?)", params_class(cx))
18341856

1857+
def test_connection_executemany_use_after_close_with_bind_parameters(self):
1858+
# See CursorTests.test_executemany_use_after_close_with_bind_parameters().
1859+
1860+
class PT(CxWrapper):
1861+
def __init__(self, cx, value):
1862+
super().__init__(cx)
1863+
self.value = value
1864+
1865+
def __getitem__(self, i):
1866+
if self.value == len(ITEMS):
1867+
self.side_effect()
1868+
return self.value
1869+
1870+
def __len__(self):
1871+
return 1
1872+
1873+
cx = sqlite.connect(":memory:")
1874+
cx.execute("create table tmp(a number)")
1875+
self.addCleanup(cx.close)
1876+
1877+
ITEMS = [PT(cx, 1), PT(cx, 2), PT(cx, 3)]
1878+
msg = r"Cannot operate on a closed database\."
1879+
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
1880+
cx.executemany("insert into tmp(a) values (?)", iter(ITEMS))
1881+
18351882
def test_connection_executescript(self):
18361883
con = self.con
18371884
con.executescript("""

0 commit comments

Comments
 (0)