-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-108558: Add a couple more sqlite Row.keys() tests #108628
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -246,11 +246,33 @@ def test_sqlite_row_as_sequence(self): | |||||||||||||||||||||||||||
self.assertEqual(list(reversed(row)), list(reversed(as_tuple))) | ||||||||||||||||||||||||||||
self.assertIsInstance(row, Sequence) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def test_sqlite_row_keys(self): | ||||||||||||||||||||||||||||
def test_sqlite_row_keys_return_columns_as_strings(self): | ||||||||||||||||||||||||||||
# Checks if the row object can return a list of columns as strings. | ||||||||||||||||||||||||||||
row = self.con.execute("select 1 as a, 2 as b").fetchone() | ||||||||||||||||||||||||||||
self.assertEqual(row.keys(), ['a', 'b']) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def test_sqlite_row_keys_raises_exception_on_empty_sql_query(self): | ||||||||||||||||||||||||||||
# Test exception raised on an empty sql query result | ||||||||||||||||||||||||||||
with self.assertRaises(AttributeError): | ||||||||||||||||||||||||||||
row = self.con.execute("select 1 as a where a == 'THISDOESNOTEXIST'").fetchone() | ||||||||||||||||||||||||||||
row.keys() | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def test_sqlite_row_keys_returns_first_value_from_cursor_description(self): | ||||||||||||||||||||||||||||
# docs.python.org: "Immediately after a query, it is | ||||||||||||||||||||||||||||
# the first member of each tuple in Cursor.description." | ||||||||||||||||||||||||||||
cur = self.con.cursor(factory=MyCursor) | ||||||||||||||||||||||||||||
cur.execute("select 1 as a") | ||||||||||||||||||||||||||||
row = cur.fetchone() | ||||||||||||||||||||||||||||
Comment on lines
+264
to
+265
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FTR, it is also possible to do:
Suggested change
and:
Suggested change
... though they are pretty cryptic 😄 |
||||||||||||||||||||||||||||
self.assertEqual(cur.description[0][0], row.keys()[0]) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def test_sqlite_row_keys_are_equal_if_rows_are_equal(self): | ||||||||||||||||||||||||||||
# Two Row objects compare equal if they have identical column names | ||||||||||||||||||||||||||||
# and values. Assert the keys values are the same too. | ||||||||||||||||||||||||||||
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() | ||||||||||||||||||||||||||||
self.assertEqual(row_1, row_2) | ||||||||||||||||||||||||||||
self.assertEqual(row_1.keys(), row_2.keys()) | ||||||||||||||||||||||||||||
Comment on lines
+268
to
+274
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you object to this test as well, @serhiy-storchaka? I think it would be simplified as such, though:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OTOH, this is already tested by |
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def test_fake_cursor_class(self): | ||||||||||||||||||||||||||||
# Issue #24257: Incorrect use of PyObject_IsInstance() caused | ||||||||||||||||||||||||||||
# segmentation fault. | ||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you narrow this down? Where is the AttributeError raised? Consider also using
assertRaisesRegex
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the type of
row
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
.fetchone()
is returning None as per the comment below,and the type of
keys
has to be a list of strings,and here is the C code for the keys method,
however I'm not familiar with the codebase enough to go between the C code and the Python code to find where the error is being raised :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the query fails, no rows are returned, so
fetchone
returnsNone
, so what you are really testing here is thatNone
does not have akeys
attribute :) I think we can do without this test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None :) As already mentioned, we don't need this test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
fetchone()
returns not aRow
, this code has no any relation to testingRow.keys()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #108628 (comment), Serhiy.