|
| 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 | + |
| 18 | + |
| 19 | +from enum import Enum |
| 20 | +from typing import Any |
| 21 | +from unittest.mock import call, Mock, patch |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | +from superset import app |
| 26 | +from superset.utils import decorators |
| 27 | + |
| 28 | + |
| 29 | +class ResponseValues(str, Enum): |
| 30 | + FAIL = "fail" |
| 31 | + WARN = "warn" |
| 32 | + OK = "ok" |
| 33 | + |
| 34 | + |
| 35 | +def test_debounce() -> None: |
| 36 | + mock = Mock() |
| 37 | + |
| 38 | + @decorators.debounce() |
| 39 | + def myfunc(arg1: int, arg2: int, kwarg1: str = "abc", kwarg2: int = 2) -> int: |
| 40 | + mock(arg1, kwarg1) |
| 41 | + return arg1 + arg2 + kwarg2 |
| 42 | + |
| 43 | + # should be called only once when arguments don't change |
| 44 | + myfunc(1, 1) |
| 45 | + myfunc(1, 1) |
| 46 | + result = myfunc(1, 1) |
| 47 | + mock.assert_called_once_with(1, "abc") |
| 48 | + assert result == 4 |
| 49 | + |
| 50 | + # kwarg order shouldn't matter |
| 51 | + myfunc(1, 0, kwarg2=2, kwarg1="haha") |
| 52 | + result = myfunc(1, 0, kwarg1="haha", kwarg2=2) |
| 53 | + mock.assert_has_calls([call(1, "abc"), call(1, "haha")]) |
| 54 | + assert result == 3 |
| 55 | + |
| 56 | + |
| 57 | +def test_statsd_gauge() -> None: |
| 58 | + @decorators.statsd_gauge("custom.prefix") |
| 59 | + def my_func(response: ResponseValues, *args: Any, **kwargs: Any) -> str: |
| 60 | + if response == ResponseValues.FAIL: |
| 61 | + raise ValueError("Error") |
| 62 | + if response == ResponseValues.WARN: |
| 63 | + raise FileNotFoundError("Not found") |
| 64 | + return "OK" |
| 65 | + |
| 66 | + with patch.object(app.config["STATS_LOGGER"], "gauge") as mock: |
| 67 | + my_func(ResponseValues.OK, 1, 2) |
| 68 | + mock.assert_called_once_with("custom.prefix.ok", 1) |
| 69 | + |
| 70 | + with pytest.raises(ValueError): |
| 71 | + my_func(ResponseValues.FAIL, 1, 2) |
| 72 | + mock.assert_called_once_with("custom.prefix.error", 1) |
| 73 | + |
| 74 | + with pytest.raises(FileNotFoundError): |
| 75 | + my_func(ResponseValues.WARN, 1, 2) |
| 76 | + mock.assert_called_once_with("custom.prefix.warn", 1) |
0 commit comments