|
| 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 | +from __future__ import annotations |
| 19 | + |
| 20 | +from datetime import timedelta |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +from airflow.models.variable import Variable |
| 25 | +from airflow.serialization.definitions.deadline import SerializedVariableInterval |
| 26 | + |
| 27 | + |
| 28 | +class TestVariableInterval: |
| 29 | + @pytest.mark.parametrize( |
| 30 | + ("value", "expected"), |
| 31 | + [ |
| 32 | + ("3", timedelta(seconds=3)), |
| 33 | + ("10", timedelta(seconds=10)), |
| 34 | + ("05", timedelta(seconds=5)), |
| 35 | + ("0", timedelta(0)), |
| 36 | + ("-5", timedelta(seconds=-5)), |
| 37 | + ], |
| 38 | + ) |
| 39 | + def test_resolve_valid(self, mocker, value, expected): |
| 40 | + mocker.patch.object(Variable, "get", return_value=value) |
| 41 | + |
| 42 | + interval = SerializedVariableInterval(key="test_interval") |
| 43 | + |
| 44 | + assert interval.resolve() == expected |
| 45 | + |
| 46 | + @pytest.mark.parametrize( |
| 47 | + ("value", "raise_missing", "match"), |
| 48 | + [ |
| 49 | + (None, True, "not found"), |
| 50 | + ("abc", False, "must be an integer"), |
| 51 | + ("", False, "must be an integer"), |
| 52 | + ], |
| 53 | + ) |
| 54 | + def test_resolve_invalid(self, mocker, value, raise_missing, match): |
| 55 | + if raise_missing: |
| 56 | + mocker.patch.object( |
| 57 | + Variable, |
| 58 | + "get", |
| 59 | + side_effect=KeyError("test_interval"), |
| 60 | + ) |
| 61 | + else: |
| 62 | + mocker.patch.object(Variable, "get", return_value=value) |
| 63 | + |
| 64 | + interval = SerializedVariableInterval(key="test_interval") |
| 65 | + |
| 66 | + with pytest.raises(ValueError, match=match): |
| 67 | + interval.resolve() |
0 commit comments