codeant-ai-for-open-source[bot] commented on code in PR #34880: URL: https://github.com/apache/superset/pull/34880#discussion_r3600529923
########## 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"] Review Comment: **Suggestion:** Add type annotations to the extracted UUID-related local variables used in query filters. [custom_rule] **Severity Level:** Minor π§Ή <details> <summary><b>Why it matters? β </b></summary> These newly added local variables hold UUID values and are left unannotated in the test, which matches the type-hints ruleβs target of annotatable variables. </details> <details> <summary><b>Rule source π </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=7badf7f7bf674cb8b7b4e6d1759f7368&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=7badf7f7bf674cb8b7b4e6d1759f7368&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:** 80:86 **Comment:** *Custom Rule: Add type annotations to the extracted UUID-related local variables used in query filters. 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=8d56bff7396ab56f5f2c141b147180b133e8ee2bb3111c516db8cf5a684c826f&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F34880&comment_hash=8d56bff7396ab56f5f2c141b147180b133e8ee2bb3111c516db8cf5a684c826f&reaction=dislike'>π</a> ########## 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"] Review Comment: **Suggestion:** Annotate the UUID extraction locals with explicit types in this test to satisfy the typing rule. [custom_rule] **Severity Level:** Minor π§Ή <details> <summary><b>Why it matters? β </b></summary> These UUID-bearing locals are newly introduced in the file and remain untyped, so the type-hints rule applies here as well. </details> <details> <summary><b>Rule source π </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=a3f1226751da4dc38259a960efb13e1b&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=a3f1226751da4dc38259a960efb13e1b&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:** 168:174 **Comment:** *Custom Rule: Annotate the UUID extraction locals with explicit types in this test to satisfy the typing rule. 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=e88291de6003bfec1be066cf3e8dad016251ff4a565c98f392d35d5758580b7f&reaction=like'>π</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F34880&comment_hash=e88291de6003bfec1be066cf3e8dad016251ff4a565c98f392d35d5758580b7f&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]
