|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel |
| 18 | + |
| 19 | +import pandas as pd |
| 20 | +from pytest_mock import MockFixture |
| 21 | + |
| 22 | + |
| 23 | +def test_execute_query_succeeded_no_retry( |
| 24 | + mocker: MockFixture, app_context: None |
| 25 | +) -> None: |
| 26 | + |
| 27 | + from superset.reports.commands.alert import AlertCommand |
| 28 | + |
| 29 | + execute_query_mock = mocker.patch( |
| 30 | + "superset.reports.commands.alert.AlertCommand._execute_query", |
| 31 | + side_effect=lambda: pd.DataFrame([{"sample_col": 0}]), |
| 32 | + ) |
| 33 | + |
| 34 | + command = AlertCommand(report_schedule=mocker.Mock()) |
| 35 | + |
| 36 | + command.validate() |
| 37 | + |
| 38 | + assert execute_query_mock.call_count == 1 |
| 39 | + |
| 40 | + |
| 41 | +def test_execute_query_succeeded_with_retries( |
| 42 | + mocker: MockFixture, app_context: None |
| 43 | +) -> None: |
| 44 | + from superset.reports.commands.alert import AlertCommand, AlertQueryError |
| 45 | + |
| 46 | + execute_query_mock = mocker.patch( |
| 47 | + "superset.reports.commands.alert.AlertCommand._execute_query" |
| 48 | + ) |
| 49 | + |
| 50 | + query_executed_count = 0 |
| 51 | + # Should match the value defined in superset_test_config.py |
| 52 | + expected_max_retries = 3 |
| 53 | + |
| 54 | + def _mocked_execute_query() -> pd.DataFrame: |
| 55 | + nonlocal query_executed_count |
| 56 | + query_executed_count += 1 |
| 57 | + |
| 58 | + if query_executed_count < expected_max_retries: |
| 59 | + raise AlertQueryError() |
| 60 | + else: |
| 61 | + return pd.DataFrame([{"sample_col": 0}]) |
| 62 | + |
| 63 | + execute_query_mock.side_effect = _mocked_execute_query |
| 64 | + execute_query_mock.__name__ = "mocked_execute_query" |
| 65 | + |
| 66 | + command = AlertCommand(report_schedule=mocker.Mock()) |
| 67 | + |
| 68 | + command.validate() |
| 69 | + |
| 70 | + assert execute_query_mock.call_count == expected_max_retries |
| 71 | + |
| 72 | + |
| 73 | +def test_execute_query_failed_no_retry(mocker: MockFixture, app_context: None) -> None: |
| 74 | + from superset.reports.commands.alert import AlertCommand, AlertQueryTimeout |
| 75 | + |
| 76 | + execute_query_mock = mocker.patch( |
| 77 | + "superset.reports.commands.alert.AlertCommand._execute_query" |
| 78 | + ) |
| 79 | + |
| 80 | + def _mocked_execute_query() -> None: |
| 81 | + raise AlertQueryTimeout |
| 82 | + |
| 83 | + execute_query_mock.side_effect = _mocked_execute_query |
| 84 | + execute_query_mock.__name__ = "mocked_execute_query" |
| 85 | + |
| 86 | + command = AlertCommand(report_schedule=mocker.Mock()) |
| 87 | + |
| 88 | + try: |
| 89 | + command.validate() |
| 90 | + except AlertQueryTimeout: |
| 91 | + pass |
| 92 | + |
| 93 | + assert execute_query_mock.call_count == 1 |
| 94 | + |
| 95 | + |
| 96 | +def test_execute_query_failed_max_retries( |
| 97 | + mocker: MockFixture, app_context: None |
| 98 | +) -> None: |
| 99 | + from superset.reports.commands.alert import AlertCommand, AlertQueryError |
| 100 | + |
| 101 | + execute_query_mock = mocker.patch( |
| 102 | + "superset.reports.commands.alert.AlertCommand._execute_query" |
| 103 | + ) |
| 104 | + |
| 105 | + def _mocked_execute_query() -> None: |
| 106 | + raise AlertQueryError |
| 107 | + |
| 108 | + execute_query_mock.side_effect = _mocked_execute_query |
| 109 | + execute_query_mock.__name__ = "mocked_execute_query" |
| 110 | + |
| 111 | + command = AlertCommand(report_schedule=mocker.Mock()) |
| 112 | + |
| 113 | + try: |
| 114 | + command.validate() |
| 115 | + except AlertQueryError: |
| 116 | + pass |
| 117 | + |
| 118 | + # Should match the value defined in superset_test_config.py |
| 119 | + assert execute_query_mock.call_count == 3 |
0 commit comments