»Core Development>Code coverage>Lib/sqlite3/test/hooks.py

Python code coverage for Lib/sqlite3/test/hooks.py

1n/a#-*- coding: iso-8859-1 -*-
2n/a# pysqlite2/test/hooks.py: tests for various SQLite-specific hooks
4n/a# Copyright (C) 2006-2007 Gerhard Häring <gh@ghaering.de>
6n/a# This file is part of pysqlite.
8n/a# This software is provided 'as-is', without any express or implied
9n/a# warranty. In no event will the authors be held liable for any damages
10n/a# arising from the use of this software.
12n/a# Permission is granted to anyone to use this software for any purpose,
13n/a# including commercial applications, and to alter it and redistribute it
14n/a# freely, subject to the following restrictions:
16n/a# 1. The origin of this software must not be misrepresented; you must not
17n/a# claim that you wrote the original software. If you use this software
18n/a# in a product, an acknowledgment in the product documentation would be
19n/a# appreciated but is not required.
20n/a# 2. Altered source versions must be plainly marked as such, and must not be
21n/a# misrepresented as being the original software.
22n/a# 3. This notice may not be removed or altered from any source distribution.
24n/aimport unittest
25n/aimport sqlite3 as sqlite
27n/aclass CollationTests(unittest.TestCase):
28n/a def CheckCreateCollationNotString(self):
29n/a con = sqlite.connect(":memory:")
30n/a with self.assertRaises(TypeError):
31n/a con.create_collation(None, lambda x, y: (x > y) - (x < y))
33n/a def CheckCreateCollationNotCallable(self):
34n/a con = sqlite.connect(":memory:")
35n/a with self.assertRaises(TypeError) as cm:
36n/a con.create_collation("X", 42)
37n/a self.assertEqual(str(cm.exception), 'parameter must be callable')
39n/a def CheckCreateCollationNotAscii(self):
40n/a con = sqlite.connect(":memory:")
41n/a with self.assertRaises(sqlite.ProgrammingError):
42n/a con.create_collation("collä", lambda x, y: (x > y) - (x < y))
44n/a def CheckCreateCollationBadUpper(self):
45n/a class BadUpperStr(str):
46n/a def upper(self):
47n/a return None
48n/a con = sqlite.connect(":memory:")
49n/a mycoll = lambda x, y: -((x > y) - (x < y))
50n/a con.create_collation(BadUpperStr("mycoll"), mycoll)
51n/a result = con.execute("""
52n/a select x from (
53n/a select 'a' as x
54n/a union
55n/a select 'b' as x
56n/a ) order by x collate mycoll
57n/a """).fetchall()
58n/a self.assertEqual(result[0][0], 'b')
59n/a self.assertEqual(result[1][0], 'a')
61n/a @unittest.skipIf(sqlite.sqlite_version_info < (3, 2, 1),
62n/a 'old SQLite versions crash on this test')
63n/a def CheckCollationIsUsed(self):
64n/a def mycoll(x, y):
65n/a # reverse order
66n/a return -((x > y) - (x < y))
68n/a con = sqlite.connect(":memory:")
69n/a con.create_collation("mycoll", mycoll)
70n/a sql = """
71n/a select x from (
72n/a select 'a' as x
73n/a union
74n/a select 'b' as x
75n/a union
76n/a select 'c' as x
77n/a ) order by x collate mycoll
78n/a """
79n/a result = con.execute(sql).fetchall()
80n/a self.assertEqual(result, [('c',), ('b',), ('a',)],
81n/a msg='the expected order was not returned')
83n/a con.create_collation("mycoll", None)
84n/a with self.assertRaises(sqlite.OperationalError) as cm:
85n/a result = con.execute(sql).fetchall()
86n/a self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll')
88n/a def CheckCollationReturnsLargeInteger(self):
89n/a def mycoll(x, y):
90n/a # reverse order
91n/a return -((x > y) - (x < y)) * 2**32
92n/a con = sqlite.connect(":memory:")
93n/a con.create_collation("mycoll", mycoll)
94n/a sql = """
95n/a select x from (
96n/a select 'a' as x
97n/a union
98n/a select 'b' as x
99n/a union
100n/a select 'c' as x
101n/a ) order by x collate mycoll
102n/a """
103n/a result = con.execute(sql).fetchall()
104n/a self.assertEqual(result, [('c',), ('b',), ('a',)],
105n/a msg="the expected order was not returned")
107n/a def CheckCollationRegisterTwice(self):
108n/a """
109n/a Register two different collation functions under the same name.
110n/a Verify that the last one is actually used.
111n/a """
112n/a con = sqlite.connect(":memory:")
113n/a con.create_collation("mycoll", lambda x, y: (x > y) - (x < y))
114n/a con.create_collation("mycoll", lambda x, y: -((x > y) - (x < y)))
115n/a result = con.execute("""
116n/a select x from (select 'a' as x union select 'b' as x) order by x collate mycoll
117n/a """).fetchall()
118n/a self.assertEqual(result[0][0], 'b')
119n/a self.assertEqual(result[1][0], 'a')
121n/a def CheckDeregisterCollation(self):
122n/a """
123n/a Register a collation, then deregister it. Make sure an error is raised if we try
124n/a to use it.
125n/a """
126n/a con = sqlite.connect(":memory:")
127n/a con.create_collation("mycoll", lambda x, y: (x > y) - (x < y))
128n/a con.create_collation("mycoll", None)
129n/a with self.assertRaises(sqlite.OperationalError) as cm:
130n/a con.execute("select 'a' as x union select 'b' as x order by x collate mycoll")
131n/a self.assertEqual(str(cm.exception), 'no such collation sequence: mycoll')
133n/aclass ProgressTests(unittest.TestCase):
134n/a def CheckProgressHandlerUsed(self):
135n/a """
136n/a Test that the progress handler is invoked once it is set.
137n/a """
138n/a con = sqlite.connect(":memory:")
139n/a progress_calls = []
140n/a def progress():
141n/a progress_calls.append(None)
142n/a return 0
143n/a con.set_progress_handler(progress, 1)
144n/a con.execute("""
145n/a create table foo(a, b)
146n/a """)
147n/a self.assertTrue(progress_calls)
150n/a def CheckOpcodeCount(self):
151n/a """
152n/a Test that the opcode argument is respected.
153n/a """
154n/a con = sqlite.connect(":memory:")
155n/a progress_calls = []
156n/a def progress():
157n/a progress_calls.append(None)
158n/a return 0
159n/a con.set_progress_handler(progress, 1)
160n/a curs = con.cursor()
161n/a curs.execute("""
162n/a create table foo (a, b)
163n/a """)
164n/a first_count = len(progress_calls)
165n/a progress_calls = []
166n/a con.set_progress_handler(progress, 2)
167n/a curs.execute("""
168n/a create table bar (a, b)
169n/a """)
170n/a second_count = len(progress_calls)
171n/a self.assertGreaterEqual(first_count, second_count)
173n/a def CheckCancelOperation(self):
174n/a """
175n/a Test that returning a non-zero value stops the operation in progress.
176n/a """
177n/a con = sqlite.connect(":memory:")
178n/a progress_calls = []
179n/a def progress():
180n/a progress_calls.append(None)
181n/a return 1
182n/a con.set_progress_handler(progress, 1)
183n/a curs = con.cursor()
184n/a self.assertRaises(
185n/a sqlite.OperationalError,
186n/a curs.execute,
187n/a "create table bar (a, b)")
189n/a def CheckClearHandler(self):
190n/a """
191n/a Test that setting the progress handler to None clears the previously set handler.
192n/a """
193n/a con = sqlite.connect(":memory:")
194n/a action = 0
195n/a def progress():
196n/a nonlocal action
197n/a action = 1
198n/a return 0
199n/a con.set_progress_handler(progress, 1)
200n/a con.set_progress_handler(None, 1)
201n/a con.execute("select 1 union select 2 union select 3").fetchall()
202n/a self.assertEqual(action, 0, "progress handler was not cleared")
204n/aclass TraceCallbackTests(unittest.TestCase):
205n/a def CheckTraceCallbackUsed(self):
206n/a """
207n/a Test that the trace callback is invoked once it is set.
208n/a """
209n/a con = sqlite.connect(":memory:")
210n/a traced_statements = []
211n/a def trace(statement):
212n/a traced_statements.append(statement)
213n/a con.set_trace_callback(trace)
214n/a con.execute("create table foo(a, b)")
215n/a self.assertTrue(traced_statements)
216n/a self.assertTrue(any("create table foo" in stmt for stmt in traced_statements))
218n/a def CheckClearTraceCallback(self):
219n/a """
220n/a Test that setting the trace callback to None clears the previously set callback.
221n/a """
222n/a con = sqlite.connect(":memory:")
223n/a traced_statements = []
224n/a def trace(statement):
225n/a traced_statements.append(statement)
226n/a con.set_trace_callback(trace)
227n/a con.set_trace_callback(None)
228n/a con.execute("create table foo(a, b)")
229n/a self.assertFalse(traced_statements, "trace callback was not cleared")
231n/a def CheckUnicodeContent(self):
232n/a """
233n/a Test that the statement can contain unicode literals.
234n/a """
235n/a unicode_value = '\xf6\xe4\xfc\xd6\xc4\xdc\xdf\u20ac'
236n/a con = sqlite.connect(":memory:")
237n/a traced_statements = []
238n/a def trace(statement):
239n/a traced_statements.append(statement)
240n/a con.set_trace_callback(trace)
241n/a con.execute("create table foo(x)")
242n/a # Can't execute bound parameters as their values don't appear
243n/a # in traced statements before SQLite 3.6.21
244n/a # (cf. http://www.sqlite.org/draft/releaselog/3_6_21.html)
245n/a con.execute('insert into foo(x) values ("%s")' % unicode_value)
246n/a con.commit()
247n/a self.assertTrue(any(unicode_value in stmt for stmt in traced_statements),
248n/a "Unicode data %s garbled in trace callback: %s"
249n/a % (ascii(unicode_value), ', '.join(map(ascii, traced_statements))))
253n/adef suite():
254n/a collation_suite = unittest.makeSuite(CollationTests, "Check")
255n/a progress_suite = unittest.makeSuite(ProgressTests, "Check")
256n/a trace_suite = unittest.makeSuite(TraceCallbackTests, "Check")
257n/a return unittest.TestSuite((collation_suite, progress_suite, trace_suite))
259n/adef test():
260n/a runner = unittest.TextTestRunner()
261n/a runner.run(suite())
263n/aif __name__ == "__main__":
264n/a test()