codeant-ai-for-open-source[bot] commented on code in PR #40860: URL: https://github.com/apache/superset/pull/40860#discussion_r3408411154
########## tests/unit_tests/commands/databases/upload_command_test.py: ########## @@ -0,0 +1,147 @@ +# 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 io +from unittest.mock import MagicMock + +import pytest +from pytest_mock import MockerFixture +from werkzeug.datastructures import FileStorage + +from superset.commands.database.exceptions import DatabaseUploadFileTooLarge +from superset.commands.database.uploaders.base import UploadCommand + + +def _file(contents: bytes) -> FileStorage: + return FileStorage(stream=io.BytesIO(contents), filename="data.bin") + + +def test_file_size_bytes_does_not_consume_stream() -> None: + file = _file(b"abcdefghij") # 10 bytes + assert UploadCommand._file_size_bytes(file) == 10 + # the stream is left at its original position so processing still works + assert file.stream.read() == b"abcdefghij" + + +def _command(file: FileStorage) -> UploadCommand: + # the reader is not exercised by validate(); a stub is sufficient + return UploadCommand( + model_id=1, + table_name="t", + file=file, + schema=None, + reader=MagicMock(), + ) + + +def _stub_passing_checks(mocker: MockerFixture) -> None: + model = mocker.MagicMock() + model.db_engine_spec.supports_file_upload = True + mocker.patch( + "superset.commands.database.uploaders.base.DatabaseDAO.find_by_id", + return_value=model, + ) + mocker.patch( + "superset.commands.database.uploaders.base.schema_allows_file_upload", + return_value=True, + ) + + +def test_validate_rejects_file_over_limit( + app_context: None, mocker: MockerFixture +) -> None: + _stub_passing_checks(mocker) + mocker.patch.dict( + "superset.commands.database.uploaders.base.current_app.config", + {"UPLOAD_MAX_FILE_SIZE_BYTES": 4}, + ) + command = _command(_file(b"too many bytes")) + with pytest.raises(DatabaseUploadFileTooLarge): + command.validate() + + +def test_validate_allows_file_within_limit( + app_context: None, mocker: MockerFixture +) -> None: + _stub_passing_checks(mocker) + mocker.patch.dict( + "superset.commands.database.uploaders.base.current_app.config", + {"UPLOAD_MAX_FILE_SIZE_BYTES": 1024}, + ) + command = _command(_file(b"small")) + command.validate() # should not raise + + +def test_validate_no_limit_when_disabled( + app_context: None, mocker: MockerFixture +) -> None: + _stub_passing_checks(mocker) + mocker.patch.dict( + "superset.commands.database.uploaders.base.current_app.config", + {"UPLOAD_MAX_FILE_SIZE_BYTES": None}, + ) + command = _command(_file(b"x" * 10_000)) + command.validate() # limit explicitly disabled (None) -> no rejection + + +def test_validate_file_size_rejects_over_limit( + app_context: None, mocker: MockerFixture +) -> None: + # the shared helper is used by both the upload and metadata paths + mocker.patch.dict( + "superset.commands.database.uploaders.base.current_app.config", + {"UPLOAD_MAX_FILE_SIZE_BYTES": 4}, + ) + with pytest.raises(DatabaseUploadFileTooLarge): + UploadCommand.validate_file_size(_file(b"too many bytes")) + + +def test_validate_file_size_allows_within_limit( + app_context: None, mocker: MockerFixture +) -> None: + mocker.patch.dict( + "superset.commands.database.uploaders.base.current_app.config", + {"UPLOAD_MAX_FILE_SIZE_BYTES": 1024}, + ) + UploadCommand.validate_file_size(_file(b"small")) # should not raise + + +class _NonSeekableStream(io.RawIOBase): Review Comment: **Suggestion:** Add a class docstring to describe the non-seekable stream behavior used by these tests. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The class is newly introduced and has no docstring. The rule explicitly requires new classes to be documented inline, so this is a verified violation. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=090b41a6f084476e998295e9786821f0&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=090b41a6f084476e998295e9786821f0&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/databases/upload_command_test.py **Line:** 122:122 **Comment:** *Custom Rule: Add a class docstring to describe the non-seekable stream behavior used by these tests. 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%2F40860&comment_hash=901173702a6067982e5e3eea2b5cf3195438fca832b48e79510a5f424424a645&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40860&comment_hash=901173702a6067982e5e3eea2b5cf3195438fca832b48e79510a5f424424a645&reaction=dislike'>👎</a> ########## tests/unit_tests/commands/databases/upload_command_test.py: ########## @@ -0,0 +1,147 @@ +# 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 io +from unittest.mock import MagicMock + +import pytest +from pytest_mock import MockerFixture +from werkzeug.datastructures import FileStorage + +from superset.commands.database.exceptions import DatabaseUploadFileTooLarge +from superset.commands.database.uploaders.base import UploadCommand + + +def _file(contents: bytes) -> FileStorage: + return FileStorage(stream=io.BytesIO(contents), filename="data.bin") + + +def test_file_size_bytes_does_not_consume_stream() -> None: + file = _file(b"abcdefghij") # 10 bytes + assert UploadCommand._file_size_bytes(file) == 10 + # the stream is left at its original position so processing still works + assert file.stream.read() == b"abcdefghij" + + +def _command(file: FileStorage) -> UploadCommand: + # the reader is not exercised by validate(); a stub is sufficient + return UploadCommand( + model_id=1, + table_name="t", + file=file, + schema=None, + reader=MagicMock(), + ) + + +def _stub_passing_checks(mocker: MockerFixture) -> None: + model = mocker.MagicMock() + model.db_engine_spec.supports_file_upload = True + mocker.patch( + "superset.commands.database.uploaders.base.DatabaseDAO.find_by_id", + return_value=model, + ) + mocker.patch( + "superset.commands.database.uploaders.base.schema_allows_file_upload", + return_value=True, + ) + + +def test_validate_rejects_file_over_limit( + app_context: None, mocker: MockerFixture +) -> None: + _stub_passing_checks(mocker) + mocker.patch.dict( + "superset.commands.database.uploaders.base.current_app.config", + {"UPLOAD_MAX_FILE_SIZE_BYTES": 4}, + ) + command = _command(_file(b"too many bytes")) + with pytest.raises(DatabaseUploadFileTooLarge): + command.validate() + + +def test_validate_allows_file_within_limit( + app_context: None, mocker: MockerFixture +) -> None: + _stub_passing_checks(mocker) + mocker.patch.dict( + "superset.commands.database.uploaders.base.current_app.config", + {"UPLOAD_MAX_FILE_SIZE_BYTES": 1024}, + ) + command = _command(_file(b"small")) + command.validate() # should not raise + + +def test_validate_no_limit_when_disabled( + app_context: None, mocker: MockerFixture +) -> None: + _stub_passing_checks(mocker) + mocker.patch.dict( + "superset.commands.database.uploaders.base.current_app.config", + {"UPLOAD_MAX_FILE_SIZE_BYTES": None}, + ) + command = _command(_file(b"x" * 10_000)) + command.validate() # limit explicitly disabled (None) -> no rejection + + +def test_validate_file_size_rejects_over_limit( + app_context: None, mocker: MockerFixture +) -> None: + # the shared helper is used by both the upload and metadata paths + mocker.patch.dict( + "superset.commands.database.uploaders.base.current_app.config", + {"UPLOAD_MAX_FILE_SIZE_BYTES": 4}, + ) + with pytest.raises(DatabaseUploadFileTooLarge): + UploadCommand.validate_file_size(_file(b"too many bytes")) + + +def test_validate_file_size_allows_within_limit( + app_context: None, mocker: MockerFixture +) -> None: + mocker.patch.dict( + "superset.commands.database.uploaders.base.current_app.config", + {"UPLOAD_MAX_FILE_SIZE_BYTES": 1024}, + ) + UploadCommand.validate_file_size(_file(b"small")) # should not raise + + +class _NonSeekableStream(io.RawIOBase): + def seekable(self) -> bool: + return False + + def tell(self) -> int: + raise OSError("not seekable") + + +def _non_seekable_file() -> FileStorage: Review Comment: **Suggestion:** Add a docstring clarifying that this helper returns a file object backed by a non-seekable stream. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This new factory helper is missing a docstring, which violates the custom rule requiring newly added Python functions to be documented. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=306498e76f924419b22b705011d0c7e3&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=306498e76f924419b22b705011d0c7e3&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/databases/upload_command_test.py **Line:** 130:130 **Comment:** *Custom Rule: Add a docstring clarifying that this helper returns a file object backed by a non-seekable stream. 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%2F40860&comment_hash=76e3d4c07e4961b88d74e9b437d50d72dbadcbcd8ef53e5fab2d5cee159d4099&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40860&comment_hash=76e3d4c07e4961b88d74e9b437d50d72dbadcbcd8ef53e5fab2d5cee159d4099&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]
