Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(164824)

Side by Side Diff: Lib/sqlite3/test/factory.py

Issue 10203: sqlite3.Row doesn't support sequence protocol
Patch Set: Created 5 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/sqlite3/dbapi2.py ('k') | Modules/_sqlite/row.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #-*- coding: iso-8859-1 -*- 1 #-*- coding: iso-8859-1 -*-
2 # pysqlite2/test/factory.py: tests for the various factories in pysqlite 2 # pysqlite2/test/factory.py: tests for the various factories in pysqlite
3 # 3 #
4 # Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de> 4 # Copyright (C) 2005-2007 Gerhard Häring <gh@ghaering.de>
5 # 5 #
6 # This file is part of pysqlite. 6 # This file is part of pysqlite.
7 # 7 #
8 # This software is provided 'as-is', without any express or implied 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 9 # warranty. In no event will the authors be held liable for any damages
10 # arising from the use of this software. 10 # arising from the use of this software.
11 # 11 #
12 # Permission is granted to anyone to use this software for any purpose, 12 # Permission is granted to anyone to use this software for any purpose,
13 # including commercial applications, and to alter it and redistribute it 13 # including commercial applications, and to alter it and redistribute it
14 # freely, subject to the following restrictions: 14 # freely, subject to the following restrictions:
15 # 15 #
16 # 1. The origin of this software must not be misrepresented; you must not 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 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 18 # in a product, an acknowledgment in the product documentation would be
19 # appreciated but is not required. 19 # appreciated but is not required.
20 # 2. Altered source versions must be plainly marked as such, and must not be 20 # 2. Altered source versions must be plainly marked as such, and must not be
21 # misrepresented as being the original software. 21 # misrepresented as being the original software.
22 # 3. This notice may not be removed or altered from any source distribution. 22 # 3. This notice may not be removed or altered from any source distribution.
23 23
24 import unittest 24 import unittest
25 import sqlite3 as sqlite 25 import sqlite3 as sqlite
26 from collections.abc import Sequence
26 27
27 class MyConnection(sqlite.Connection): 28 class MyConnection(sqlite.Connection):
28 def __init__(self, *args, **kwargs): 29 def __init__(self, *args, **kwargs):
29 sqlite.Connection.__init__(self, *args, **kwargs) 30 sqlite.Connection.__init__(self, *args, **kwargs)
30 31
31 def dict_factory(cursor, row): 32 def dict_factory(cursor, row):
32 d = {} 33 d = {}
33 for idx, col in enumerate(cursor.description): 34 for idx, col in enumerate(cursor.description):
34 d[col[0]] = row[idx] 35 d[col[0]] = row[idx]
35 return d 36 return d
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 self.assertTrue(row_2 != row_3) 135 self.assertTrue(row_2 != row_3)
135 136
136 self.assertFalse(row_1 != row_1) 137 self.assertFalse(row_1 != row_1)
137 self.assertFalse(row_1 != row_2) 138 self.assertFalse(row_1 != row_2)
138 self.assertFalse(row_2 == row_3) 139 self.assertFalse(row_2 == row_3)
139 140
140 self.assertEqual(row_1, row_2) 141 self.assertEqual(row_1, row_2)
141 self.assertEqual(hash(row_1), hash(row_2)) 142 self.assertEqual(hash(row_1), hash(row_2))
142 self.assertNotEqual(row_1, row_3) 143 self.assertNotEqual(row_1, row_3)
143 self.assertNotEqual(hash(row_1), hash(row_3)) 144 self.assertNotEqual(hash(row_1), hash(row_3))
145
146 def CheckSqliteRowAsSequence(self):
147 """ Checks if the row object can act like a sequence """
148 self.con.row_factory = sqlite.Row
149 row = self.con.execute("select 1 as a, 2 as b").fetchone()
150
151 as_tuple = tuple(row)
152 self.assertEqual(list(reversed(row)), list(reversed(as_tuple)))
153 self.assertEqual(row[-1], 2)
154 self.assertEqual(row[-2], 1)
155 with self.assertRaises(IndexError):
156 row[-3]
157 with self.assertRaisesRegex(IndexError,
158 "cannot fit 'int' into an "
159 "index-sized integer"):
160 row[2 ** 1000]
161 self.assertIsInstance(row, Sequence)
144 162
145 def tearDown(self): 163 def tearDown(self):
146 self.con.close() 164 self.con.close()
147 165
148 class TextFactoryTests(unittest.TestCase): 166 class TextFactoryTests(unittest.TestCase):
149 def setUp(self): 167 def setUp(self):
150 self.con = sqlite.connect(":memory:") 168 self.con = sqlite.connect(":memory:")
151 169
152 def CheckUnicode(self): 170 def CheckUnicode(self):
153 austria = "Österreich" 171 austria = "Österreich"
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 text_suite = unittest.makeSuite(TextFactoryTests, "Check") 242 text_suite = unittest.makeSuite(TextFactoryTests, "Check")
225 text_zero_bytes_suite = unittest.makeSuite(TextFactoryTestsWithEmbeddedZeroB ytes, "Check") 243 text_zero_bytes_suite = unittest.makeSuite(TextFactoryTestsWithEmbeddedZeroB ytes, "Check")
226 return unittest.TestSuite((connection_suite, cursor_suite, row_suite_compat, row_suite, text_suite, text_zero_bytes_suite)) 244 return unittest.TestSuite((connection_suite, cursor_suite, row_suite_compat, row_suite, text_suite, text_zero_bytes_suite))
227 245
228 def test(): 246 def test():
229 runner = unittest.TextTestRunner() 247 runner = unittest.TextTestRunner()
230 runner.run(suite()) 248 runner.run(suite())
231 249
232 if __name__ == "__main__": 250 if __name__ == "__main__":
233 test() 251 test()
OLDNEW
« no previous file with comments | « Lib/sqlite3/dbapi2.py ('k') | Modules/_sqlite/row.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+