Petri Lehtinen | f854799 | 2012-02-02 17:17:36 +0200 | [diff] [blame] | 1 | #-*- coding: iso-8859-1 -*- |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2 | # pysqlite2/test/factory.py: tests for the various factories in pysqlite |
| 3 | # |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 4 | # Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de> |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5 | # |
| 6 | # This file is part of pysqlite. |
| 7 | # |
| 8 | # This software is provided 'as-is', without any express or implied |
| 9 | # warranty. In no event will the authors be held liable for any damages |
| 10 | # arising from the use of this software. |
| 11 | # |
| 12 | # Permission is granted to anyone to use this software for any purpose, |
| 13 | # including commercial applications, and to alter it and redistribute it |
| 14 | # freely, subject to the following restrictions: |
| 15 | # |
| 16 | # 1. The origin of this software must not be misrepresented; you must not |
| 17 | # claim that you wrote the original software. If you use this software |
| 18 | # in a product, an acknowledgment in the product documentation would be |
| 19 | # appreciated but is not required. |
| 20 | # 2. Altered source versions must be plainly marked as such, and must not be |
| 21 | # misrepresented as being the original software. |
| 22 | # 3. This notice may not be removed or altered from any source distribution. |
| 23 | |
| 24 | import unittest |
| 25 | import sqlite3 as sqlite |
Serhiy Storchaka | 47a9813 | 2014-05-28 12:58:34 +0300 | [diff] [blame] | 26 | from collections.abc import Sequence |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 27 | |
| 28 | class MyConnection(sqlite.Connection): |
| 29 | def __init__(self, *args, **kwargs): |
| 30 | sqlite.Connection.__init__(self, *args, **kwargs) |
| 31 | |
| 32 | def dict_factory(cursor, row): |
| 33 | d = {} |
| 34 | for idx, col in enumerate(cursor.description): |
| 35 | d[col[0]] = row[idx] |
| 36 | return d |
| 37 | |
| 38 | class MyCursor(sqlite.Cursor): |
| 39 | def __init__(self, *args, **kwargs): |
| 40 | sqlite.Cursor.__init__(self, *args, **kwargs) |
| 41 | self.row_factory = dict_factory |
| 42 | |
| 43 | class ConnectionFactoryTests(unittest.TestCase): |
| 44 | def setUp(self): |
| 45 | self.con = sqlite.connect(":memory:", factory=MyConnection) |
| 46 | |
| 47 | def tearDown(self): |
| 48 | self.con.close() |
| 49 | |
| 50 | def CheckIsInstance(self): |
Serhiy Storchaka | 78ee078 | 2013-11-17 00:39:12 +0200 | [diff] [blame] | 51 | self.assertIsInstance(self.con, MyConnection) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 52 | |
| 53 | class CursorFactoryTests(unittest.TestCase): |
| 54 | def setUp(self): |
| 55 | self.con = sqlite.connect(":memory:") |
| 56 | |
| 57 | def tearDown(self): |
| 58 | self.con.close() |
| 59 | |
| 60 | def CheckIsInstance(self): |
| 61 | cur = self.con.cursor(factory=MyCursor) |
Serhiy Storchaka | 78ee078 | 2013-11-17 00:39:12 +0200 | [diff] [blame] | 62 | self.assertIsInstance(cur, MyCursor) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 63 | |
| 64 | class RowFactoryTestsBackwardsCompat(unittest.TestCase): |
| 65 | def setUp(self): |
| 66 | self.con = sqlite.connect(":memory:") |
| 67 | |
| 68 | def CheckIsProducedByFactory(self): |
| 69 | cur = self.con.cursor(factory=MyCursor) |
| 70 | cur.execute("select 4+5 as foo") |
| 71 | row = cur.fetchone() |
Serhiy Storchaka | 78ee078 | 2013-11-17 00:39:12 +0200 | [diff] [blame] | 72 | self.assertIsInstance(row, dict) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 73 | cur.close() |
| 74 | |
| 75 | def tearDown(self): |
| 76 | self.con.close() |
| 77 | |
| 78 | class RowFactoryTests(unittest.TestCase): |
| 79 | def setUp(self): |
| 80 | self.con = sqlite.connect(":memory:") |
| 81 | |
| 82 | def CheckCustomFactory(self): |
| 83 | self.con.row_factory = lambda cur, row: list(row) |
| 84 | row = self.con.execute("select 1, 2").fetchone() |
Serhiy Storchaka | 78ee078 | 2013-11-17 00:39:12 +0200 | [diff] [blame] | 85 | self.assertIsInstance(row, list) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 86 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 87 | def CheckSqliteRowIndex(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 88 | self.con.row_factory = sqlite.Row |
| 89 | row = self.con.execute("select 1 as a, 2 as b").fetchone() |
Serhiy Storchaka | 78ee078 | 2013-11-17 00:39:12 +0200 | [diff] [blame] | 90 | self.assertIsInstance(row, sqlite.Row) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 91 | |
| 92 | col1, col2 = row["a"], row["b"] |
Serhiy Storchaka | 78ee078 | 2013-11-17 00:39:12 +0200 | [diff] [blame] | 93 | self.assertEqual(col1, 1, "by name: wrong result for column 'a'") |
| 94 | self.assertEqual(col2, 2, "by name: wrong result for column 'a'") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 95 | |
| 96 | col1, col2 = row["A"], row["B"] |
Serhiy Storchaka | 78ee078 | 2013-11-17 00:39:12 +0200 | [diff] [blame] | 97 | self.assertEqual(col1, 1, "by name: wrong result for column 'A'") |
| 98 | self.assertEqual(col2, 2, "by name: wrong result for column 'B'") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 99 | |
Serhiy Storchaka | 47a9813 | 2014-05-28 12:58:34 +0300 | [diff] [blame] | 100 | self.assertEqual(row[0], 1, "by index: wrong result for column 0") |
| 101 | self.assertEqual(row[1], 2, "by index: wrong result for column 1") |
| 102 | self.assertEqual(row[-1], 2, "by index: wrong result for column -1") |
| 103 | self.assertEqual(row[-2], 1, "by index: wrong result for column -2") |
| 104 | |
| 105 | with self.assertRaises(IndexError): |
| 106 | row['c'] |
| 107 | with self.assertRaises(IndexError): |
| 108 | row[2] |
| 109 | with self.assertRaises(IndexError): |
| 110 | row[-3] |
| 111 | with self.assertRaises(IndexError): |
| 112 | row[2**1000] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 113 | |
Serhiy Storchaka | 72e731c | 2015-03-31 13:33:11 +0300 | [diff] [blame^] | 114 | def CheckSqliteRowSlice(self): |
| 115 | # A sqlite.Row can be sliced like a list. |
| 116 | self.con.row_factory = sqlite.Row |
| 117 | row = self.con.execute("select 1, 2, 3, 4").fetchone() |
| 118 | self.assertEqual(row[0:0], ()) |
| 119 | self.assertEqual(row[0:1], (1,)) |
| 120 | self.assertEqual(row[1:3], (2, 3)) |
| 121 | self.assertEqual(row[3:1], ()) |
| 122 | # Explicit bounds are optional. |
| 123 | self.assertEqual(row[1:], (2, 3, 4)) |
| 124 | self.assertEqual(row[:3], (1, 2, 3)) |
| 125 | # Slices can use negative indices. |
| 126 | self.assertEqual(row[-2:-1], (3,)) |
| 127 | self.assertEqual(row[-2:], (3, 4)) |
| 128 | # Slicing supports steps. |
| 129 | self.assertEqual(row[0:4:2], (1, 3)) |
| 130 | self.assertEqual(row[3:0:-2], (4, 2)) |
| 131 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 132 | def CheckSqliteRowIter(self): |
| 133 | """Checks if the row object is iterable""" |
| 134 | self.con.row_factory = sqlite.Row |
| 135 | row = self.con.execute("select 1 as a, 2 as b").fetchone() |
| 136 | for col in row: |
| 137 | pass |
| 138 | |
| 139 | def CheckSqliteRowAsTuple(self): |
| 140 | """Checks if the row object can be converted to a tuple""" |
| 141 | self.con.row_factory = sqlite.Row |
| 142 | row = self.con.execute("select 1 as a, 2 as b").fetchone() |
| 143 | t = tuple(row) |
Benjamin Peterson | 29352c4 | 2014-02-15 13:19:59 -0500 | [diff] [blame] | 144 | self.assertEqual(t, (row['a'], row['b'])) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 145 | |
| 146 | def CheckSqliteRowAsDict(self): |
| 147 | """Checks if the row object can be correctly converted to a dictionary""" |
| 148 | self.con.row_factory = sqlite.Row |
| 149 | row = self.con.execute("select 1 as a, 2 as b").fetchone() |
| 150 | d = dict(row) |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 151 | self.assertEqual(d["a"], row["a"]) |
| 152 | self.assertEqual(d["b"], row["b"]) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 153 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 154 | def CheckSqliteRowHashCmp(self): |
| 155 | """Checks if the row object compares and hashes correctly""" |
| 156 | self.con.row_factory = sqlite.Row |
| 157 | row_1 = self.con.execute("select 1 as a, 2 as b").fetchone() |
| 158 | row_2 = self.con.execute("select 1 as a, 2 as b").fetchone() |
| 159 | row_3 = self.con.execute("select 1 as a, 3 as b").fetchone() |
| 160 | |
Serhiy Storchaka | 78ee078 | 2013-11-17 00:39:12 +0200 | [diff] [blame] | 161 | self.assertEqual(row_1, row_1) |
| 162 | self.assertEqual(row_1, row_2) |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 163 | self.assertTrue(row_2 != row_3) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 164 | |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 165 | self.assertFalse(row_1 != row_1) |
| 166 | self.assertFalse(row_1 != row_2) |
| 167 | self.assertFalse(row_2 == row_3) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 168 | |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 169 | self.assertEqual(row_1, row_2) |
| 170 | self.assertEqual(hash(row_1), hash(row_2)) |
| 171 | self.assertNotEqual(row_1, row_3) |
| 172 | self.assertNotEqual(hash(row_1), hash(row_3)) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 173 | |
Serhiy Storchaka | 47a9813 | 2014-05-28 12:58:34 +0300 | [diff] [blame] | 174 | def CheckSqliteRowAsSequence(self): |
| 175 | """ Checks if the row object can act like a sequence """ |
| 176 | self.con.row_factory = sqlite.Row |
| 177 | row = self.con.execute("select 1 as a, 2 as b").fetchone() |
| 178 | |
| 179 | as_tuple = tuple(row) |
| 180 | self.assertEqual(list(reversed(row)), list(reversed(as_tuple))) |
| 181 | self.assertIsInstance(row, Sequence) |
| 182 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 183 | def tearDown(self): |
| 184 | self.con.close() |
| 185 | |
| 186 | class TextFactoryTests(unittest.TestCase): |
| 187 | def setUp(self): |
| 188 | self.con = sqlite.connect(":memory:") |
| 189 | |
| 190 | def CheckUnicode(self): |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 191 | austria = "Österreich" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 192 | row = self.con.execute("select ?", (austria,)).fetchone() |
Serhiy Storchaka | 78ee078 | 2013-11-17 00:39:12 +0200 | [diff] [blame] | 193 | self.assertEqual(type(row[0]), str, "type of row[0] must be unicode") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 194 | |
| 195 | def CheckString(self): |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 196 | self.con.text_factory = bytes |
| 197 | austria = "Österreich" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 198 | row = self.con.execute("select ?", (austria,)).fetchone() |
Serhiy Storchaka | 78ee078 | 2013-11-17 00:39:12 +0200 | [diff] [blame] | 199 | self.assertEqual(type(row[0]), bytes, "type of row[0] must be bytes") |
| 200 | self.assertEqual(row[0], austria.encode("utf-8"), "column must equal original data in UTF-8") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 201 | |
| 202 | def CheckCustom(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 203 | self.con.text_factory = lambda x: str(x, "utf-8", "ignore") |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 204 | austria = "Österreich" |
| 205 | row = self.con.execute("select ?", (austria,)).fetchone() |
Serhiy Storchaka | 78ee078 | 2013-11-17 00:39:12 +0200 | [diff] [blame] | 206 | self.assertEqual(type(row[0]), str, "type of row[0] must be unicode") |
Gregory P. Smith | 04cecaf | 2009-07-04 08:32:15 +0000 | [diff] [blame] | 207 | self.assertTrue(row[0].endswith("reich"), "column must contain original data") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 208 | |
| 209 | def CheckOptimizedUnicode(self): |
Petri Lehtinen | bc35beb | 2012-02-09 21:09:03 +0200 | [diff] [blame] | 210 | # In py3k, str objects are always returned when text_factory |
| 211 | # is OptimizedUnicode |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 212 | self.con.text_factory = sqlite.OptimizedUnicode |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 213 | austria = "Österreich" |
| 214 | germany = "Deutchland" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 215 | a_row = self.con.execute("select ?", (austria,)).fetchone() |
| 216 | d_row = self.con.execute("select ?", (germany,)).fetchone() |
Serhiy Storchaka | 78ee078 | 2013-11-17 00:39:12 +0200 | [diff] [blame] | 217 | self.assertEqual(type(a_row[0]), str, "type of non-ASCII row must be str") |
| 218 | self.assertEqual(type(d_row[0]), str, "type of ASCII-only row must be str") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 219 | |
| 220 | def tearDown(self): |
| 221 | self.con.close() |
| 222 | |
Petri Lehtinen | 023fe33 | 2012-02-01 22:18:19 +0200 | [diff] [blame] | 223 | class TextFactoryTestsWithEmbeddedZeroBytes(unittest.TestCase): |
| 224 | def setUp(self): |
| 225 | self.con = sqlite.connect(":memory:") |
| 226 | self.con.execute("create table test (value text)") |
| 227 | self.con.execute("insert into test (value) values (?)", ("a\x00b",)) |
| 228 | |
| 229 | def CheckString(self): |
| 230 | # text_factory defaults to str |
| 231 | row = self.con.execute("select value from test").fetchone() |
| 232 | self.assertIs(type(row[0]), str) |
| 233 | self.assertEqual(row[0], "a\x00b") |
| 234 | |
| 235 | def CheckBytes(self): |
| 236 | self.con.text_factory = bytes |
| 237 | row = self.con.execute("select value from test").fetchone() |
| 238 | self.assertIs(type(row[0]), bytes) |
| 239 | self.assertEqual(row[0], b"a\x00b") |
| 240 | |
| 241 | def CheckBytearray(self): |
| 242 | self.con.text_factory = bytearray |
| 243 | row = self.con.execute("select value from test").fetchone() |
| 244 | self.assertIs(type(row[0]), bytearray) |
| 245 | self.assertEqual(row[0], b"a\x00b") |
| 246 | |
| 247 | def CheckCustom(self): |
| 248 | # A custom factory should receive a bytes argument |
| 249 | self.con.text_factory = lambda x: x |
| 250 | row = self.con.execute("select value from test").fetchone() |
| 251 | self.assertIs(type(row[0]), bytes) |
| 252 | self.assertEqual(row[0], b"a\x00b") |
| 253 | |
| 254 | def tearDown(self): |
| 255 | self.con.close() |
| 256 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 257 | def suite(): |
| 258 | connection_suite = unittest.makeSuite(ConnectionFactoryTests, "Check") |
| 259 | cursor_suite = unittest.makeSuite(CursorFactoryTests, "Check") |
| 260 | row_suite_compat = unittest.makeSuite(RowFactoryTestsBackwardsCompat, "Check") |
| 261 | row_suite = unittest.makeSuite(RowFactoryTests, "Check") |
| 262 | text_suite = unittest.makeSuite(TextFactoryTests, "Check") |
Petri Lehtinen | 023fe33 | 2012-02-01 22:18:19 +0200 | [diff] [blame] | 263 | text_zero_bytes_suite = unittest.makeSuite(TextFactoryTestsWithEmbeddedZeroBytes, "Check") |
| 264 | return unittest.TestSuite((connection_suite, cursor_suite, row_suite_compat, row_suite, text_suite, text_zero_bytes_suite)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 265 | |
| 266 | def test(): |
| 267 | runner = unittest.TextTestRunner() |
| 268 | runner.run(suite()) |
| 269 | |
| 270 | if __name__ == "__main__": |
| 271 | test() |