codeant-ai-for-open-source[bot] commented on code in PR #40878:
URL: https://github.com/apache/superset/pull/40878#discussion_r3377465595


##########
tests/unit_tests/tasks/test_get_current_user.py:
##########
@@ -0,0 +1,165 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Testes unitários para get_current_user (superset/tasks/utils.py).
+
+Técnicas aplicadas:
+  - Caixa-preta : particionamento de equivalência (CV1, CI1-CI3)
+  - Caixa-branca: cobertura de branch + MC/DC (D1, D2)
+  - Isolamento  : substituição de g via Flask app_context + patch direto
+"""
+
+import importlib.util
+import pathlib
+import sys
+import types
+from unittest.mock import MagicMock, patch
+
+import pytest
+from flask import Flask
+
+
+def _stub(name, **attrs):
+    mod = types.ModuleType(name)
+    mod.__dict__.update(attrs)
+    sys.modules.setdefault(name, mod)
+    return mod
+
+
+_stub("celery")
+_stub("celery.utils")
+_stub("celery.utils.log", get_task_logger=lambda n: MagicMock())
+_stub("superset_core")
+_stub("superset_core.tasks")
+_stub("superset_core.tasks.types", TaskProperties=dict, TaskScope=MagicMock())
+_stub(
+    "superset.tasks.exceptions",
+    ExecutorNotFoundError=Exception,
+    InvalidExecutorError=Exception,
+)
+_stub(
+    "superset.tasks.types",
+    ChosenExecutor=MagicMock(),
+    Executor=MagicMock(),
+    ExecutorType=MagicMock(),
+    FixedExecutor=MagicMock(),
+)
+_stub("superset.utils")
+_stub(
+    "superset.utils.json",
+    loads=MagicMock(),
+    dumps=MagicMock(),
+    JSONDecodeError=ValueError,
+)
+_stub("superset.utils.hashing", hash_from_str=MagicMock(return_value="abc" * 
30))
+_stub("superset.utils.urls", get_url_path=MagicMock())
+
+_path = pathlib.Path(__file__).parents[3] / "superset" / "tasks" / "utils.py"
+_spec = importlib.util.spec_from_file_location("superset.tasks.utils", _path)
+assert _spec is not None
+assert _spec.loader is not None
+_mod = importlib.util.module_from_spec(_spec)
+_spec.loader.exec_module(_mod)
+

Review Comment:
   **🟠 Architect Review — HIGH**
   
   The test module mutates global `sys.modules` at import time with broad stubs 
(e.g. `superset.utils`, `superset.utils.hashing`, `superset.tasks.types`, 
`superset_core`) via `_stub(...)` and never restores them, so these fake 
modules replace the real packages for all later tests in the same process. 
Given pytest's deterministic collection order 
(`tests/unit_tests/tasks/test_get_current_user.py` is imported before 
`tests/unit_tests/utils/test_hashing.py` and other tests that import these 
modules), this injects cross-test contamination and causes order-dependent 
failures when other tests expect the real implementations.
   
   **Suggestion:** Avoid permanent `sys.modules` mutation by either importing 
`superset.tasks.utils` directly as in 
`tests/unit_tests/tasks/test_utils.py:1-14` (so normal imports resolve real 
dependencies), or, if module-level stubbing is required, follow the existing 
pattern used in 
`tests/unit_tests/mcp_service/sql_lab/tool/test_open_sql_lab_with_context.py:15-55`
 and `tests/unit_tests/mcp_service/sql_lab/tool/test_save_sql_query.py:8-50` to 
save original `sys.modules` entries and restore them after each import.
   
   
   [Fix in 
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=56edf3d0135f479384d3fdc1716f6c8e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 | [Fix in VSCode 
Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=56edf3d0135f479384d3fdc1716f6c8e&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 an **Architect / Logical Review** comment left during a code review. 
These reviews are first-class, important findings — not optional suggestions. 
Do NOT dismiss this as a 'big architectural change' just because the title says 
architect review; most of these can be resolved with a small, localized fix 
once the intent is understood.
   
   **Path:** tests/unit_tests/tasks/test_get_current_user.py
   **Line:** 42:76
   **Comment:**
        *HIGH: The test module mutates global `sys.modules` at import time with 
broad stubs (e.g. `superset.utils`, `superset.utils.hashing`, 
`superset.tasks.types`, `superset_core`) via `_stub(...)` and never restores 
them, so these fake modules replace the real packages for all later tests in 
the same process. Given pytest's deterministic collection order 
(`tests/unit_tests/tasks/test_get_current_user.py` is imported before 
`tests/unit_tests/utils/test_hashing.py` and other tests that import these 
modules), this injects cross-test contamination and causes order-dependent 
failures when other tests expect the real implementations.
   
   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.
   If a suggested approach is provided above, use it as the authoritative 
instruction. If no explicit code suggestion is given, you MUST still draft and 
apply your own minimal, localized fix — do not punt back with 'no suggestion 
provided, review manually'. Keep the change as small as possible: add a guard 
clause, gate on a loading state, reorder an await, wrap in a conditional, etc. 
Do not refactor surrounding code or expand scope beyond the finding.
   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>



-- 
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]

Reply via email to