Skip to content

Commit f0c5c4d

Browse files
committed
simplify test cases
1 parent e00919d commit f0c5c4d

File tree

1 file changed

+45
-66
lines changed

1 file changed

+45
-66
lines changed

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 45 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -728,31 +728,19 @@ def test_database_keyword(self):
728728
self.assertEqual(type(cx), sqlite.Connection)
729729

730730

731-
class CxWrapper:
731+
class ParamsCxCloseInIterMany:
732732
def __init__(self, cx):
733733
self.cx = cx
734734

735-
def side_effect(self):
736-
self.cx.close()
737-
738-
739-
class ParamsCxCloseInIterMany(CxWrapper):
740735
def __iter__(self):
741-
self.side_effect()
736+
self.cx.close()
742737
return iter([(1,), (2,), (3,)])
743738

744739

745-
class ParamsCxCloseInNext(CxWrapper):
746-
def __init__(self, cx):
747-
super().__init__(cx)
748-
self.r = iter(range(10))
749-
750-
def __iter__(self):
751-
return self
752-
753-
def __next__(self):
754-
self.side_effect()
755-
return (next(self.r),)
740+
def ParamsCxCloseInNext(cx):
741+
for i in range(10):
742+
cx.close()
743+
yield (i,)
756744

757745

758746
class CursorTests(unittest.TestCase):
@@ -896,15 +884,10 @@ def test_execute_use_after_close_with_bind_parameters(self):
896884
#
897885
# Regression test for https://github.com/python/cpython/issues/143198.
898886

899-
class PT(CxWrapper):
900-
def __init__(self, cx, value):
901-
super().__init__(cx)
902-
self.value = value
903-
887+
class PT:
904888
def __getitem__(self, i):
905-
self.side_effect()
906-
return self.value
907-
889+
cx.close()
890+
return 1
908891
def __len__(self):
909892
return 1
910893

@@ -914,7 +897,7 @@ def __len__(self):
914897
cu = cx.cursor()
915898
msg = r"Cannot operate on a closed database\."
916899
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
917-
cu.execute("insert into tmp(a) values (?)", PT(cx, 1))
900+
cu.execute("insert into tmp(a) values (?)", PT())
918901

919902
def test_execute_named_param_and_sequence(self):
920903
dataset = (
@@ -1099,7 +1082,9 @@ def test_executemany_use_after_close(self, params_class):
10991082
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
11001083
cu.executemany("insert into tmp(a) values (?)", params_class(cx))
11011084

1102-
def test_executemany_use_after_close_with_bind_parameters(self):
1085+
@subTests(("j", "n"), ([0, 1], [0, 3], [1, 3], [2, 3]))
1086+
@subTests("wtype", (list, lambda x: x))
1087+
def test_executemany_use_after_close_with_bind_parameters(self, j, n, wtype):
11031088
# Prevent SIGSEGV when closing the connection while binding parameters.
11041089
#
11051090
# Internally, the connection's state is checked after bind_parameters().
@@ -1109,28 +1094,25 @@ def test_executemany_use_after_close_with_bind_parameters(self):
11091094
#
11101095
# Regression test for https://github.com/python/cpython/issues/143198.
11111096

1112-
class PT(CxWrapper):
1113-
def __init__(self, cx, value):
1114-
super().__init__(cx)
1115-
self.value = value
1097+
cx = sqlite.connect(":memory:")
1098+
cx.execute("create table tmp(a number)")
1099+
self.addCleanup(cx.close)
11161100

1101+
class PT:
1102+
def __init__(self, value):
1103+
self.value = value
11171104
def __getitem__(self, i):
1118-
if self.value == len(ITEMS):
1119-
self.side_effect()
1105+
if self.value == j:
1106+
cx.close()
11201107
return self.value
1121-
11221108
def __len__(self):
11231109
return 1
11241110

1125-
cx = sqlite.connect(":memory:")
1126-
cx.execute("create table tmp(a number)")
1127-
self.addCleanup(cx.close)
1128-
1129-
ITEMS = [PT(cx, 1), PT(cx, 2), PT(cx, 3)]
11301111
cu = cx.cursor()
11311112
msg = r"Cannot operate on a closed database\."
1113+
items = iter(wtype(map(PT, range(n))))
11321114
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
1133-
cu.executemany("insert into tmp(a) values (?)", iter(ITEMS))
1115+
cu.executemany("insert into tmp(a) values (?)", items)
11341116

11351117
def test_fetch_iter(self):
11361118
# Optional DB-API extension.
@@ -1816,24 +1798,20 @@ def test_connection_execute(self):
18161798
def test_connection_execute_use_after_close_with_bind_parameters(self):
18171799
# See CursorTests.test_execute_use_after_close_with_bind_parameters().
18181800

1819-
class PT(CxWrapper):
1820-
def __init__(self, cx, value):
1821-
super().__init__(cx)
1822-
self.value = value
1801+
cx = sqlite.connect(":memory:")
1802+
cx.execute("create table tmp(a number)")
1803+
self.addCleanup(cx.close)
18231804

1805+
class PT:
18241806
def __getitem__(self, i):
1825-
self.side_effect()
1826-
return self.value
1827-
1807+
cx.close()
1808+
return 1
18281809
def __len__(self):
18291810
return 1
18301811

1831-
cx = sqlite.connect(":memory:")
1832-
cx.execute("create table tmp(a number)")
1833-
self.addCleanup(cx.close)
18341812
msg = r"Cannot operate on a closed database\."
18351813
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
1836-
cx.execute("insert into tmp(a) values (?)", PT(cx, 1))
1814+
cx.execute("insert into tmp(a) values (?)", PT())
18371815

18381816
def test_connection_executemany(self):
18391817
con = self.con
@@ -1854,30 +1832,31 @@ def test_connection_executemany_use_after_close(self, params_class):
18541832
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
18551833
cx.executemany("insert into tmp(a) values (?)", params_class(cx))
18561834

1857-
def test_connection_executemany_use_after_close_with_bind_parameters(self):
1835+
@subTests(("j", "n"), ([0, 1], [0, 3], [1, 3], [2, 3]))
1836+
@subTests("wtype", (list, lambda x: x))
1837+
def test_connection_executemany_use_after_close_with_bind_parameters(
1838+
self, j, n, wtype,
1839+
):
18581840
# See CursorTests.test_executemany_use_after_close_with_bind_parameters().
18591841

1860-
class PT(CxWrapper):
1861-
def __init__(self, cx, value):
1862-
super().__init__(cx)
1863-
self.value = value
1842+
cx = sqlite.connect(":memory:")
1843+
cx.execute("create table tmp(a number)")
1844+
self.addCleanup(cx.close)
18641845

1846+
class PT:
1847+
def __init__(self, value):
1848+
self.value = value
18651849
def __getitem__(self, i):
1866-
if self.value == len(ITEMS):
1867-
self.side_effect()
1850+
if self.value == j:
1851+
cx.close()
18681852
return self.value
1869-
18701853
def __len__(self):
18711854
return 1
18721855

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)]
1856+
items = iter(wtype(map(PT, range(n))))
18781857
msg = r"Cannot operate on a closed database\."
18791858
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
1880-
cx.executemany("insert into tmp(a) values (?)", iter(ITEMS))
1859+
cx.executemany("insert into tmp(a) values (?)", items)
18811860

18821861
def test_connection_executescript(self):
18831862
con = self.con

0 commit comments

Comments
 (0)