Repository: python/cpython
Pull request: gh-108558: Improve sqlite3 row factory tests
- Fix for [gh-108558: Add tests for sqlite3.Row.keys()](https://github.com/python/cpython/issues/108558), adding tests for `sqlite3.Row.keys()` which currently doesn't have any.
- Make RowFactoryTests less noisy in verbose mode
- Reduce boilerplate code in RowFactoryTests


Problem statement:
Add explicit test for sqlite3.Row.keys()
### Has this already been discussed elsewhere?

No response given

### Links to previous discussion of this feature:

_No response_

### Proposal:

Seems, there are no tests for `sqlite3.Row.keys()`.

<!-- gh-linked-prs -->
### Linked PRs
* gh-108578
* gh-108615
* gh-108616
* gh-108628
<!-- /gh-linked-prs -->

Hints from issue discussion:
Hey @erlend-aasland / @serhiy-storchaka - I'm a test engineer and would love to contribute to Python. Been using it for 9+ years. Any chance of taking a look at this issue?
Great, have a go at it! :) I'll get a ping when you create the PR.

Commit message: Add test_sqlite_row_keys poc

Patch to review:
diff --git a/Lib/test/test_sqlite3/test_factory.py b/Lib/test/test_sqlite3/test_factory.py
index a7c4417862aff7..2c3352c6edcc21 100644
--- a/Lib/test/test_sqlite3/test_factory.py
+++ b/Lib/test/test_sqlite3/test_factory.py
@@ -250,6 +250,13 @@ 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):
+        """ Checks if the row object can return its keys """
+        self.con.row_factory = sqlite.Row
+        row = self.con.execute("select 1 as a, 2 as b").fetchone()
+
+        self.assertEqual(row.keys(), ['a', 'b'])
+
     def test_fake_cursor_class(self):
         # Issue #24257: Incorrect use of PyObject_IsInstance() caused
         # segmentation fault.


Code review comments:
diff_hunk: @@ -250,6 +250,13 @@ 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):
+        """ Checks if the row object can return its keys """
path: Lib/test/test_sqlite3/test_factory.py
text: @user1:
FYI, if you need to leave a descriptive comment, please don't use docstrings, because they mess up the verbose mode; instead use ordinary `#` comments :)

I know there are a couple other occurrences of this practice in `RowFactoryTests`[^1]; now would be a good time to clean those up.

[^1]: `test_sqlite_row_iter`, `test_sqlite_row_as_tuple`, `test_sqlite_row_as_dict`, `test_sqlite_row_hash_cmp`, and `test_sqlite_row_as_sequence`.

@user1:
(Try `./python.exe -m test test_sqlite3 -v -m RowFactoryTests` for verbose mode; edit out the docstrings and run it again ;) )

@author:
All done! :) 

@user1:
Thanks!