-
Notifications
You must be signed in to change notification settings - Fork 34
Add named parameter support #795
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
8835494
61a5d28
9e06065
6b3d478
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 |
|---|---|---|
|
|
@@ -492,6 +492,44 @@ def test_execute_with_timezone(mocked_connection): | |
| assert result[0][1].tzname() == "UTC" | ||
|
|
||
|
|
||
| def test_execute_with_named_params(mocked_connection): | ||
| """ | ||
| Verify that named %(name)s parameters are converted to positional ? markers | ||
| and the values are passed as an ordered list. | ||
| """ | ||
| cursor = mocked_connection.cursor() | ||
| cursor.execute( | ||
| "SELECT * FROM t WHERE a = %(a)s AND b = %(b)s", | ||
| {"a": 1, "b": 2}, | ||
| ) | ||
| mocked_connection.client.sql.assert_called_once_with( | ||
| "SELECT * FROM t WHERE a = ? AND b = ?", [1, 2], None | ||
| ) | ||
|
|
||
|
|
||
| def test_execute_with_named_params_repeated(mocked_connection): | ||
| """ | ||
| Verify that a parameter name used multiple times in the SQL is resolved | ||
| correctly each time it appears. | ||
| """ | ||
| cursor = mocked_connection.cursor() | ||
| cursor.execute("SELECT %(x)s, %(x)s", {"x": 42}) | ||
| mocked_connection.client.sql.assert_called_once_with( | ||
| "SELECT ?, ?", [42, 42], None | ||
| ) | ||
|
Comment on lines
+516
to
+519
Member
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. Btw. as a follow up we should change this behavior to have this return
Contributor
Author
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. Thanks for suggestion. We can keep track of of already seen names and reuse them inside
Member
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. I already created a PR: #796
Contributor
Author
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. ok super! |
||
|
|
||
|
|
||
| def test_execute_with_named_params_missing(mocked_connection): | ||
| """ | ||
| Verify that a ProgrammingError is raised when a placeholder name is absent | ||
| from the parameters dict, and that the client is never called. | ||
| """ | ||
| cursor = mocked_connection.cursor() | ||
| with pytest.raises(ProgrammingError, match="Named parameter 'z' not found"): | ||
| cursor.execute("SELECT %(z)s", {"a": 1}) | ||
| mocked_connection.client.sql.assert_not_called() | ||
|
|
||
|
|
||
| def test_cursor_close(mocked_connection): | ||
| """ | ||
| Verify that a cursor is not closed if not specifically closed. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.