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


##########
tests/unit_tests/commands/dashboard/importers/v1/assets_test.py:
##########
@@ -0,0 +1,215 @@
+# 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.
+
+import copy
+
+from pytest_mock import MockerFixture
+from sqlalchemy.orm.session import Session
+
+from tests.unit_tests.fixtures.assets_configs import (
+    charts_config_1,
+    dashboards_config_1,
+    databases_config,
+    datasets_config,
+)
+
+
+def test_import_dashboard_overwrite_charts_and_datasets(
+    mocker: MockerFixture, session: Session
+) -> None:
+    """
+    Test that existing dashboards are overwritten.
+    """
+    import time
+
+    from superset import db, security_manager
+    from superset.commands.dashboard.importers.v1 import 
ImportDashboardsCommand
+    from superset.connectors.sqla.models import SqlaTable
+    from superset.models.dashboard import Dashboard
+    from superset.models.slice import Slice
+
+    mocker.patch.object(security_manager, "can_access", return_value=True)
+
+    engine = db.session.get_bind()
+    Dashboard.metadata.create_all(engine)
+
+    # for some reason it won't allow me to reuse the same config in the second 
import
+    # thus declaring two configs with the same content
+    base_configs = {
+        **copy.deepcopy(databases_config),
+        **copy.deepcopy(datasets_config),
+        **copy.deepcopy(charts_config_1),
+        **copy.deepcopy(dashboards_config_1),
+    }
+    new_configs = {
+        **copy.deepcopy(databases_config),
+        **copy.deepcopy(datasets_config),
+        **copy.deepcopy(charts_config_1),
+        **copy.deepcopy(dashboards_config_1),
+    }
+
+    # tweak the dashboard/chart/dataset payloads so the second import actually
+    # changes a column value. SQLAlchemy only touches `changed_on` (via
+    # `onupdate`) when a flush detects a real attribute diff, so re-importing
+    # byte-for-byte identical config, as `base_configs` and `new_configs`
+    # otherwise are, would not bump `changed_on` at all and this test would be
+    # asserting something that isn't true of the underlying overwrite
+    # mechanism.
+    new_configs["dashboards/Video_Game_Sales_11.yaml"]["description"] = (
+        "updated description"
+    )
+    new_configs["charts/Games_per_Genre_over_time_95.yaml"]["cache_timeout"] = 
60
+    new_configs["datasets/examples/video_game_sales.yaml"]["description"] = (
+        "updated description"
+    )
+
+    # gettings uuids
+    dashboard_configs = list(dashboards_config_1.values())
+    dashboard_uuid = dashboard_configs[0]["uuid"]
+    chart_configs = list(charts_config_1.values())
+    chart_uuid = chart_configs[0]["uuid"]
+    dataset_configs = list(datasets_config.values())
+    dataset_uuid = dataset_configs[0]["uuid"]
+
+    # importing first time and retrieving initial records
+    ImportDashboardsCommand._import(base_configs, overwrite=True, 
overwrite_all=True)
+    imported_dashboard = (
+        db.session.query(Dashboard).filter_by(uuid=dashboard_uuid).one()
+    )
+    imported_chart = db.session.query(Slice).filter_by(uuid=chart_uuid).one()
+    imported_dataset = 
db.session.query(SqlaTable).filter_by(uuid=dataset_uuid).one()
+
+    # extracting changed_on field to compare later, ignoring milliseconds
+    initial_dashboard_changed_on = imported_dashboard.changed_on.strftime(
+        "%Y-%m-%d %H:%M:%S"
+    )
+    initial_chart_changed_on = imported_chart.changed_on.strftime("%Y-%m-%d 
%H:%M:%S")
+    initial_dataset_changed_on = imported_dataset.changed_on.strftime(
+        "%Y-%m-%d %H:%M:%S"
+    )
+
+    # ensuring the changed_on field will be different
+    time.sleep(1)
+
+    # importing second time and retrieving updated records
+    ImportDashboardsCommand._import(new_configs, overwrite=True, 
overwrite_all=True)
+    imported_dashboard = (
+        db.session.query(Dashboard).filter_by(uuid=dashboard_uuid).one()
+    )
+    imported_chart = db.session.query(Slice).filter_by(uuid=chart_uuid).one()
+    imported_dataset = 
db.session.query(SqlaTable).filter_by(uuid=dataset_uuid).one()
+
+    # extracting changed_on field to compare with the previous retrieved
+    # ignoring milliseconds
+    final_dashboard_changed_on = imported_dashboard.changed_on.strftime(
+        "%Y-%m-%d %H:%M:%S"
+    )
+    final_chart_changed_on = imported_chart.changed_on.strftime("%Y-%m-%d 
%H:%M:%S")
+    final_dataset_changed_on = imported_dataset.changed_on.strftime("%Y-%m-%d 
%H:%M:%S")
+
+    # asserting the changed_on field was updated on all three records
+    assert initial_dashboard_changed_on != final_dashboard_changed_on
+    assert initial_chart_changed_on != final_chart_changed_on
+    assert initial_dataset_changed_on != final_dataset_changed_on
+    # asserting the changed_on field was updated to a later value on all three 
records
+    assert final_dashboard_changed_on >= initial_dashboard_changed_on
+    assert final_chart_changed_on >= initial_chart_changed_on
+    assert final_dataset_changed_on >= initial_dataset_changed_on
+
+
+def test_import_dashboard_do_not_overwrite_charts_and_datasets(
+    mocker: MockerFixture, session: Session
+) -> None:
+    """
+    Test that existing dashboards are overwritten but charts and datasets are 
not.
+    """
+    import time
+
+    from superset import db, security_manager
+    from superset.commands.dashboard.importers.v1 import 
ImportDashboardsCommand
+    from superset.connectors.sqla.models import SqlaTable
+    from superset.models.dashboard import Dashboard
+    from superset.models.slice import Slice
+
+    mocker.patch.object(security_manager, "can_access", return_value=True)
+
+    engine = db.session.get_bind()
+    Dashboard.metadata.create_all(engine)
+
+    # for some reason it won't allow me to reuse the same config in the second 
import
+    # thus declaring two configs with the same content
+    base_configs = {
+        **copy.deepcopy(databases_config),
+        **copy.deepcopy(datasets_config),
+        **copy.deepcopy(charts_config_1),
+        **copy.deepcopy(dashboards_config_1),
+    }
+    new_configs = {
+        **copy.deepcopy(databases_config),
+        **copy.deepcopy(datasets_config),
+        **copy.deepcopy(charts_config_1),
+        **copy.deepcopy(dashboards_config_1),
+    }
+
+    # gettings uuids
+    dashboard_configs = list(dashboards_config_1.values())
+    dashboard_uuid = dashboard_configs[0]["uuid"]
+    chart_configs = list(charts_config_1.values())
+    chart_uuid = chart_configs[0]["uuid"]
+    dataset_configs = list(datasets_config.values())
+    dataset_uuid = dataset_configs[0]["uuid"]
+
+    # importing first time and retrieving initial records
+    ImportDashboardsCommand._import(base_configs, overwrite=True, 
overwrite_all=False)
+    imported_dashboard = (
+        db.session.query(Dashboard).filter_by(uuid=dashboard_uuid).one()
+    )
+    imported_chart = db.session.query(Slice).filter_by(uuid=chart_uuid).one()
+    imported_dataset = 
db.session.query(SqlaTable).filter_by(uuid=dataset_uuid).one()
+
+    # extracting changed_on field to compare later, ignoring milliseconds
+    initial_dashboard_changed_on = imported_dashboard.changed_on.strftime(
+        "%Y-%m-%d %H:%M:%S"
+    )
+    initial_chart_changed_on = imported_chart.changed_on.strftime("%Y-%m-%d 
%H:%M:%S")
+    initial_dataset_changed_on = imported_dataset.changed_on.strftime(
+        "%Y-%m-%d %H:%M:%S"
+    )
+
+    # ensuring the changed_on field will be different
+    time.sleep(1)
+
+    # importing second time and retrieving updated records
+    ImportDashboardsCommand._import(new_configs, overwrite=True, 
overwrite_all=False)
+    imported_dashboard = (
+        db.session.query(Dashboard).filter_by(uuid=dashboard_uuid).one()
+    )
+    imported_chart = db.session.query(Slice).filter_by(uuid=chart_uuid).one()
+    imported_dataset = 
db.session.query(SqlaTable).filter_by(uuid=dataset_uuid).one()
+
+    # extracting changed_on field to compare with the previous retrieved
+    # ignoring milliseconds
+    final_dashboard_changed_on = imported_dashboard.changed_on.strftime(
+        "%Y-%m-%d %H:%M:%S"
+    )
+    final_chart_changed_on = imported_chart.changed_on.strftime("%Y-%m-%d 
%H:%M:%S")
+    final_dataset_changed_on = imported_dataset.changed_on.strftime("%Y-%m-%d 
%H:%M:%S")
+
+    # changed_on should update on the dashboard but not on charts and datasets
+    assert initial_dashboard_changed_on != final_dashboard_changed_on

Review Comment:
   **Suggestion:** The test re-imports an unchanged dashboard config and then 
asserts that `changed_on` must differ, but without any dashboard field change 
SQLAlchemy may not issue an update and `changed_on` can remain identical. This 
makes the test assertion incorrect/flaky. Update `new_configs` with at least 
one dashboard attribute change (like the first test does) before the second 
import, or assert behavior that does not depend on `changed_on` changing for 
identical payloads. [logic error]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ⚠️ Unit test may fail despite correct importer behavior.
   - ⚠️ Dashboard import overwrite_all=False semantics coverage unreliable.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Run the unit test 
`test_import_dashboard_do_not_overwrite_charts_and_datasets` in
   `tests/unit_tests/commands/dashboard/importers/v1/assets_test.py:134-215`.
   
   2. Observe that `base_configs` and `new_configs` are built from the same 
deep-copied
   fixtures without any modifications (lines 153-166), so the second import 
uses an identical
   dashboard payload to the first.
   
   3. The test calls `ImportDashboardsCommand._import(new_configs, 
overwrite=True,
   overwrite_all=False)` at line 197, which in turn invokes 
`ImportDashboardsCommand._import`
   in `superset/commands/dashboard/importers/v1/__init__.py:99-147`, and that 
calls
   `import_dashboard(config, overwrite=overwrite)` from
   `superset/commands/dashboard/importers/v1/utils.py:274-287`.
   
   4. In `import_dashboard` (utils.py:274-87), when `existing` is found and 
`overwrite=True`,
   it sets `config["id"] = existing.id` then calls 
`Dashboard.import_from_dict(config,
   recursive=False)`; this applies the same values back onto the existing row, 
so SQLAlchemy
   detects no real column changes and does not bump `changed_on` (which is only 
updated via
   `onupdate=datetime.now` in `AuditMixinNullable`), causing 
`final_dashboard_changed_on` to
   equal `initial_dashboard_changed_on` and making the assertion at line 213 
incorrect/flaky.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=515ed1f75dcf4c14a960833b6820c959&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=515ed1f75dcf4c14a960833b6820c959&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/dashboard/importers/v1/assets_test.py
   **Line:** 213:213
   **Comment:**
        *Logic Error: The test re-imports an unchanged dashboard config and 
then asserts that `changed_on` must differ, but without any dashboard field 
change SQLAlchemy may not issue an update and `changed_on` can remain 
identical. This makes the test assertion incorrect/flaky. Update `new_configs` 
with at least one dashboard attribute change (like the first test does) before 
the second import, or assert behavior that does not depend on `changed_on` 
changing for identical payloads.
   
   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%2F34880&comment_hash=020641665f4b904d82a75e9052acdee399013a4ec481a0481a825815eaa392d5&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F34880&comment_hash=020641665f4b904d82a75e9052acdee399013a4ec481a0481a825815eaa392d5&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]

Reply via email to