codeant-ai-for-open-source[bot] commented on code in PR #40130: URL: https://github.com/apache/superset/pull/40130#discussion_r3397305342
########## tests/unit_tests/commands/dataset/restore_test.py: ########## @@ -0,0 +1,169 @@ +# 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. +"""Unit tests for RestoreDatasetCommand.""" + +from __future__ import annotations + +import uuid +from datetime import datetime, timezone +from unittest.mock import MagicMock, patch + +import pytest + +# Stable UUIDs make the test boundary realistic: the production command +# loads by UUID, not integer ID. The test only mocks the DAO lookup, but +# the argument shape should still match. +_DATASET_UUID = str(uuid.uuid4()) +_MISSING_UUID = str(uuid.uuid4()) + + +def test_restore_dataset_clears_deleted_at(app_context: None) -> None: + """RestoreDatasetCommand.run() restores a soft-deleted dataset.""" + from superset.commands.dataset.restore import RestoreDatasetCommand + + dataset = MagicMock() + dataset.deleted_at = datetime(2026, 1, 1, tzinfo=timezone.utc) + dataset.id = 1 + + with ( + patch( + "superset.daos.dataset.DatasetDAO.find_by_id", return_value=dataset + ) as mock_find, + patch("superset.commands.restore.security_manager") as mock_sec, + patch( + "superset.daos.dataset.DatasetDAO.has_active_logical_duplicate", + return_value=False, + ), + ): + mock_sec.raise_for_ownership.return_value = None + + cmd = RestoreDatasetCommand(_DATASET_UUID) + cmd.run() + + mock_find.assert_called_once() + dataset.restore.assert_called_once() + + +def test_restore_dataset_not_found_raises(app_context: None) -> None: + """RestoreDatasetCommand raises DatasetNotFoundError for missing dataset.""" + from superset.commands.dataset.exceptions import DatasetNotFoundError + from superset.commands.dataset.restore import RestoreDatasetCommand + + with patch("superset.daos.dataset.DatasetDAO.find_by_id", return_value=None): + cmd = RestoreDatasetCommand(_MISSING_UUID) + with pytest.raises(DatasetNotFoundError): + cmd.run() + + +def test_restore_active_dataset_raises_not_found(app_context: None) -> None: + """RestoreDatasetCommand raises DatasetNotFoundError for non-deleted dataset.""" + from superset.commands.dataset.exceptions import DatasetNotFoundError + from superset.commands.dataset.restore import RestoreDatasetCommand + + dataset = MagicMock() + dataset.deleted_at = None # not soft-deleted + + with patch("superset.daos.dataset.DatasetDAO.find_by_id", return_value=dataset): + cmd = RestoreDatasetCommand(_DATASET_UUID) + with pytest.raises(DatasetNotFoundError): + cmd.run() + + +def test_restore_dataset_forbidden_raises(app_context: None) -> None: + """RestoreDatasetCommand raises DatasetForbiddenError on permission check.""" + from superset.commands.dataset.exceptions import DatasetForbiddenError + from superset.commands.dataset.restore import RestoreDatasetCommand + from superset.exceptions import SupersetSecurityException + + dataset = MagicMock() + dataset.deleted_at = datetime(2026, 1, 1, tzinfo=timezone.utc) + + def raise_security(*args: object, **kwargs: object) -> None: + raise SupersetSecurityException(MagicMock()) Review Comment: **Suggestion:** Add an inline docstring to the newly introduced helper function so it complies with the rule that all new functions must be documented. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The helper function `raise_security` is newly introduced in this file and has no docstring. The custom rule requires newly added Python functions and classes to include docstrings, so this is a real violation. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=27476dfd21f8477ab33f58c077329297&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=27476dfd21f8477ab33f58c077329297&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/dataset/restore_test.py **Line:** 95:96 **Comment:** *Custom Rule: Add an inline docstring to the newly introduced helper function so it complies with the rule that all new functions must be documented. 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%2F40130&comment_hash=deaf6187e54690890410d3c95fa3c67190d0410f2a3c4c9a21f385425fd4ce5c&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40130&comment_hash=deaf6187e54690890410d3c95fa3c67190d0410f2a3c4c9a21f385425fd4ce5c&reaction=dislike'>👎</a> ########## tests/unit_tests/dao/base_dao_test.py: ########## @@ -52,6 +52,40 @@ class TestDAOWithNoneModel(BaseDAO[MockModel]): # ============================================================================= +def test_escape_like_coerces_non_string_values() -> None: + """``_escape_like`` coerces non-str input rather than raising. + + ``ColumnOperator.value`` is typed ``Any``, so a numeric or ``None`` value can + reach a LIKE-family operator; it must degrade to a literal match instead of + raising ``AttributeError`` on ``str.replace``. + """ + # Wildcards in string input are still escaped, backslash first. + assert _escape_like("50%_off") == "50\\%\\_off" + assert _escape_like("a\\b") == "a\\\\b" + # Non-string input is coerced, not crashed. + assert _escape_like(5) == "5" + assert _escape_like(3.5) == "3.5" + assert _escape_like(None) == "" + + +def test_like_operator_accepts_non_string_value() -> None: + """A non-string value flows through a LIKE operator without raising. + + Guards the ``_escape_like`` coercion end-to-end: before it, ``ct``/``like`` + with a numeric value raised ``AttributeError`` deep in operator application. + """ + Base_test = declarative_base() # noqa: N806 + + class TestModel(Base_test): # type: ignore + __tablename__ = "test_model_like" + id = Column(Integer, primary_key=True) + name = Column(String(50)) Review Comment: **Suggestion:** Add a short class docstring to this newly introduced inner model class so new classes are documented inline. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly introduced class in a new hunk, and it does not include a docstring. The custom rule requires new Python classes to be documented inline, so the suggestion matches a real violation. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=c12851abab824d95806b975386bfd8a8&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=c12851abab824d95806b975386bfd8a8&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/dao/base_dao_test.py **Line:** 79:82 **Comment:** *Custom Rule: Add a short class docstring to this newly introduced inner model class so new classes are documented inline. 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%2F40130&comment_hash=5f63bfd120e16a9d9fd29954037ea0b254b2dfb3c7ab755df397075d9654ac85&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40130&comment_hash=5f63bfd120e16a9d9fd29954037ea0b254b2dfb3c7ab755df397075d9654ac85&reaction=dislike'>👎</a> ########## superset/commands/dataset/restore.py: ########## @@ -0,0 +1,54 @@ +# 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. +"""Command to restore a soft-deleted dataset.""" + +from superset.commands.dataset.exceptions import ( + DatasetForbiddenError, + DatasetLogicalDuplicateError, + DatasetNotFoundError, + DatasetRestoreFailedError, +) +from superset.commands.restore import BaseRestoreCommand +from superset.connectors.sqla.models import SqlaTable +from superset.daos.dataset import DatasetDAO + + +class RestoreDatasetCommand(BaseRestoreCommand[SqlaTable]): + """Restore a soft-deleted dataset by clearing its ``deleted_at`` field. + + Most behaviour is inherited from ``BaseRestoreCommand``. The override + here adds a logical-duplicate check: ``SqlaTable`` uniqueness is + enforced in application code (no DB constraint), so it is possible + for another active dataset to have claimed the same ``(database_id, + catalog, schema, table_name)`` while this one was soft-deleted. + Restore should not silently produce two active datasets pointing at + the same physical table. + """ + + dao = DatasetDAO + not_found_exc = DatasetNotFoundError + forbidden_exc = DatasetForbiddenError + restore_failed_exc = DatasetRestoreFailedError + + def validate(self) -> SqlaTable: # type: ignore[override] + model = super().validate() Review Comment: **Suggestion:** Add an inline docstring to the new method so the function is documented as required for newly added Python functions. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The new method `validate` is added without a docstring. The provided rule requires newly added Python functions and classes to include docstrings, so this is a real violation. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=55c0fec0aa0b469cba2ca7f5d4ea5c32&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=55c0fec0aa0b469cba2ca7f5d4ea5c32&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:** superset/commands/dataset/restore.py **Line:** 47:48 **Comment:** *Custom Rule: Add an inline docstring to the new method so the function is documented as required for newly added Python functions. 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%2F40130&comment_hash=ee4067891d5e6f96b0f1ab5caf0befba213388a431aac5a1b325b56ea1af88d7&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40130&comment_hash=ee4067891d5e6f96b0f1ab5caf0befba213388a431aac5a1b325b56ea1af88d7&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]
