Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-108558: Improve sqlite3 row factory tests #108578

Merged
merged 6 commits into from
Aug 29, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Lib/test/test_sqlite3/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,22 @@ def test_sqlite_row_iter(self):
self.assertEqual(items, [1, 2])

def test_sqlite_row_as_tuple(self):
"""Checks if the row object can be converted to a tuple"""
# Checks if the row object can be converted to a tuple
self.con.row_factory = sqlite.Row
row = self.con.execute("select 1 as a, 2 as b").fetchone()
t = tuple(row)
self.assertEqual(t, (row['a'], row['b']))

def test_sqlite_row_as_dict(self):
"""Checks if the row object can be correctly converted to a dictionary"""
# Checks if the row object can be correctly converted to a dictionary
self.con.row_factory = sqlite.Row
row = self.con.execute("select 1 as a, 2 as b").fetchone()
d = dict(row)
self.assertEqual(d["a"], row["a"])
self.assertEqual(d["b"], row["b"])

def test_sqlite_row_hash_cmp(self):
"""Checks if the row object compares and hashes correctly"""
# Checks if the row object compares and hashes correctly
self.con.row_factory = sqlite.Row
row_1 = self.con.execute("select 1 as a, 2 as b").fetchone()
row_2 = self.con.execute("select 1 as a, 2 as b").fetchone()
Expand Down Expand Up @@ -242,14 +242,21 @@ def test_sqlite_row_hash_cmp(self):
self.assertEqual(hash(row_1), hash(row_2))

def test_sqlite_row_as_sequence(self):
""" Checks if the row object can act like a sequence """
# Checks if the row object can act like a sequence
self.con.row_factory = sqlite.Row
row = self.con.execute("select 1 as a, 2 as b").fetchone()

as_tuple = tuple(row)
self.assertEqual(list(reversed(row)), list(reversed(as_tuple)))
self.assertIsInstance(row, Sequence)

def test_sqlite_row_keys(self):
# Checks if the row object can return a list of columns as strings
self.con.row_factory = sqlite.Row
row = self.con.execute("select 1 as a, 2 as b").fetchone()

erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(row.keys(), ['a', 'b'])

def test_fake_cursor_class(self):
# Issue #24257: Incorrect use of PyObject_IsInstance() caused
# segmentation fault.
Expand Down
Loading