| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
from cylc.flow.xtriggers.wall_clock import _wall_clock, validate |
| 18 |
from metomi.isodatetime.parsers import DurationParser |
| 19 |
import pytest |
| 20 |
from pytest import param |
| 21 |
|
| 22 |
|
| 23 |
@pytest.mark.parametrize('trigger_time, expected', [ |
| 24 |
(499, True), |
| 25 |
(500, False), |
| 26 |
]) |
| 27 |
def test_wall_clock( |
| 28 |
monkeypatch: pytest.MonkeyPatch, trigger_time: int, expected: bool |
| 29 |
): |
| 30 |
monkeypatch.setattr( |
| 31 |
'cylc.flow.xtriggers.wall_clock.time', lambda: 500 |
| 32 |
) |
| 33 |
assert _wall_clock(trigger_time) == expected |
| 34 |
|
| 35 |
|
| 36 |
@pytest.fixture |
| 37 |
def monkeypatch_interval_parser(monkeypatch): |
| 38 |
"""Interval parse only works normally if a WorkflowSpecifics |
| 39 |
object identify the parser to be used. |
| 40 |
""" |
| 41 |
monkeypatch.setattr( |
| 42 |
'cylc.flow.xtriggers.wall_clock.interval_parse', |
| 43 |
DurationParser().parse |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
def test_validate_good(monkeypatch_interval_parser): |
| 48 |
validate({'offset': 'PT1H'}) |
| 49 |
|
| 50 |
|
| 51 |
@pytest.mark.parametrize( |
| 52 |
'args, err', ( |
| 53 |
param({'offset': 1}, "^Invalid", id='invalid-offset-int'), |
| 54 |
param({'offset': 'Zaphod'}, "^Invalid", id='invalid-offset-str'), |
| 55 |
) |
| 56 |
) |
| 57 |
def test_validate_exceptions( |
| 58 |
monkeypatch_interval_parser, args, err |
| 59 |
): |
| 60 |
with pytest.raises(Exception, match=err): |
| 61 |
validate(args) |