annotate Lib/sqlite3/test/factory.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 981d161a52be
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
74726
d2ab2e4ce3ae Make sqlite3 tests editable with Emacs
Petri Lehtinen <petri@digip.org>
parents: 74708
diff changeset
1 #-*- coding: iso-8859-1 -*-
37362
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 #
46178
e06e08e8fdce Bring sqlite3 module up-to-date with what's now in 2.6. Almost. I intentionally
Gerhard Häring <gh@ghaering.de>
parents: 43777
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):
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
50 self.assertIsInstance(self.con, MyConnection)
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
51
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
52 class CursorFactoryTests(unittest.TestCase):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
53 def setUp(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
54 self.con = sqlite.connect(":memory:")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
55
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
56 def tearDown(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
57 self.con.close()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
58
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
59 def CheckIsInstance(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
60 cur = self.con.cursor(factory=MyCursor)
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
61 self.assertIsInstance(cur, MyCursor)
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
62
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
63 class RowFactoryTestsBackwardsCompat(unittest.TestCase):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
64 def setUp(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
65 self.con = sqlite.connect(":memory:")
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 def CheckIsProducedByFactory(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
68 cur = self.con.cursor(factory=MyCursor)
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
69 cur.execute("select 4+5 as foo")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
70 row = cur.fetchone()
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
71 self.assertIsInstance(row, dict)
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
72 cur.close()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
73
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
74 def tearDown(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
75 self.con.close()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
76
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
77 class RowFactoryTests(unittest.TestCase):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
78 def setUp(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
79 self.con = sqlite.connect(":memory:")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
80
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
81 def CheckCustomFactory(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
82 self.con.row_factory = lambda cur, row: list(row)
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
83 row = self.con.execute("select 1, 2").fetchone()
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
84 self.assertIsInstance(row, list)
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
85
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
86 def CheckSqliteRowIndex(self):
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
87 self.con.row_factory = sqlite.Row
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
88 row = self.con.execute("select 1 as a, 2 as b").fetchone()
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
89 self.assertIsInstance(row, sqlite.Row)
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
90
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
91 col1, col2 = row["a"], row["b"]
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
92 self.assertEqual(col1, 1, "by name: wrong result for column 'a'")
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
93 self.assertEqual(col2, 2, "by name: wrong result for column 'a'")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
94
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
95 col1, col2 = row["A"], row["B"]
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
96 self.assertEqual(col1, 1, "by name: wrong result for column 'A'")
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
97 self.assertEqual(col2, 2, "by name: wrong result for column 'B'")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
98
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
99 col1, col2 = row[0], row[1]
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
100 self.assertEqual(col1, 1, "by index: wrong result for column 0")
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
101 self.assertEqual(col2, 2, "by index: wrong result for column 1")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
102
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
103 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
104 """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
105 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
106 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
107 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
108 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
109
b9d4ced23506 Merged changes from standalone version 2.3.3. This should probably all be
Gerhard Häring <gh@ghaering.de>
parents: 37362
diff changeset
110 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
111 """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
112 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
113 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
114 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
115
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 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
117 """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
118 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
119 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
120 d = dict(row)
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46664
diff changeset
121 self.assertEqual(d["a"], row["a"])
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46664
diff changeset
122 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
123
46664
031dfe455585 Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46178
diff changeset
124 def CheckSqliteRowHashCmp(self):
031dfe455585 Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46178
diff changeset
125 """Checks if the row object compares and hashes correctly"""
031dfe455585 Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46178
diff changeset
126 self.con.row_factory = sqlite.Row
031dfe455585 Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46178
diff changeset
127 row_1 = self.con.execute("select 1 as a, 2 as b").fetchone()
031dfe455585 Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46178
diff changeset
128 row_2 = self.con.execute("select 1 as a, 2 as b").fetchone()
031dfe455585 Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46178
diff changeset
129 row_3 = self.con.execute("select 1 as a, 3 as b").fetchone()
031dfe455585 Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46178
diff changeset
130
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
131 self.assertEqual(row_1, row_1)
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
132 self.assertEqual(row_1, row_2)
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46664
diff changeset
133 self.assertTrue(row_2 != row_3)
46664
031dfe455585 Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46178
diff changeset
134
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46664
diff changeset
135 self.assertFalse(row_1 != row_1)
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46664
diff changeset
136 self.assertFalse(row_1 != row_2)
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46664
diff changeset
137 self.assertFalse(row_2 == row_3)
46664
031dfe455585 Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46178
diff changeset
138
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46664
diff changeset
139 self.assertEqual(row_1, row_2)
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46664
diff changeset
140 self.assertEqual(hash(row_1), hash(row_2))
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46664
diff changeset
141 self.assertNotEqual(row_1, row_3)
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46664
diff changeset
142 self.assertNotEqual(hash(row_1), hash(row_3))
46664
031dfe455585 Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46178
diff changeset
143
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
144 def tearDown(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
145 self.con.close()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
146
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
147 class TextFactoryTests(unittest.TestCase):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
148 def setUp(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
149 self.con = sqlite.connect(":memory:")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
150
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
151 def CheckUnicode(self):
42525
e64286053b25 Make the sqlite tests pass.
Gerhard Häring <gh@ghaering.de>
parents: 41575
diff changeset
152 austria = "Österreich"
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
153 row = self.con.execute("select ?", (austria,)).fetchone()
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
154 self.assertEqual(type(row[0]), str, "type of row[0] must be unicode")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
155
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
156 def CheckString(self):
42525
e64286053b25 Make the sqlite tests pass.
Gerhard Häring <gh@ghaering.de>
parents: 41575
diff changeset
157 self.con.text_factory = bytes
e64286053b25 Make the sqlite tests pass.
Gerhard Häring <gh@ghaering.de>
parents: 41575
diff changeset
158 austria = "Österreich"
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
159 row = self.con.execute("select ?", (austria,)).fetchone()
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
160 self.assertEqual(type(row[0]), bytes, "type of row[0] must be bytes")
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
161 self.assertEqual(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
162
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
163 def CheckCustom(self):
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 40625
diff changeset
164 self.con.text_factory = lambda x: str(x, "utf-8", "ignore")
42525
e64286053b25 Make the sqlite tests pass.
Gerhard Häring <gh@ghaering.de>
parents: 41575
diff changeset
165 austria = "Österreich"
e64286053b25 Make the sqlite tests pass.
Gerhard Häring <gh@ghaering.de>
parents: 41575
diff changeset
166 row = self.con.execute("select ?", (austria,)).fetchone()
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
167 self.assertEqual(type(row[0]), str, "type of row[0] must be unicode")
55602
680ad8eed33b Merged revisions 73833,73838,73850-73852,73856-73857 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46664
diff changeset
168 self.assertTrue(row[0].endswith("reich"), "column must contain original data")
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
169
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
170 def CheckOptimizedUnicode(self):
74850
0fc10a33eb4c Undocument and clean up sqlite3.OptimizedUnicode
Petri Lehtinen <petri@digip.org>
parents: 74726
diff changeset
171 # In py3k, str objects are always returned when text_factory
0fc10a33eb4c Undocument and clean up sqlite3.OptimizedUnicode
Petri Lehtinen <petri@digip.org>
parents: 74726
diff changeset
172 # is OptimizedUnicode
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
173 self.con.text_factory = sqlite.OptimizedUnicode
42525
e64286053b25 Make the sqlite tests pass.
Gerhard Häring <gh@ghaering.de>
parents: 41575
diff changeset
174 austria = "Österreich"
e64286053b25 Make the sqlite tests pass.
Gerhard Häring <gh@ghaering.de>
parents: 41575
diff changeset
175 germany = "Deutchland"
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
176 a_row = self.con.execute("select ?", (austria,)).fetchone()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
177 d_row = self.con.execute("select ?", (germany,)).fetchone()
87178
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
178 self.assertEqual(type(a_row[0]), str, "type of non-ASCII row must be str")
981d161a52be Issue #19601: Use specific asserts in sqlite3 tests.
Serhiy Storchaka <storchaka@gmail.com>
parents: 74850
diff changeset
179 self.assertEqual(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
180
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
181 def tearDown(self):
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
182 self.con.close()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
183
74708
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
184 class TextFactoryTestsWithEmbeddedZeroBytes(unittest.TestCase):
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
185 def setUp(self):
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
186 self.con = sqlite.connect(":memory:")
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
187 self.con.execute("create table test (value text)")
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
188 self.con.execute("insert into test (value) values (?)", ("a\x00b",))
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
189
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
190 def CheckString(self):
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
191 # text_factory defaults to str
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
192 row = self.con.execute("select value from test").fetchone()
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
193 self.assertIs(type(row[0]), str)
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
194 self.assertEqual(row[0], "a\x00b")
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
195
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
196 def CheckBytes(self):
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
197 self.con.text_factory = bytes
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
198 row = self.con.execute("select value from test").fetchone()
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
199 self.assertIs(type(row[0]), bytes)
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
200 self.assertEqual(row[0], b"a\x00b")
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
201
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
202 def CheckBytearray(self):
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
203 self.con.text_factory = bytearray
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
204 row = self.con.execute("select value from test").fetchone()
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
205 self.assertIs(type(row[0]), bytearray)
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
206 self.assertEqual(row[0], b"a\x00b")
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
207
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
208 def CheckCustom(self):
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
209 # A custom factory should receive a bytes argument
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
210 self.con.text_factory = lambda x: x
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
211 row = self.con.execute("select value from test").fetchone()
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
212 self.assertIs(type(row[0]), bytes)
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
213 self.assertEqual(row[0], b"a\x00b")
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
214
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
215 def tearDown(self):
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
216 self.con.close()
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
217
37362
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
218 def suite():
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
219 connection_suite = unittest.makeSuite(ConnectionFactoryTests, "Check")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
220 cursor_suite = unittest.makeSuite(CursorFactoryTests, "Check")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
221 row_suite_compat = unittest.makeSuite(RowFactoryTestsBackwardsCompat, "Check")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
222 row_suite = unittest.makeSuite(RowFactoryTests, "Check")
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
223 text_suite = unittest.makeSuite(TextFactoryTests, "Check")
74708
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
224 text_zero_bytes_suite = unittest.makeSuite(TextFactoryTestsWithEmbeddedZeroBytes, "Check")
2e13011b3719 sqlite3: Handle strings with embedded zeros correctly
Petri Lehtinen <petri@digip.org>
parents: 55602
diff changeset
225 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
226
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
227 def test():
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
228 runner = unittest.TextTestRunner()
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
229 runner.run(suite())
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
230
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
231 if __name__ == "__main__":
e546274d6503 merged the sqlite-integration branch.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
diff changeset
232 test()