codeant-ai-for-open-source[bot] commented on code in PR #41075: URL: https://github.com/apache/superset/pull/41075#discussion_r3658767421
########## tests/unit_tests/tasks/test_version_history_retention.py: ########## @@ -0,0 +1,200 @@ +# 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 the operational instrumentation in +``superset.tasks.version_history_retention``. + +Covers the branches that emit statsd counters: the ``retention_days <= 0`` +short-circuit, incomplete shadow-table resolution, the ``OperationalError`` +retry path, and the terminal failure counter. The +"happy path" / SERIALIZABLE retry behaviour against a real database is +exercised by ``tests/integration_tests/versioning/retention_prune_tests.py``; +this file pins the metric-emission contract that is load-bearing for +operator alerting. +""" + +from __future__ import annotations + +from collections.abc import Iterator +from unittest.mock import MagicMock, patch + +import pytest +from sqlalchemy.exc import OperationalError +from sqlalchemy_continuum.exc import ClassNotVersioned + +from superset.tasks import version_history_retention + + [email protected](name="stats") +def _stats_fixture() -> Iterator[MagicMock]: + """Patch the shared stats logger so every test can assert on + emissions without standing up the real statsd backend.""" + with patch.object( + version_history_retention, "stats_logger_manager" + ) as mock_manager: + mock_manager.instance = MagicMock() + yield mock_manager.instance + + +def test_retention_disabled_emits_skipped_metric(stats: MagicMock) -> None: + """``retention_days <= 0`` is the documented "disable retention" + config. The early-return must emit ``superset.versioning.retention.skipped`` + so a dashboard can tell "operator disabled it" apart from "scheduler + isn't running".""" + result = version_history_retention._prune_old_versions_impl(retention_days=0) + assert result == {"skipped": 1} + stats.incr.assert_called_once_with("superset.versioning.retention.skipped") + stats.gauge.assert_not_called() + + +def test_task_normalizes_string_retention_config(stats: MagicMock) -> None: + """String values from custom config modules are normalized to integers.""" + mock_app: MagicMock = MagicMock() + mock_app.config = {"SUPERSET_VERSION_HISTORY_RETENTION_DAYS": "30"} + with ( + patch.object(version_history_retention, "current_app", mock_app), + patch.object( + version_history_retention, + "_prune_old_versions_impl", + return_value={"pruned_transactions": 0}, + ) as prune, + ): + result = version_history_retention.prune_old_versions() + + assert result == {"pruned_transactions": 0} + prune.assert_called_once_with(30) + stats.incr.assert_not_called() + + +def test_incomplete_shadow_table_resolution_fails_closed( + stats: MagicMock, +) -> None: + """Missing shadow metadata must abort before the destructive pass.""" + with ( + patch.object( + version_history_retention, + "_resolve_shadow_tables", + side_effect=RuntimeError("missing shadow"), + ), + pytest.raises(RuntimeError, match="missing shadow"), + ): + version_history_retention._prune_old_versions_impl(retention_days=30) + stats.incr.assert_not_called() + + +def test_resolve_shadow_tables_rejects_partial_registry() -> None: + """One missing versioned mapper makes the complete registry unsafe.""" + resolved_table: MagicMock = MagicMock() + + def resolve_version_class(model: type[object]) -> MagicMock: + if model.__name__ == "TableColumn": + raise ClassNotVersioned(model) + version_model = MagicMock() + version_model.__table__ = resolved_table + return version_model + + with patch("sqlalchemy_continuum.version_class", side_effect=resolve_version_class): Review Comment: ✅ **Customized review instruction saved!** **Instruction:** > When the code under test imports a dependency inside the function body, patch the dependency on its source module rather than the consumer module; do not flag source-module patching as incorrect in this pattern. **Applied to:** - `**/test/**` - `**/tests/**` - `**/*test*.py` --- 💡 *To manage or update this instruction, visit: [CodeAnt AI Settings](https://app.codeant.ai/org/settings/learnings)* -- 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]
