mikebridge commented on code in PR #41549: URL: https://github.com/apache/superset/pull/41549#discussion_r3574305862
########## tests/unit_tests/tasks/test_deletion_retention.py: ########## @@ -0,0 +1,131 @@ +# 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 deletion-retention configuration and window resolution. + +The shared value overrides config, an unset value falls back to config, ``0`` +is preserved as the disable value, and malformed shared values use the fallback. +""" + +from datetime import datetime +from typing import Any +from unittest.mock import patch + +import pytest +from flask.config import Config + + [email protected] +def app_config(app_context: None) -> Config: + from flask import current_app + + current_app.config["SUPERSET_SOFT_DELETE_RETENTION_DAYS"] = 30 + return current_app.config + + +def _resolve() -> int: + from superset.commands.deletion_retention.window import resolve_retention_window + + return resolve_retention_window() + + +def test_unset_falls_back_to_config(app_config: Config) -> None: + with patch( + "superset.commands.deletion_retention.window.get_shared_value", + return_value=None, + ): + assert _resolve() == 30 + + +def test_shared_value_overrides_config(app_config: Config) -> None: + with patch( + "superset.commands.deletion_retention.window.get_shared_value", + return_value=7, + ): + assert _resolve() == 7 + + +def test_zero_shared_value_is_preserved_not_coerced(app_config: Config) -> None: + # `0` is a meaningful "disable"; it must survive (never `or`-coerced to 30). + with patch( + "superset.commands.deletion_retention.window.get_shared_value", + return_value=0, + ): + assert _resolve() == 0 + + +def test_malformed_shared_value_falls_back(app_config: Config) -> None: + for bad in ("oops", -3, True, 1.5): + with patch( + "superset.commands.deletion_retention.window.get_shared_value", + return_value=bad, + ): + assert _resolve() == 30 + + +def test_window_zero_disables_the_task(app_context: None) -> None: + # A zero window short-circuits the purge entirely. + import superset.tasks.deletion_retention as mod + + with patch.object(mod, "_soft_delete_models") as models: + result = mod._purge_impl(0, dry_run=False) + assert result == {"skipped": 1} + models.assert_not_called() + + +def test_clock_uses_now_not_utcnow() -> None: + # The cutoff uses the same clock as the soft-delete substrate's deleted_at. + import inspect + + import superset.tasks.deletion_retention as mod + + src = inspect.getsource(mod._purge_impl) + assert "datetime.now()" in src + assert "datetime.utcnow()" not in src + + +def test_default_config_is_safe() -> None: + from superset import config + + assert config.SUPERSET_SOFT_DELETE_RETENTION_DAYS == 30 + assert config.SUPERSET_SOFT_DELETE_PURGE_DRY_RUN is True + + +def test_default_celery_config_registers_daily_purge() -> None: + from superset import config + + assert "superset.tasks.deletion_retention" in config.CeleryConfig.imports + entry: dict[str, Any] = config.CeleryConfig.beat_schedule[ + "deletion_retention.purge_soft_deleted" + ] + assert entry["task"] == "deletion_retention.purge_soft_deleted" + + +def test_purge_model_counts_only_committed_deletions(app_context: None) -> None: + import superset.tasks.deletion_retention as mod + from superset.commands.deletion_retention.purge_cascade import CascadeResult + from superset.models.slice import Slice + + lost_race = CascadeResult( + purged=False, entity_type="chart", entity_uuid="lost-race" + ) Review Comment: Addressed in 05b04165b7: lost_race now has an explicit CascadeResult annotation. ########## tests/unit_tests/tasks/test_deletion_retention.py: ########## @@ -0,0 +1,131 @@ +# 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 deletion-retention configuration and window resolution. + +The shared value overrides config, an unset value falls back to config, ``0`` +is preserved as the disable value, and malformed shared values use the fallback. +""" + +from datetime import datetime +from typing import Any +from unittest.mock import patch + +import pytest +from flask.config import Config + + [email protected] +def app_config(app_context: None) -> Config: + from flask import current_app + + current_app.config["SUPERSET_SOFT_DELETE_RETENTION_DAYS"] = 30 + return current_app.config + + +def _resolve() -> int: + from superset.commands.deletion_retention.window import resolve_retention_window + + return resolve_retention_window() + + +def test_unset_falls_back_to_config(app_config: Config) -> None: + with patch( + "superset.commands.deletion_retention.window.get_shared_value", + return_value=None, + ): + assert _resolve() == 30 + + +def test_shared_value_overrides_config(app_config: Config) -> None: + with patch( + "superset.commands.deletion_retention.window.get_shared_value", + return_value=7, + ): + assert _resolve() == 7 + + +def test_zero_shared_value_is_preserved_not_coerced(app_config: Config) -> None: + # `0` is a meaningful "disable"; it must survive (never `or`-coerced to 30). + with patch( + "superset.commands.deletion_retention.window.get_shared_value", + return_value=0, + ): + assert _resolve() == 0 + + +def test_malformed_shared_value_falls_back(app_config: Config) -> None: + for bad in ("oops", -3, True, 1.5): + with patch( + "superset.commands.deletion_retention.window.get_shared_value", + return_value=bad, + ): + assert _resolve() == 30 + + +def test_window_zero_disables_the_task(app_context: None) -> None: + # A zero window short-circuits the purge entirely. + import superset.tasks.deletion_retention as mod + + with patch.object(mod, "_soft_delete_models") as models: + result = mod._purge_impl(0, dry_run=False) + assert result == {"skipped": 1} + models.assert_not_called() + + +def test_clock_uses_now_not_utcnow() -> None: + # The cutoff uses the same clock as the soft-delete substrate's deleted_at. + import inspect + + import superset.tasks.deletion_retention as mod + + src = inspect.getsource(mod._purge_impl) + assert "datetime.now()" in src + assert "datetime.utcnow()" not in src + + +def test_default_config_is_safe() -> None: + from superset import config + + assert config.SUPERSET_SOFT_DELETE_RETENTION_DAYS == 30 + assert config.SUPERSET_SOFT_DELETE_PURGE_DRY_RUN is True + + +def test_default_celery_config_registers_daily_purge() -> None: + from superset import config + + assert "superset.tasks.deletion_retention" in config.CeleryConfig.imports + entry: dict[str, Any] = config.CeleryConfig.beat_schedule[ + "deletion_retention.purge_soft_deleted" + ] + assert entry["task"] == "deletion_retention.purge_soft_deleted" + + +def test_purge_model_counts_only_committed_deletions(app_context: None) -> None: + import superset.tasks.deletion_retention as mod + from superset.commands.deletion_retention.purge_cascade import CascadeResult + from superset.models.slice import Slice + + lost_race = CascadeResult( + purged=False, entity_type="chart", entity_uuid="lost-race" + ) + with ( + patch.object(mod, "_iter_eligible_ids", return_value=[[1]]), + patch.object(mod, "_purge_one", return_value=lost_race), + ): + result = mod._purge_model(Slice, datetime.now(), dry_run=False) Review Comment: Addressed in 05b04165b7: result now has an explicit tuple[int, int, int, int] annotation. -- 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]
