Vitor-Avila commented on code in PR #28176: URL: https://github.com/apache/superset/pull/28176#discussion_r1589736955
########## tests/unit_tests/commands/report/base_test.py: ########## @@ -0,0 +1,191 @@ +# 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 functools import wraps +from typing import Any, Callable +from unittest.mock import patch + +import pytest + +from superset.commands.report.base import BaseReportScheduleCommand +from superset.commands.report.exceptions import ReportScheduleFrequencyNotAllowed +from superset.reports.models import ReportScheduleType + +REPORT_TYPES = { + ReportScheduleType.ALERT, + ReportScheduleType.REPORT, +} + +TEST_SCHEDULES_EVERY_MINUTE = { + "* * * * *", + "1-5 * * * *", + "10-20 * * * *", + "0,45,10-20 * * * *", + "23,45,50,51 * * * *", + "10,20,30,40-45 * * * *", +} + +TEST_SCHEDULES_SINGLE_MINUTES = { + "1,5,8,10,12 * * * *", + "10 1 * * *", + "27,2 1-5 * * *", +} + +TEST_SCHEDULES = TEST_SCHEDULES_EVERY_MINUTE.union(TEST_SCHEDULES_SINGLE_MINUTES) + + +def app_custom_config( + alert_minimum_interval: int | None = None, + report_minimum_interval: int | None = None, +) -> Callable[[Callable[..., Any]], Callable[..., Any]]: + """ + Decorator to mock the current_app.config values dynamically for each test. + + :param alert_minimum_interval: Minimum interval in minutes for alerts. Defaults to None. + :param report_minimum_interval: Minimum interval in minutes for reports. Defaults to None. + + :returns: A decorator that wraps a function. + """ + + def decorator(func: Callable[..., Any]) -> Callable[..., Any]: + @wraps(func) + def wrapper(*args: Any, **kwargs: Any) -> Any: + with patch( + "superset.commands.report.base.current_app.config" + ) as mock_config: + mock_config.get.side_effect = lambda key: { + "ALERT_MINIMUM_INTERVAL_MINUTES": alert_minimum_interval, + "REPORT_MINIMUM_INTERVAL_MINUTES": report_minimum_interval, + }.get(key) + return func(*args, **kwargs) + + return wrapper + + return decorator + + +@app_custom_config() +def test_validate_report_frequency() -> None: + """ + Test the ``validate_report_frequency`` method when there's + no minimum frequency configured. + """ + for report_type in REPORT_TYPES: + for schedule in TEST_SCHEDULES: + BaseReportScheduleCommand().validate_report_frequency( + schedule, + report_type, + ) + + +@app_custom_config(alert_minimum_interval=4, report_minimum_interval=5) +def test_validate_report_frequency_minimum_set() -> None: + """ + Test the ``validate_report_frequency`` method when there's + minimum frequencies configured. + """ + + BaseReportScheduleCommand().validate_report_frequency( + "1,5 * * * *", + ReportScheduleType.ALERT, + ) + BaseReportScheduleCommand().validate_report_frequency( + "6,11 * * * *", + ReportScheduleType.REPORT, + ) + + +@app_custom_config(alert_minimum_interval=2, report_minimum_interval=5) +def test_validate_report_frequency_invalid_schedule() -> None: + """ + Test the ``validate_report_frequency`` method when the configured + schedule exceeds the limit. + """ + with pytest.raises(ReportScheduleFrequencyNotAllowed): + BaseReportScheduleCommand().validate_report_frequency( + "1,2 * * * *", + ReportScheduleType.ALERT, + ) + + with pytest.raises(ReportScheduleFrequencyNotAllowed): + BaseReportScheduleCommand().validate_report_frequency( + "1,5 * * * *", + ReportScheduleType.REPORT, + ) + + +@app_custom_config(alert_minimum_interval=10) +def test_validate_report_frequency_alert_only() -> None: + """ + Test the ``validate_report_frequency`` method when there's + only a configuration for alerts and user is creating report. + """ + for schedule in TEST_SCHEDULES: Review Comment: ohh, wasn't familiar with it! just added it -- 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]
