annotate Lib/sqlite3/test/factory.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 93ac4b12a750
children d004ccdd6a57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
1 #-*- coding: ISO-8859-1 -*-
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
2 # pysqlite2/test/factory.py: tests for the various factories in pysqlite
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
3 #
46166
a21854e510b4 Update sqlite3 module to match current version of pysqlite.
Gerhard Häring <gh@ghaering.de>
parents: 40625
diff changeset
4 # Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
5 #
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
6 # This file is part of pysqlite.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
7 #
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
8 # This software is provided 'as-is', without any express or implied
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
9 # warranty. In no event will the authors be held liable for any damages
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
10 # arising from the use of this software.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
11 #
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
12 # Permission is granted to anyone to use this software for any purpose,
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
13 # including commercial applications, and to alter it and redistribute it
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
14 # freely, subject to the following restrictions:
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
15 #
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
16 # 1. The origin of this software must not be misrepresented; you must not
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
17 # claim that you wrote the original software. If you use this software
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
18 # in a product, an acknowledgment in the product documentation would be
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
19 # appreciated but is not required.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
20 # 2. Altered source versions must be plainly marked as such, and must not be
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
21 # misrepresented as being the original software.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
22 # 3. This notice may not be removed or altered from any source distribution.
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
23
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
24 import unittest
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
25 import sqlite3 as sqlite
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
26
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
27 class MyConnection(sqlite.Connection):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
28 def __init__(self, *args, **kwargs):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
29 sqlite.Connection.__init__(self, *args, **kwargs)
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
30
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
31 def dict_factory(cursor, row):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
32 d = {}
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
33 for idx, col in enumerate(cursor.description):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
34 d[col[0]] = row[idx]
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
35 return d
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
36
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
37 class MyCursor(sqlite.Cursor):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
38 def __init__(self, *args, **kwargs):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
39 sqlite.Cursor.__init__(self, *args, **kwargs)
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
40 self.row_factory = dict_factory
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
41
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
42 class ConnectionFactoryTests(unittest.TestCase):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
43 def setUp(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
44 self.con = sqlite.connect(":memory:", factory=MyConnection)
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
45
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
46 def tearDown(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
47 self.con.close()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
48
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
49 def CheckIsInstance(self):
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
50 self.assertTrue(isinstance(self.con,
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
51 MyConnection),
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
52 "connection is not instance of MyConnection")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
53
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
54 class CursorFactoryTests(unittest.TestCase):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
55 def setUp(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
56 self.con = sqlite.connect(":memory:")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
57
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
58 def tearDown(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
59 self.con.close()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
60
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
61 def CheckIsInstance(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
62 cur = self.con.cursor(factory=MyCursor)
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
63 self.assertTrue(isinstance(cur,
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
64 MyCursor),
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
65 "cursor is not instance of MyCursor")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
66
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
67 class RowFactoryTestsBackwardsCompat(unittest.TestCase):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
68 def setUp(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
69 self.con = sqlite.connect(":memory:")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
70
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
71 def CheckIsProducedByFactory(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
72 cur = self.con.cursor(factory=MyCursor)
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
73 cur.execute("select 4+5 as foo")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
74 row = cur.fetchone()
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
75 self.assertTrue(isinstance(row,
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
76 dict),
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
77 "row is not instance of dict")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
78 cur.close()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
79
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
80 def tearDown(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
81 self.con.close()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
82
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
83 class RowFactoryTests(unittest.TestCase):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
84 def setUp(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
85 self.con = sqlite.connect(":memory:")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
86
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
87 def CheckCustomFactory(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
88 self.con.row_factory = lambda cur, row: list(row)
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
89 row = self.con.execute("select 1, 2").fetchone()
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
90 self.assertTrue(isinstance(row,
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
91 list),
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
92 "row is not instance of list")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
93
40625
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
94 def CheckSqliteRowIndex(self):
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
95 self.con.row_factory = sqlite.Row
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
96 row = self.con.execute("select 1 as a, 2 as b").fetchone()
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
97 self.assertTrue(isinstance(row,
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
98 sqlite.Row),
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
99 "row is not instance of sqlite.Row")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
100
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
101 col1, col2 = row["a"], row["b"]
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
102 self.assertTrue(col1 == 1, "by name: wrong result for column 'a'")
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
103 self.assertTrue(col2 == 2, "by name: wrong result for column 'a'")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
104
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
105 col1, col2 = row["A"], row["B"]
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
106 self.assertTrue(col1 == 1, "by name: wrong result for column 'A'")
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
107 self.assertTrue(col2 == 2, "by name: wrong result for column 'B'")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
108
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
109 col1, col2 = row[0], row[1]
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
110 self.assertTrue(col1 == 1, "by index: wrong result for column 0")
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
111 self.assertTrue(col2 == 2, "by index: wrong result for column 1")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
112
40625
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
113 def CheckSqliteRowIter(self):
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
114 """Checks if the row object is iterable"""
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
115 self.con.row_factory = sqlite.Row
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
116 row = self.con.execute("select 1 as a, 2 as b").fetchone()
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
117 for col in row:
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
118 pass
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
119
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
120 def CheckSqliteRowAsTuple(self):
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
121 """Checks if the row object can be converted to a tuple"""
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
122 self.con.row_factory = sqlite.Row
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
123 row = self.con.execute("select 1 as a, 2 as b").fetchone()
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
124 t = tuple(row)
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
125
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
126 def CheckSqliteRowAsDict(self):
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
127 """Checks if the row object can be correctly converted to a dictionary"""
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
128 self.con.row_factory = sqlite.Row
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
129 row = self.con.execute("select 1 as a, 2 as b").fetchone()
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
130 d = dict(row)
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
131 self.assertEqual(d["a"], row["a"])
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
132 self.assertEqual(d["b"], row["b"])
40625
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
133
46658
b93a041cc221 Applied sqliterow-richcmp.diff patch from Thomas Heller in Issue2152. The
Gerhard Häring <gh@ghaering.de>
parents: 46166
diff changeset
134 def CheckSqliteRowHashCmp(self):
b93a041cc221 Applied sqliterow-richcmp.diff patch from Thomas Heller in Issue2152. The
Gerhard Häring <gh@ghaering.de>
parents: 46166
diff changeset
135 """Checks if the row object compares and hashes correctly"""
b93a041cc221 Applied sqliterow-richcmp.diff patch from Thomas Heller in Issue2152. The
Gerhard Häring <gh@ghaering.de>
parents: 46166
diff changeset
136 self.con.row_factory = sqlite.Row
b93a041cc221 Applied sqliterow-richcmp.diff patch from Thomas Heller in Issue2152. The
Gerhard Häring <gh@ghaering.de>
parents: 46166
diff changeset
137 row_1 = self.con.execute("select 1 as a, 2 as b").fetchone()
b93a041cc221 Applied sqliterow-richcmp.diff patch from Thomas Heller in Issue2152. The
Gerhard Häring <gh@ghaering.de>
parents: 46166
diff changeset
138 row_2 = self.con.execute("select 1 as a, 2 as b").fetchone()
b93a041cc221 Applied sqliterow-richcmp.diff patch from Thomas Heller in Issue2152. The
Gerhard Häring <gh@ghaering.de>
parents: 46166
diff changeset
139 row_3 = self.con.execute("select 1 as a, 3 as b").fetchone()
b93a041cc221 Applied sqliterow-richcmp.diff patch from Thomas Heller in Issue2152. The
Gerhard Häring <gh@ghaering.de>
parents: 46166
diff changeset
140
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
141 self.assertTrue(row_1 == row_1)
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
142 self.assertTrue(row_1 == row_2)
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
143 self.assertTrue(row_2 != row_3)
46658
b93a041cc221 Applied sqliterow-richcmp.diff patch from Thomas Heller in Issue2152. The
Gerhard Häring <gh@ghaering.de>
parents: 46166
diff changeset
144
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
145 self.assertFalse(row_1 != row_1)
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
146 self.assertFalse(row_1 != row_2)
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
147 self.assertFalse(row_2 == row_3)
46658
b93a041cc221 Applied sqliterow-richcmp.diff patch from Thomas Heller in Issue2152. The
Gerhard Häring <gh@ghaering.de>
parents: 46166
diff changeset
148
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
149 self.assertEqual(row_1, row_2)
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
150 self.assertEqual(hash(row_1), hash(row_2))
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
151 self.assertNotEqual(row_1, row_3)
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
152 self.assertNotEqual(hash(row_1), hash(row_3))
46658
b93a041cc221 Applied sqliterow-richcmp.diff patch from Thomas Heller in Issue2152. The
Gerhard Häring <gh@ghaering.de>
parents: 46166
diff changeset
153
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
154 def tearDown(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
155 self.con.close()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
156
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
157 class TextFactoryTests(unittest.TestCase):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
158 def setUp(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
159 self.con = sqlite.connect(":memory:")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
160
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
161 def CheckUnicode(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
162 austria = unicode("Österreich", "latin1")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
163 row = self.con.execute("select ?", (austria,)).fetchone()
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
164 self.assertTrue(type(row[0]) == unicode, "type of row[0] must be unicode")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
165
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
166 def CheckString(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
167 self.con.text_factory = str
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
168 austria = unicode("Österreich", "latin1")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
169 row = self.con.execute("select ?", (austria,)).fetchone()
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
170 self.assertTrue(type(row[0]) == str, "type of row[0] must be str")
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
171 self.assertTrue(row[0] == austria.encode("utf-8"), "column must equal original data in UTF-8")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
172
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
173 def CheckCustom(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
174 self.con.text_factory = lambda x: unicode(x, "utf-8", "ignore")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
175 austria = unicode("Österreich", "latin1")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
176 row = self.con.execute("select ?", (austria.encode("latin1"),)).fetchone()
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
177 self.assertTrue(type(row[0]) == unicode, "type of row[0] must be unicode")
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
178 self.assertTrue(row[0].endswith(u"reich"), "column must contain original data")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
179
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
180 def CheckOptimizedUnicode(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
181 self.con.text_factory = sqlite.OptimizedUnicode
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
182 austria = unicode("Österreich", "latin1")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
183 germany = unicode("Deutchland")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
184 a_row = self.con.execute("select ?", (austria,)).fetchone()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
185 d_row = self.con.execute("select ?", (germany,)).fetchone()
55106
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
186 self.assertTrue(type(a_row[0]) == unicode, "type of non-ASCII row must be unicode")
7ff1405ccb64 Merge r73838 from py3k branch. Use the nondeprecated unittest method
Gregory P. Smith <greg@mad-scientist.com>
parents: 46658
diff changeset
187 self.assertTrue(type(d_row[0]) == str, "type of ASCII-only row must be str")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
188
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
189 def tearDown(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
190 self.con.close()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
191
74709
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
192 class TextFactoryTestsWithEmbeddedZeroBytes(unittest.TestCase):
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
193 def setUp(self):
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
194 self.con = sqlite.connect(":memory:")
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
195 self.con.execute("create table test (value text)")
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
196 self.con.execute("insert into test (value) values (?)", ("a\x00b",))
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
197
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
198 def CheckString(self):
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
199 # text_factory defaults to unicode
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
200 row = self.con.execute("select value from test").fetchone()
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
201 self.assertIs(type(row[0]), unicode)
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
202 self.assertEqual(row[0], "a\x00b")
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
203
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
204 def CheckCustom(self):
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
205 # A custom factory should receive an str argument
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
206 self.con.text_factory = lambda x: x
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
207 row = self.con.execute("select value from test").fetchone()
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
208 self.assertIs(type(row[0]), str)
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
209 self.assertEqual(row[0], "a\x00b")
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
210
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
211 def CheckOptimizedUnicodeAsString(self):
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
212 # ASCII -> str argument
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
213 self.con.text_factory = sqlite.OptimizedUnicode
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
214 row = self.con.execute("select value from test").fetchone()
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
215 self.assertIs(type(row[0]), str)
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
216 self.assertEqual(row[0], "a\x00b")
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
217
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
218 def CheckOptimizedUnicodeAsUnicode(self):
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
219 # Non-ASCII -> unicode argument
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
220 self.con.text_factory = sqlite.OptimizedUnicode
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
221 self.con.execute("delete from test")
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
222 self.con.execute("insert into test (value) values (?)", (u'ä\0ö',))
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
223 row = self.con.execute("select value from test").fetchone()
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
224 self.assertIs(type(row[0]), unicode)
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
225 self.assertEqual(row[0], u"ä\x00ö")
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
226
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
227 def tearDown(self):
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
228 self.con.close()
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
229
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
230 def suite():
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
231 connection_suite = unittest.makeSuite(ConnectionFactoryTests, "Check")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
232 cursor_suite = unittest.makeSuite(CursorFactoryTests, "Check")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
233 row_suite_compat = unittest.makeSuite(RowFactoryTestsBackwardsCompat, "Check")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
234 row_suite = unittest.makeSuite(RowFactoryTests, "Check")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
235 text_suite = unittest.makeSuite(TextFactoryTests, "Check")
74709
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
236 text_zero_bytes_suite = unittest.makeSuite(TextFactoryTestsWithEmbeddedZeroBytes, "Check")
93ac4b12a750 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55106
diff changeset
237 return unittest.TestSuite((connection_suite, cursor_suite, row_suite_compat, row_suite, text_suite, text_zero_bytes_suite))
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
238
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
239 def test():
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
240 runner = unittest.TextTestRunner()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
241 runner.run(suite())
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
242
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
243 if __name__ == "__main__":
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
244 test()