Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1003b34

Browse files
committed
Modernize sqlite3 tests
Update current tests that use old pattern with assertRaises to make them more maintainable.
1 parent 0e1d680 commit 1003b34

6 files changed

Lines changed: 62 additions & 257 deletions

File tree

Lib/sqlite3/test/dbapi.py

Lines changed: 27 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,8 @@ def CheckCursor(self):
122122

123123
def CheckFailedOpen(self):
124124
YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db"
125-
try:
125+
with self.assertRaises(sqlite.OperationalError):
126126
con = sqlite.connect(YOU_CANNOT_OPEN_THIS)
127-
except sqlite.OperationalError:
128-
return
129-
self.fail("should have raised an OperationalError")
130127

131128
def CheckClose(self):
132129
self.cx.close()
@@ -202,22 +199,12 @@ def CheckExecuteNoArgs(self):
202199
self.cu.execute("delete from test")
203200

204201
def CheckExecuteIllegalSql(self):
205-
try:
202+
with self.assertRaises(sqlite.OperationalError):
206203
self.cu.execute("select asdf")
207-
self.fail("should have raised an OperationalError")
208-
except sqlite.OperationalError:
209-
return
210-
except:
211-
self.fail("raised wrong exception")
212204

213205
def CheckExecuteTooMuchSql(self):
214-
try:
206+
with self.assertRaises(sqlite.Warning):
215207
self.cu.execute("select 5+4; select 4+5")
216-
self.fail("should have raised a Warning")
217-
except sqlite.Warning:
218-
return
219-
except:
220-
self.fail("raised wrong exception")
221208

222209
def CheckExecuteTooMuchSql2(self):
223210
self.cu.execute("select 5+4; -- foo bar")
@@ -232,13 +219,8 @@ def CheckExecuteTooMuchSql3(self):
232219
""")
233220

234221
def CheckExecuteWrongSqlArg(self):
235-
try:
222+
with self.assertRaises(ValueError):
236223
self.cu.execute(42)
237-
self.fail("should have raised a ValueError")
238-
except ValueError:
239-
return
240-
except:
241-
self.fail("raised wrong exception.")
242224

243225
def CheckExecuteArgInt(self):
244226
self.cu.execute("insert into test(id) values (?)", (42,))
@@ -263,27 +245,18 @@ def CheckExecuteNonIterable(self):
263245

264246
def CheckExecuteWrongNoOfArgs1(self):
265247
# too many parameters
266-
try:
248+
with self.assertRaises(sqlite.ProgrammingError):
267249
self.cu.execute("insert into test(id) values (?)", (17, "Egon"))
268-
self.fail("should have raised ProgrammingError")
269-
except sqlite.ProgrammingError:
270-
pass
271250

272251
def CheckExecuteWrongNoOfArgs2(self):
273252
# too little parameters
274-
try:
253+
with self.assertRaises(sqlite.ProgrammingError):
275254
self.cu.execute("insert into test(id) values (?)")
276-
self.fail("should have raised ProgrammingError")
277-
except sqlite.ProgrammingError:
278-
pass
279255

280256
def CheckExecuteWrongNoOfArgs3(self):
281257
# no parameters, parameters are needed
282-
try:
258+
with self.assertRaises(sqlite.ProgrammingError):
283259
self.cu.execute("insert into test(id) values (?)")
284-
self.fail("should have raised ProgrammingError")
285-
except sqlite.ProgrammingError:
286-
pass
287260

288261
def CheckExecuteParamList(self):
289262
self.cu.execute("insert into test(name) values ('foo')")
@@ -322,27 +295,18 @@ def __missing__(self, key):
322295

323296
def CheckExecuteDictMappingTooLittleArgs(self):
324297
self.cu.execute("insert into test(name) values ('foo')")
325-
try:
298+
with self.assertRaises(sqlite.ProgrammingError):
326299
self.cu.execute("select name from test where name=:name and id=:id", {"name": "foo"})
327-
self.fail("should have raised ProgrammingError")
328-
except sqlite.ProgrammingError:
329-
pass
330300

331301
def CheckExecuteDictMappingNoArgs(self):
332302
self.cu.execute("insert into test(name) values ('foo')")
333-
try:
303+
with self.assertRaises(sqlite.ProgrammingError):
334304
self.cu.execute("select name from test where name=:name")
335-
self.fail("should have raised ProgrammingError")
336-
except sqlite.ProgrammingError:
337-
pass
338305

339306
def CheckExecuteDictMappingUnnamed(self):
340307
self.cu.execute("insert into test(name) values ('foo')")
341-
try:
308+
with self.assertRaises(sqlite.ProgrammingError):
342309
self.cu.execute("select name from test where name=?", {"name": "foo"})
343-
self.fail("should have raised ProgrammingError")
344-
except sqlite.ProgrammingError:
345-
pass
346310

347311
def CheckClose(self):
348312
self.cu.close()
@@ -403,32 +367,16 @@ def mygen():
403367
self.cu.executemany("insert into test(income) values (?)", mygen())
404368

405369
def CheckExecuteManyWrongSqlArg(self):
406-
try:
370+
with self.assertRaises(ValueError):
407371
self.cu.executemany(42, [(3,)])
408-
self.fail("should have raised a ValueError")
409-
except ValueError:
410-
return
411-
except:
412-
self.fail("raised wrong exception.")
413372

414373
def CheckExecuteManySelect(self):
415-
try:
374+
with self.assertRaises(sqlite.ProgrammingError):
416375
self.cu.executemany("select ?", [(3,)])
417-
self.fail("should have raised a ProgrammingError")
418-
except sqlite.ProgrammingError:
419-
return
420-
except:
421-
self.fail("raised wrong exception.")
422376

423377
def CheckExecuteManyNotIterable(self):
424-
try:
378+
with self.assertRaises(TypeError):
425379
self.cu.executemany("insert into test(income) values (?)", 42)
426-
self.fail("should have raised a TypeError")
427-
except TypeError:
428-
return
429-
except Exception as e:
430-
print("raised", e.__class__)
431-
self.fail("raised wrong exception.")
432380

433381
def CheckFetchIter(self):
434382
# Optional DB-API extension.
@@ -505,22 +453,15 @@ def CheckCursorConnection(self):
505453
self.assertEqual(self.cu.connection, self.cx)
506454

507455
def CheckWrongCursorCallable(self):
508-
try:
456+
with self.assertRaises(TypeError):
509457
def f(): pass
510458
cur = self.cx.cursor(f)
511-
self.fail("should have raised a TypeError")
512-
except TypeError:
513-
return
514-
self.fail("should have raised a ValueError")
515459

516460
def CheckCursorWrongClass(self):
517461
class Foo: pass
518462
foo = Foo()
519-
try:
463+
with self.assertRaises(TypeError):
520464
cur = sqlite.Cursor(foo)
521-
self.fail("should have raised a ValueError")
522-
except TypeError:
523-
pass
524465

525466
@unittest.skipUnless(threading, 'This test requires threading.')
526467
class ThreadTests(unittest.TestCase):
@@ -719,22 +660,14 @@ def CheckScriptStringSql(self):
719660
def CheckScriptSyntaxError(self):
720661
con = sqlite.connect(":memory:")
721662
cur = con.cursor()
722-
raised = False
723-
try:
663+
with self.assertRaises(sqlite.OperationalError):
724664
cur.executescript("create table test(x); asdf; create table test2(x)")
725-
except sqlite.OperationalError:
726-
raised = True
727-
self.assertEqual(raised, True, "should have raised an exception")
728665

729666
def CheckScriptErrorNormal(self):
730667
con = sqlite.connect(":memory:")
731668
cur = con.cursor()
732-
raised = False
733-
try:
669+
with self.assertRaises(sqlite.OperationalError):
734670
cur.executescript("create table test(sadfsadfdsa); select foo from hurz;")
735-
except sqlite.OperationalError:
736-
raised = True
737-
self.assertEqual(raised, True, "should have raised an exception")
738671

739672
def CheckCursorExecutescriptAsBytes(self):
740673
con = sqlite.connect(":memory:")
@@ -772,59 +705,34 @@ def tearDown(self):
772705
def CheckClosedConCursor(self):
773706
con = sqlite.connect(":memory:")
774707
con.close()
775-
try:
708+
with self.assertRaises(sqlite.ProgrammingError):
776709
cur = con.cursor()
777-
self.fail("Should have raised a ProgrammingError")
778-
except sqlite.ProgrammingError:
779-
pass
780-
except:
781-
self.fail("Should have raised a ProgrammingError")
782710

783711
def CheckClosedConCommit(self):
784712
con = sqlite.connect(":memory:")
785713
con.close()
786-
try:
714+
with self.assertRaises(sqlite.ProgrammingError):
787715
con.commit()
788-
self.fail("Should have raised a ProgrammingError")
789-
except sqlite.ProgrammingError:
790-
pass
791-
except:
792-
self.fail("Should have raised a ProgrammingError")
793716

794717
def CheckClosedConRollback(self):
795718
con = sqlite.connect(":memory:")
796719
con.close()
797-
try:
720+
with self.assertRaises(sqlite.ProgrammingError):
798721
con.rollback()
799-
self.fail("Should have raised a ProgrammingError")
800-
except sqlite.ProgrammingError:
801-
pass
802-
except:
803-
self.fail("Should have raised a ProgrammingError")
804722

805723
def CheckClosedCurExecute(self):
806724
con = sqlite.connect(":memory:")
807725
cur = con.cursor()
808726
con.close()
809-
try:
727+
with self.assertRaises(sqlite.ProgrammingError):
810728
cur.execute("select 4")
811-
self.fail("Should have raised a ProgrammingError")
812-
except sqlite.ProgrammingError:
813-
pass
814-
except:
815-
self.fail("Should have raised a ProgrammingError")
816729

817730
def CheckClosedCreateFunction(self):
818731
con = sqlite.connect(":memory:")
819732
con.close()
820733
def f(x): return 17
821-
try:
734+
with self.assertRaises(sqlite.ProgrammingError):
822735
con.create_function("foo", 1, f)
823-
self.fail("Should have raised a ProgrammingError")
824-
except sqlite.ProgrammingError:
825-
pass
826-
except:
827-
self.fail("Should have raised a ProgrammingError")
828736

829737
def CheckClosedCreateAggregate(self):
830738
con = sqlite.connect(":memory:")
@@ -836,49 +744,29 @@ def step(self, x):
836744
pass
837745
def finalize(self):
838746
return 17
839-
try:
747+
with self.assertRaises(sqlite.ProgrammingError):
840748
con.create_aggregate("foo", 1, Agg)
841-
self.fail("Should have raised a ProgrammingError")
842-
except sqlite.ProgrammingError:
843-
pass
844-
except:
845-
self.fail("Should have raised a ProgrammingError")
846749

847750
def CheckClosedSetAuthorizer(self):
848751
con = sqlite.connect(":memory:")
849752
con.close()
850753
def authorizer(*args):
851754
return sqlite.DENY
852-
try:
755+
with self.assertRaises(sqlite.ProgrammingError):
853756
con.set_authorizer(authorizer)
854-
self.fail("Should have raised a ProgrammingError")
855-
except sqlite.ProgrammingError:
856-
pass
857-
except:
858-
self.fail("Should have raised a ProgrammingError")
859757

860758
def CheckClosedSetProgressCallback(self):
861759
con = sqlite.connect(":memory:")
862760
con.close()
863761
def progress(): pass
864-
try:
762+
with self.assertRaises(sqlite.ProgrammingError):
865763
con.set_progress_handler(progress, 100)
866-
self.fail("Should have raised a ProgrammingError")
867-
except sqlite.ProgrammingError:
868-
pass
869-
except:
870-
self.fail("Should have raised a ProgrammingError")
871764

872765
def CheckClosedCall(self):
873766
con = sqlite.connect(":memory:")
874767
con.close()
875-
try:
768+
with self.assertRaises(sqlite.ProgrammingError):
876769
con()
877-
self.fail("Should have raised a ProgrammingError")
878-
except sqlite.ProgrammingError:
879-
pass
880-
except:
881-
self.fail("Should have raised a ProgrammingError")
882770

883771
class ClosedCurTests(unittest.TestCase):
884772
def setUp(self):
@@ -900,15 +788,9 @@ def CheckClosed(self):
900788
else:
901789
params = []
902790

903-
try:
791+
with self.assertRaises(sqlite.ProgrammingError):
904792
method = getattr(cur, method_name)
905-
906793
method(*params)
907-
self.fail("Should have raised a ProgrammingError: method " + method_name)
908-
except sqlite.ProgrammingError:
909-
pass
910-
except:
911-
self.fail("Should have raised a ProgrammingError: " + method_name)
912794

913795
def suite():
914796
module_suite = unittest.makeSuite(ModuleTests, "Check")

Lib/sqlite3/test/hooks.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,14 @@ def tearDown(self):
3333

3434
def CheckCreateCollationNotCallable(self):
3535
con = sqlite.connect(":memory:")
36-
try:
36+
with self.assertRaises(TypeError) as cm:
3737
con.create_collation("X", 42)
38-
self.fail("should have raised a TypeError")
39-
except TypeError as e:
40-
self.assertEqual(e.args[0], "parameter must be callable")
38+
self.assertEqual(str(cm.exception), 'parameter must be callable')
4139

4240
def CheckCreateCollationNotAscii(self):
4341
con = sqlite.connect(":memory:")
44-
try:
42+
with self.assertRaises(sqlite.ProgrammingError):
4543
con.create_collation("collä", lambda x, y: (x > y) - (x < y))
46-
self.fail("should have raised a ProgrammingError")
47-
except sqlite.ProgrammingError as e:
48-
pass
4944

5045
@unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 1),
5146
'old SQLite versions crash on this test')
@@ -70,11 +65,9 @@ def mycoll(x, y):
7065
self.fail("the expected order was not returned")
7166

7267
con.create_collation("mycoll", None)
73-
try:
68+
with self.assertRaises(sqlite.OperationalError) as cm:
7469
result = con.execute(sql).fetchall()
75-
self.fail("should have raised an OperationalError")
76-
except sqlite.OperationalError as e:
77-
self.assertEqual(e.args[0].lower(), "no such collation sequence: mycoll")
70+
self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll')
7871

7972
def CheckCollationReturnsLargeInteger(self):
8073
def mycoll(x, y):
@@ -106,8 +99,8 @@ def CheckCollationRegisterTwice(self):
10699
result = con.execute("""
107100
select x from (select 'a' as x union select 'b' as x) order by x collate mycoll
108101
""").fetchall()
109-
if result[0][0] != 'b' or result[1][0] != 'a':
110-
self.fail("wrong collation function is used")
102+
self.assertEqual(result[0][0], 'b')
103+
self.assertEqual(result[1][0], 'a')
111104

112105
def CheckDeregisterCollation(self):
113106
"""
@@ -117,12 +110,9 @@ def CheckDeregisterCollation(self):
117110
con = sqlite.connect(":memory:")
118111
con.create_collation("mycoll", lambda x, y: (x > y) - (x < y))
119112
con.create_collation("mycoll", None)
120-
try:
113+
with self.assertRaises(sqlite.OperationalError) as cm:
121114
con.execute("select 'a' as x union select 'b' as x order by x collate mycoll")
122-
self.fail("should have raised an OperationalError")
123-
except sqlite.OperationalError as e:
124-
if not e.args[0].startswith("no such collation sequence"):
125-
self.fail("wrong OperationalError raised")
115+
self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll')
126116

127117
class ProgressTests(unittest.TestCase):
128118
def CheckProgressHandlerUsed(self):

0 commit comments

Comments
 (0)