summaryrefslogtreecommitdiffstats
path: root/Lib/sqlite3
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/sqlite3')
-rw-r--r--Lib/sqlite3/test/dbapi.py2
-rw-r--r--Lib/sqlite3/test/factory.py53
-rw-r--r--Lib/sqlite3/test/hooks.py56
-rw-r--r--Lib/sqlite3/test/regression.py11
-rw-r--r--Lib/sqlite3/test/transactions.py2
-rw-r--r--Lib/sqlite3/test/types.py6
-rw-r--r--Lib/sqlite3/test/userfunctions.py2
7 files changed, 91 insertions, 41 deletions
diff --git a/Lib/sqlite3/test/dbapi.py b/Lib/sqlite3/test/dbapi.py
index 202bd38..b7ec1ad 100644
--- a/Lib/sqlite3/test/dbapi.py
+++ b/Lib/sqlite3/test/dbapi.py
@@ -1,4 +1,4 @@
-#-*- coding: ISO-8859-1 -*-
+#-*- coding: iso-8859-1 -*-
# pysqlite2/test/dbapi.py: tests for DB-API compliance
#
# Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
diff --git a/Lib/sqlite3/test/factory.py b/Lib/sqlite3/test/factory.py
index 7f6f347..1013755 100644
--- a/Lib/sqlite3/test/factory.py
+++ b/Lib/sqlite3/test/factory.py
@@ -1,4 +1,4 @@
-#-*- coding: ISO-8859-1 -*-
+#-*- coding: iso-8859-1 -*-
# pysqlite2/test/factory.py: tests for the various factories in pysqlite
#
# Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
@@ -47,9 +47,7 @@ class ConnectionFactoryTests(unittest.TestCase):
self.con.close()
def CheckIsInstance(self):
- self.assertTrue(isinstance(self.con,
- MyConnection),
- "connection is not instance of MyConnection")
+ self.assertIsInstance(self.con, MyConnection)
class CursorFactoryTests(unittest.TestCase):
def setUp(self):
@@ -60,9 +58,7 @@ class CursorFactoryTests(unittest.TestCase):
def CheckIsInstance(self):
cur = self.con.cursor(factory=MyCursor)
- self.assertTrue(isinstance(cur,
- MyCursor),
- "cursor is not instance of MyCursor")
+ self.assertIsInstance(cur, MyCursor)
class RowFactoryTestsBackwardsCompat(unittest.TestCase):
def setUp(self):
@@ -72,9 +68,7 @@ class RowFactoryTestsBackwardsCompat(unittest.TestCase):
cur = self.con.cursor(factory=MyCursor)
cur.execute("select 4+5 as foo")
row = cur.fetchone()
- self.assertTrue(isinstance(row,
- dict),
- "row is not instance of dict")
+ self.assertIsInstance(row, dict)
cur.close()
def tearDown(self):
@@ -87,28 +81,24 @@ class RowFactoryTests(unittest.TestCase):
def CheckCustomFactory(self):
self.con.row_factory = lambda cur, row: list(row)
row = self.con.execute("select 1, 2").fetchone()
- self.assertTrue(isinstance(row,
- list),
- "row is not instance of list")
+ self.assertIsInstance(row, list)
def CheckSqliteRowIndex(self):
self.con.row_factory = sqlite.Row
row = self.con.execute("select 1 as a, 2 as b").fetchone()
- self.assertTrue(isinstance(row,
- sqlite.Row),
- "row is not instance of sqlite.Row")
+ self.assertIsInstance(row, sqlite.Row)
col1, col2 = row["a"], row["b"]
- self.assertTrue(col1 == 1, "by name: wrong result for column 'a'")
- self.assertTrue(col2 == 2, "by name: wrong result for column 'a'")
+ self.assertEqual(col1, 1, "by name: wrong result for column 'a'")
+ self.assertEqual(col2, 2, "by name: wrong result for column 'a'")
col1, col2 = row["A"], row["B"]
- self.assertTrue(col1 == 1, "by name: wrong result for column 'A'")
- self.assertTrue(col2 == 2, "by name: wrong result for column 'B'")
+ self.assertEqual(col1, 1, "by name: wrong result for column 'A'")
+ self.assertEqual(col2, 2, "by name: wrong result for column 'B'")
col1, col2 = row[0], row[1]
- self.assertTrue(col1 == 1, "by index: wrong result for column 0")
- self.assertTrue(col2 == 2, "by index: wrong result for column 1")
+ self.assertEqual(col1, 1, "by index: wrong result for column 0")
+ self.assertEqual(col2, 2, "by index: wrong result for column 1")
def CheckSqliteRowIter(self):
"""Checks if the row object is iterable"""
@@ -122,6 +112,7 @@ class RowFactoryTests(unittest.TestCase):
self.con.row_factory = sqlite.Row
row = self.con.execute("select 1 as a, 2 as b").fetchone()
t = tuple(row)
+ self.assertEqual(t, (row['a'], row['b']))
def CheckSqliteRowAsDict(self):
"""Checks if the row object can be correctly converted to a dictionary"""
@@ -138,8 +129,8 @@ class RowFactoryTests(unittest.TestCase):
row_2 = self.con.execute("select 1 as a, 2 as b").fetchone()
row_3 = self.con.execute("select 1 as a, 3 as b").fetchone()
- self.assertTrue(row_1 == row_1)
- self.assertTrue(row_1 == row_2)
+ self.assertEqual(row_1, row_1)
+ self.assertEqual(row_1, row_2)
self.assertTrue(row_2 != row_3)
self.assertFalse(row_1 != row_1)
@@ -161,30 +152,32 @@ class TextFactoryTests(unittest.TestCase):
def CheckUnicode(self):
austria = "Österreich"
row = self.con.execute("select ?", (austria,)).fetchone()
- self.assertTrue(type(row[0]) == str, "type of row[0] must be unicode")
+ self.assertEqual(type(row[0]), str, "type of row[0] must be unicode")
def CheckString(self):
self.con.text_factory = bytes
austria = "Österreich"
row = self.con.execute("select ?", (austria,)).fetchone()
- self.assertTrue(type(row[0]) == bytes, "type of row[0] must be bytes")
- self.assertTrue(row[0] == austria.encode("utf-8"), "column must equal original data in UTF-8")
+ self.assertEqual(type(row[0]), bytes, "type of row[0] must be bytes")
+ self.assertEqual(row[0], austria.encode("utf-8"), "column must equal original data in UTF-8")
def CheckCustom(self):
self.con.text_factory = lambda x: str(x, "utf-8", "ignore")
austria = "Österreich"
row = self.con.execute("select ?", (austria,)).fetchone()
- self.assertTrue(type(row[0]) == str, "type of row[0] must be unicode")
+ self.assertEqual(type(row[0]), str, "type of row[0] must be unicode")
self.assertTrue(row[0].endswith("reich"), "column must contain original data")
def CheckOptimizedUnicode(self):
+ # In py3k, str objects are always returned when text_factory
+ # is OptimizedUnicode
self.con.text_factory = sqlite.OptimizedUnicode
austria = "Österreich"
germany = "Deutchland"
a_row = self.con.execute("select ?", (austria,)).fetchone()
d_row = self.con.execute("select ?", (germany,)).fetchone()
- self.assertTrue(type(a_row[0]) == str, "type of non-ASCII row must be str")
- self.assertTrue(type(d_row[0]) == str, "type of ASCII-only row must be str")
+ self.assertEqual(type(a_row[0]), str, "type of non-ASCII row must be str")
+ self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str")
def tearDown(self):
self.con.close()
diff --git a/Lib/sqlite3/test/hooks.py b/Lib/sqlite3/test/hooks.py
index 9544149..ede0bec 100644
--- a/Lib/sqlite3/test/hooks.py
+++ b/Lib/sqlite3/test/hooks.py
@@ -1,4 +1,4 @@
-#-*- coding: ISO-8859-1 -*-
+#-*- coding: iso-8859-1 -*-
# pysqlite2/test/hooks.py: tests for various SQLite-specific hooks
#
# Copyright (C) 2006-2007 Gerhard Häring <gh@ghaering.de>
@@ -162,7 +162,7 @@ class ProgressTests(unittest.TestCase):
create table bar (a, b)
""")
second_count = len(progress_calls)
- self.assertTrue(first_count > second_count)
+ self.assertGreaterEqual(first_count, second_count)
def CheckCancelOperation(self):
"""
@@ -195,10 +195,60 @@ class ProgressTests(unittest.TestCase):
con.execute("select 1 union select 2 union select 3").fetchall()
self.assertEqual(action, 0, "progress handler was not cleared")
+class TraceCallbackTests(unittest.TestCase):
+ def CheckTraceCallbackUsed(self):
+ """
+ Test that the trace callback is invoked once it is set.
+ """
+ con = sqlite.connect(":memory:")
+ traced_statements = []
+ def trace(statement):
+ traced_statements.append(statement)
+ con.set_trace_callback(trace)
+ con.execute("create table foo(a, b)")
+ self.assertTrue(traced_statements)
+ self.assertTrue(any("create table foo" in stmt for stmt in traced_statements))
+
+ def CheckClearTraceCallback(self):
+ """
+ Test that setting the trace callback to None clears the previously set callback.
+ """
+ con = sqlite.connect(":memory:")
+ traced_statements = []
+ def trace(statement):
+ traced_statements.append(statement)
+ con.set_trace_callback(trace)
+ con.set_trace_callback(None)
+ con.execute("create table foo(a, b)")
+ self.assertFalse(traced_statements, "trace callback was not cleared")
+
+ def CheckUnicodeContent(self):
+ """
+ Test that the statement can contain unicode literals.
+ """
+ unicode_value = '\xf6\xe4\xfc\xd6\xc4\xdc\xdf\u20ac'
+ con = sqlite.connect(":memory:")
+ traced_statements = []
+ def trace(statement):
+ traced_statements.append(statement)
+ con.set_trace_callback(trace)
+ con.execute("create table foo(x)")
+ # Can't execute bound parameters as their values don't appear
+ # in traced statements before SQLite 3.6.21
+ # (cf. http://www.sqlite.org/draft/releaselog/3_6_21.html)
+ con.execute('insert into foo(x) values ("%s")' % unicode_value)
+ con.commit()
+ self.assertTrue(any(unicode_value in stmt for stmt in traced_statements),
+ "Unicode data %s garbled in trace callback: %s"
+ % (ascii(unicode_value), ', '.join(map(ascii, traced_statements))))
+
+
+
def suite():
collation_suite = unittest.makeSuite(CollationTests, "Check")
progress_suite = unittest.makeSuite(ProgressTests, "Check")
- return unittest.TestSuite((collation_suite, progress_suite))
+ trace_suite = unittest.makeSuite(TraceCallbackTests, "Check")
+ return unittest.TestSuite((collation_suite, progress_suite, trace_suite))
def test():
runner = unittest.TextTestRunner()
diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py
index 5e2fbf9..c557ab6 100644
--- a/Lib/sqlite3/test/regression.py
+++ b/Lib/sqlite3/test/regression.py
@@ -161,7 +161,8 @@ class RegressionTests(unittest.TestCase):
def CheckCursorConstructorCallCheck(self):
"""
- Verifies that cursor methods check wether base class __init__ was called.
+ Verifies that cursor methods check whether base class __init__ was
+ called.
"""
class Cursor(sqlite.Cursor):
def __init__(self, con):
@@ -187,7 +188,8 @@ class RegressionTests(unittest.TestCase):
def CheckConnectionConstructorCallCheck(self):
"""
- Verifies that connection methods check wether base class __init__ was called.
+ Verifies that connection methods check whether base class __init__ was
+ called.
"""
class Connection(sqlite.Connection):
def __init__(self, name):
@@ -328,6 +330,11 @@ class RegressionTests(unittest.TestCase):
datetime.datetime(2012, 4, 4, 15, 6, 0, 123456),
])
+ def CheckInvalidIsolationLevelType(self):
+ # isolation level is a string, not an integer
+ self.assertRaises(TypeError,
+ sqlite.connect, ":memory:", isolation_level=123)
+
def suite():
regression_suite = unittest.makeSuite(RegressionTests, "Check")
diff --git a/Lib/sqlite3/test/transactions.py b/Lib/sqlite3/test/transactions.py
index 70e96a1..feb4fa1 100644
--- a/Lib/sqlite3/test/transactions.py
+++ b/Lib/sqlite3/test/transactions.py
@@ -1,4 +1,4 @@
-#-*- coding: ISO-8859-1 -*-
+#-*- coding: iso-8859-1 -*-
# pysqlite2/test/transactions.py: tests transactions
#
# Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
diff --git a/Lib/sqlite3/test/types.py b/Lib/sqlite3/test/types.py
index 29413e1..a8fdad9 100644
--- a/Lib/sqlite3/test/types.py
+++ b/Lib/sqlite3/test/types.py
@@ -1,4 +1,4 @@
-#-*- coding: ISO-8859-1 -*-
+#-*- coding: iso-8859-1 -*-
# pysqlite2/test/types.py: tests for type conversion and detection
#
# Copyright (C) 2005 Gerhard Häring <gh@ghaering.de>
@@ -85,7 +85,7 @@ class DeclTypesTests(unittest.TestCase):
if isinstance(_val, bytes):
# sqlite3 always calls __init__ with a bytes created from a
# UTF-8 string when __conform__ was used to store the object.
- _val = _val.decode('utf8')
+ _val = _val.decode('utf-8')
self.val = _val
def __cmp__(self, other):
@@ -229,7 +229,7 @@ class DeclTypesTests(unittest.TestCase):
self.assertEqual(type(value), float)
def CheckNumber2(self):
- """Checks wether converter names are cut off at '(' characters"""
+ """Checks whether converter names are cut off at '(' characters"""
self.cur.execute("insert into test(n2) values (5)")
value = self.cur.execute("select n2 from test").fetchone()[0]
# if the converter is not used, it's an int instead of a float
diff --git a/Lib/sqlite3/test/userfunctions.py b/Lib/sqlite3/test/userfunctions.py
index 9a6a828..69e2ec2 100644
--- a/Lib/sqlite3/test/userfunctions.py
+++ b/Lib/sqlite3/test/userfunctions.py
@@ -1,4 +1,4 @@
-#-*- coding: ISO-8859-1 -*-
+#-*- coding: iso-8859-1 -*-
# pysqlite2/test/userfunctions.py: tests for user-defined functions and
# aggregates.
#