bito-code-review[bot] commented on code in PR #38576: URL: https://github.com/apache/superset/pull/38576#discussion_r3476969857
########## tests/unit_tests/tasks/test_thumbnails.py: ########## @@ -0,0 +1,134 @@ +# 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 unittest.mock import MagicMock, patch + +import pytest + + [email protected] +def mock_thumbnail_cache(): + with patch("superset.tasks.thumbnails.thumbnail_cache", True): + yield + + [email protected] +def mock_dashboard(): + dashboard = MagicMock() + dashboard.id = 1 + dashboard.digest = "test_digest" + return dashboard + + +def _make_screenshot_mock(cache_key: str = "test_cache_key") -> MagicMock: + screenshot = MagicMock() + screenshot.get_cache_key.return_value = cache_key + return screenshot + + +# --------------------------------------------------------------------------- +# Helpers shared across tests +# --------------------------------------------------------------------------- + +_COMMON_PATCHES = [ + "superset.tasks.thumbnails.get_executor", + "superset.tasks.thumbnails.security_manager", + "superset.tasks.thumbnails.override_user", + "superset.tasks.thumbnails.get_url_path", + "superset.tasks.thumbnails.DashboardScreenshot", +] + + +def _apply_patches(fn): + """Stack the five common patches onto a test function.""" + for p in reversed(_COMMON_PATCHES): + fn = patch(p)(fn) + return fn + + +# --------------------------------------------------------------------------- +# Tests +# --------------------------------------------------------------------------- + + +@patch("superset.tasks.thumbnails.thumbnail_cache", None) +def test_cache_dashboard_thumbnail_skips_when_no_cache(): + """Task exits early when no thumbnail cache is configured.""" + from superset.tasks.thumbnails import cache_dashboard_thumbnail + + result = cache_dashboard_thumbnail(current_user="admin", dashboard_id=1, force=True) + + assert result is None + + +@_apply_patches +def test_cache_dashboard_thumbnail_always_delegates_to_compute_and_cache( + mock_screenshot_cls, + mock_get_url_path, + mock_override_user, + mock_security_manager, + mock_get_executor, + mock_dashboard, + mock_thumbnail_cache, +): + """Task always calls compute_and_cache; dedup lives inside compute_and_cache.""" + from superset.tasks.thumbnails import cache_dashboard_thumbnail + + mock_screenshot = _make_screenshot_mock() + mock_screenshot_cls.return_value = mock_screenshot + mock_get_executor.return_value = (None, "admin") + mock_security_manager.find_user.return_value = MagicMock() + + with patch("superset.models.dashboard.Dashboard.get", return_value=mock_dashboard): + cache_dashboard_thumbnail( Review Comment: <!-- Bito Reply --> The update to pass `resolved_cache_key` through to `compute_and_cache` correctly addresses the issue of the unused variable identified in the review. This change ensures that the resolved key is utilized as intended within the task logic. -- 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]
