bito-code-review[bot] commented on code in PR #41133: URL: https://github.com/apache/superset/pull/41133#discussion_r3533010916
########## tests/unit_tests/tasks/test_export_dashboard_excel.py: ########## @@ -0,0 +1,379 @@ +# 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. +from __future__ import annotations + +import glob +import os +import tempfile +from collections.abc import Iterator +from contextlib import ExitStack +from typing import Any +from unittest import mock + +import pytest +from celery.exceptions import SoftTimeLimitExceeded + +from superset.utils import json + +MODULE = "superset.tasks.export_dashboard_excel" + + +# A minimal valid 1x1 transparent PNG for image-mode tests. +_PNG_1x1 = ( + b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06" + b"\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc\x00\x01\x00\x00\x05\x00" + b"\x01\r\n-\xb4\x00\x00\x00\x00IEND\xaeB`\x82" +) + + +def _chart( + chart_id: int, + name: str, + has_context: bool = True, + viz_type: str = "line", +) -> mock.MagicMock: + chart = mock.MagicMock() + chart.id = chart_id + chart.slice_name = name + chart.viz_type = viz_type + chart.query_context = json.dumps({"queries": [{}]}) if has_context else None + return chart + + +def _media(path: str) -> list[str]: + """Embedded media entries of an xlsx (which is a zip archive).""" + import zipfile + + with zipfile.ZipFile(path) as archive: + return [n for n in archive.namelist() if n.startswith("xl/media/")] + + [email protected] +def mocks() -> Iterator[dict[str, Any]]: + """Patch every external dependency of the task; keep the real xlsx writer.""" + with ExitStack() as stack: + # Use explicit MagicMock instances: patch() auto-creates async-flavored + # mocks for these targets (their real objects expose async members), which + # would make calls like security_manager.get_user_by_id() return coroutines. + patched = { + name: stack.enter_context( + mock.patch(f"{MODULE}.{name}", new=mock.MagicMock()) + ) + for name in ( + "security_manager", + "db", + "get_charts_in_layout_order", + "get_dashboard_filter_context", + "ChartDataQueryContextSchema", + "ChartDataCommand", + "render_chart_image", + "s3", + "email", + "cache_manager", + ) + } + user = mock.MagicMock() + user.email = "[email protected]" + patched["security_manager"].get_user_by_id.return_value = user + + dashboard = mock.MagicMock() + dashboard.id = 1 + dashboard.dashboard_title = "Sales" + patched[ + "db" + ].session.query.return_value.filter_by.return_value.one_or_none.return_value = ( # noqa: E501 + dashboard + ) + + patched["get_dashboard_filter_context"].return_value.extra_form_data = {} + patched["s3"].generate_presigned_url.return_value = "https://signed/file.xlsx" + + patched["user"] = user + patched["dashboard"] = dashboard + yield patched + + +def _run( + job_id: str = "job-1", + mode: str = "data", + inflight_key: str | None = None, Review Comment: <div> <div id="suggestion"> <div id="issue"><b>Missing test coverage for inflight_key=None path</b></div> <div id="fix"> The `if inflight_key:` guard at production line 336 has no test coverage when the default value `None` is used. Add a test that calls `_run()` without `inflight_key` and asserts `cache.delete` is not called. </div> </div> <small><i>Code Review Run #f931e0</i></small> </div> --- Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>) - [ ] Yes, avoid them -- 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]
