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

Delta Between Two Patch Sets: Lib/sqlite3/test/factory.py

Issue 13583: sqlite3.Row doesn't support slice indexes
Left Patch Set: Created 5 years, 9 months ago
Right Patch Set: Created 4 years, 10 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Modules/_sqlite/row.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 self.assertIsInstance(row, sqlite.Row) 90 self.assertIsInstance(row, sqlite.Row)
90 91
91 col1, col2 = row["a"], row["b"] 92 col1, col2 = row["a"], row["b"]
92 self.assertEqual(col1, 1, "by name: wrong result for column 'a'") 93 self.assertEqual(col1, 1, "by name: wrong result for column 'a'")
93 self.assertEqual(col2, 2, "by name: wrong result for column 'a'") 94 self.assertEqual(col2, 2, "by name: wrong result for column 'a'")
94 95
95 col1, col2 = row["A"], row["B"] 96 col1, col2 = row["A"], row["B"]
96 self.assertEqual(col1, 1, "by name: wrong result for column 'A'") 97 self.assertEqual(col1, 1, "by name: wrong result for column 'A'")
97 self.assertEqual(col2, 2, "by name: wrong result for column 'B'") 98 self.assertEqual(col2, 2, "by name: wrong result for column 'B'")
98 99
99 col1, col2 = row[0], row[1] 100 self.assertEqual(row[0], 1, "by index: wrong result for column 0")
100 self.assertEqual(col1, 1, "by index: wrong result for column 0") 101 self.assertEqual(row[1], 2, "by index: wrong result for column 1")
101 self.assertEqual(col2, 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]
102 113
103 def CheckSqliteRowSlice(self): 114 def CheckSqliteRowSlice(self):
104 # A sqlite.Row can be sliced like a list. 115 # A sqlite.Row can be sliced like a list.
105 self.con.row_factory = sqlite.Row 116 self.con.row_factory = sqlite.Row
106 row = self.con.execute("select 1, 2, 3, 4").fetchone() 117 row = self.con.execute("select 1, 2, 3, 4").fetchone()
107 self.assertEqual(row[0:0], ()) 118 self.assertEqual(row[0:0], ())
108 self.assertEqual(row[0:1], (1,)) 119 self.assertEqual(row[0:1], (1,))
109 self.assertEqual(row[1:3], (2, 3)) 120 self.assertEqual(row[1:3], (2, 3))
110 self.assertEqual(row[3:1], ()) 121 self.assertEqual(row[3:1], ())
111 # Explicit bounds are optional. 122 # Explicit bounds are optional.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 164
154 self.assertFalse(row_1 != row_1) 165 self.assertFalse(row_1 != row_1)
155 self.assertFalse(row_1 != row_2) 166 self.assertFalse(row_1 != row_2)
156 self.assertFalse(row_2 == row_3) 167 self.assertFalse(row_2 == row_3)
157 168
158 self.assertEqual(row_1, row_2) 169 self.assertEqual(row_1, row_2)
159 self.assertEqual(hash(row_1), hash(row_2)) 170 self.assertEqual(hash(row_1), hash(row_2))
160 self.assertNotEqual(row_1, row_3) 171 self.assertNotEqual(row_1, row_3)
161 self.assertNotEqual(hash(row_1), hash(row_3)) 172 self.assertNotEqual(hash(row_1), hash(row_3))
162 173
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
163 def tearDown(self): 183 def tearDown(self):
164 self.con.close() 184 self.con.close()
165 185
166 class TextFactoryTests(unittest.TestCase): 186 class TextFactoryTests(unittest.TestCase):
167 def setUp(self): 187 def setUp(self):
168 self.con = sqlite.connect(":memory:") 188 self.con = sqlite.connect(":memory:")
169 189
170 def CheckUnicode(self): 190 def CheckUnicode(self):
171 austria = "Österreich" 191 austria = "Österreich"
172 row = self.con.execute("select ?", (austria,)).fetchone() 192 row = self.con.execute("select ?", (austria,)).fetchone()
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 text_suite = unittest.makeSuite(TextFactoryTests, "Check") 262 text_suite = unittest.makeSuite(TextFactoryTests, "Check")
243 text_zero_bytes_suite = unittest.makeSuite(TextFactoryTestsWithEmbeddedZeroB ytes, "Check") 263 text_zero_bytes_suite = unittest.makeSuite(TextFactoryTestsWithEmbeddedZeroB ytes, "Check")
244 return unittest.TestSuite((connection_suite, cursor_suite, row_suite_compat, row_suite, text_suite, text_zero_bytes_suite)) 264 return unittest.TestSuite((connection_suite, cursor_suite, row_suite_compat, row_suite, text_suite, text_zero_bytes_suite))
245 265
246 def test(): 266 def test():
247 runner = unittest.TextTestRunner() 267 runner = unittest.TextTestRunner()
248 runner.run(suite()) 268 runner.run(suite())
249 269
250 if __name__ == "__main__": 270 if __name__ == "__main__":
251 test() 271 test()
LEFTRIGHT

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