codeant-ai-for-open-source[bot] commented on code in PR #42412:
URL: https://github.com/apache/superset/pull/42412#discussion_r3670991380
##########
tests/unit_tests/commands/chart/streaming_export_command_test.py:
##########
@@ -103,17 +103,11 @@ def test_csv_generation_with_small_dataset(mocker:
MockerFixture) -> None:
[],
]
- mock_connection = mocker.MagicMock()
- mock_connection.execution_options.return_value.execute.return_value = (
- mock_result_proxy
- )
- mock_connection.__enter__.return_value = mock_connection
- mock_connection.__exit__.return_value = None
+ mock_conn = mocker.MagicMock()
+ mock_conn.cursor.return_value = mock_cursor
- mock_engine = mocker.MagicMock()
- mock_engine.connect.return_value = mock_connection
- datasource.database.get_sqla_engine.return_value.__enter__.return_value = (
- mock_engine
+ datasource.database.get_raw_connection.return_value = (
+ mock_conn
)
Review Comment:
**Suggestion:** The test configures `get_raw_connection()` to return a raw
`MagicMock`, but the real API returns a context manager that must be entered
with `with`. This causes the tests to exercise an artificial contract and
allows the production `closing(get_raw_connection(...))` regression to pass
unnoticed. Configure the mock as an entered context manager and assert that the
context is entered and exited. [api mismatch]
<details>
<summary><b>Severity Level:</b> Critical 🚨</summary>
```mdx
- ❌ Unit tests miss production connection-entry failures.
- ⚠️ Streaming export regressions can pass CI undetected.
- ⚠️ Connection cleanup behavior is not verified.
```
</details>
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. Run the chart streaming unit tests, whose common setup patches the
database at
`tests/unit_tests/commands/chart/streaming_export_command_test.py:27-48`.
2. Each test assigns a raw `mock_conn` directly as
`get_raw_connection.return_value`, for
example at
`tests/unit_tests/commands/chart/streaming_export_command_test.py:106-111`,
rather than configuring `return_value.__enter__.return_value`.
3. Production `Database.get_raw_connection()` is a context manager at
`superset/models/core.py:675-692`, and the production code is supposed to
enter it with
`with ... as conn`.
4. Consequently, these tests exercise a different API contract: they allow
`conn.cursor()`
directly and cannot detect the current `closing(get_raw_connection(...))`
failure at
`superset/commands/streaming_export/base.py:243-247`. Configure the entered
connection
mock and assert context entry and exit.
```
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=b2f112c439c0478d9a5160daef8a3d5a&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=b2f112c439c0478d9a5160daef8a3d5a&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** tests/unit_tests/commands/chart/streaming_export_command_test.py
**Line:** 109:111
**Comment:**
*Api Mismatch: The test configures `get_raw_connection()` to return a
raw `MagicMock`, but the real API returns a context manager that must be
entered with `with`. This causes the tests to exercise an artificial contract
and allows the production `closing(get_raw_connection(...))` regression to pass
unnoticed. Configure the mock as an entered context manager and assert that the
context is entered and exited.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42412&comment_hash=77704b89be1c8fc7154a627a6a06498b8d7e27db9d6dc5f3e4a8e5ef11f3fe18&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42412&comment_hash=77704b89be1c8fc7154a627a6a06498b8d7e27db9d6dc5f3e4a8e5ef11f3fe18&reaction=dislike'>👎</a>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]