| 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 typing import Callable, Optional |
| 18 |
from unittest.mock import Mock |
| 19 |
|
| 20 |
import pytest |
| 21 |
from pytest import param |
| 22 |
|
| 23 |
from cylc.flow.cycling import PointBase |
| 24 |
from cylc.flow.cycling.integer import IntegerPoint |
| 25 |
from cylc.flow.cycling.iso8601 import ISO8601Point |
| 26 |
from cylc.flow.flow_mgr import FlowNums |
| 27 |
from cylc.flow.id import Tokens |
| 28 |
from cylc.flow.task_proxy import TaskProxy |
| 29 |
from cylc.flow.taskdef import TaskDef |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.parametrize( |
| 33 |
'itask_point, offset_str, expected', |
| 34 |
[ |
| 35 |
param( # date -u -d 19700101 "+%s" |
| 36 |
ISO8601Point('19700101T00Z'), 'PT0M', 0, id="zero epoch" |
| 37 |
), |
| 38 |
param( # 2025 is not a leap year: Jan 1 + P2M = P59D |
| 39 |
ISO8601Point('20250101T00Z'), 'PT0M', 1735689600, id="nonleap base" |
| 40 |
), |
| 41 |
param( |
| 42 |
ISO8601Point('20250101T00Z'), 'P59D', 1740787200, id="nonleap off1" |
| 43 |
), |
| 44 |
param( |
| 45 |
ISO8601Point('20250101T00Z'), 'P2M', 1740787200, id="nonleap off2" |
| 46 |
), |
| 47 |
param( # 2024 is a leap year: Jan 1 + P2M = P60D |
| 48 |
ISO8601Point('20240101T00Z'), 'PT0M', 1704067200, id="leap base" |
| 49 |
), |
| 50 |
param( |
| 51 |
ISO8601Point('20240101T00Z'), 'P60D', 1709251200, id="leap off1" |
| 52 |
), |
| 53 |
param( |
| 54 |
ISO8601Point('20240101T00Z'), 'P2M', 1709251200, id="leap off2" |
| 55 |
), |
| 56 |
] |
| 57 |
) |
| 58 |
def test_get_clock_trigger_time( |
| 59 |
itask_point: PointBase, |
| 60 |
offset_str: str, |
| 61 |
expected: int, |
| 62 |
set_cycling_type: Callable |
| 63 |
) -> None: |
| 64 |
"""Test get_clock_trigger_time() for exact and inexact offsets.""" |
| 65 |
set_cycling_type(itask_point.TYPE) |
| 66 |
mock_itask = Mock( |
| 67 |
point=itask_point.standardise(), |
| 68 |
clock_trigger_times={} |
| 69 |
) |
| 70 |
assert TaskProxy.get_clock_trigger_time( |
| 71 |
mock_itask, mock_itask.point, offset_str) == expected |
| 72 |
|
| 73 |
|
| 74 |
@pytest.mark.parametrize( |
| 75 |
'name_str, expected', |
| 76 |
[('beer', True), |
| 77 |
('FAM', True), |
| 78 |
('root', True), |
| 79 |
('horse', False), |
| 80 |
('F*', True), |
| 81 |
('*', True)] |
| 82 |
) |
| 83 |
def test_name_match(name_str: str, expected: bool): |
| 84 |
"""Test TaskProxy.name_match(). |
| 85 |
|
| 86 |
For a task named "beer" in family "FAM". |
| 87 |
""" |
| 88 |
mock_tdef = Mock(namespace_hierarchy=['root', 'FAM', 'beer']) |
| 89 |
mock_tdef.name = 'beer' |
| 90 |
mock_itask = Mock(tdef=mock_tdef) |
| 91 |
|
| 92 |
assert TaskProxy.name_match(mock_itask, name_str) is expected |
| 93 |
|
| 94 |
|
| 95 |
@pytest.mark.parametrize( |
| 96 |
'status_str, expected', |
| 97 |
[param('waiting', True, id="Basic"), |
| 98 |
param('w*', False, id="Globs don't work"), |
| 99 |
param(None, True, id="None always matches")] |
| 100 |
) |
| 101 |
def test_status_match(status_str: Optional[str], expected: bool): |
| 102 |
"""Test TaskProxy.status_match(). |
| 103 |
|
| 104 |
For a task with status "waiting". |
| 105 |
""" |
| 106 |
mock_itask = Mock(state=Mock(status='waiting')) |
| 107 |
|
| 108 |
assert TaskProxy.status_match(mock_itask, status_str) is expected |
| 109 |
|
| 110 |
|
| 111 |
@pytest.mark.parametrize('itask_flow_nums, flow_nums, expected', [ |
| 112 |
param({1, 2}, {2}, {2}, id="subset"), |
| 113 |
param({2}, {1, 2}, {2}, id="superset"), |
| 114 |
param({1, 2}, {3, 4}, set(), id="disjoint"), |
| 115 |
param({1, 2}, set(), {1, 2}, id="all-matches-num"), |
| 116 |
param(set(), {1, 2}, set(), id="num-doesnt-match-none"), |
| 117 |
param(set(), set(), set(), id="all-doesnt-match-none"), |
| 118 |
]) |
| 119 |
def test_match_flows( |
| 120 |
itask_flow_nums: FlowNums, flow_nums: FlowNums, expected: FlowNums |
| 121 |
): |
| 122 |
mock_itask = Mock(flow_nums=itask_flow_nums) |
| 123 |
assert TaskProxy.match_flows(mock_itask, flow_nums) == expected |
| 124 |
|
| 125 |
|
| 126 |
def test_match_flows_copy(): |
| 127 |
"""Test that this method does not return the same reference as |
| 128 |
itask.flow_nums, otherwise you could end up unexpectedly mutating |
| 129 |
itask.flow_nums.""" |
| 130 |
mock_itask = Mock(flow_nums={1, 2}) |
| 131 |
result = TaskProxy.match_flows(mock_itask, set()) |
| 132 |
assert result == mock_itask.flow_nums |
| 133 |
assert result is not mock_itask.flow_nums |
| 134 |
|
| 135 |
|
| 136 |
def test_job_tokens(): |
| 137 |
itask = TaskProxy( |
| 138 |
Tokens('wflow'), |
| 139 |
TaskDef('foo', {}, None, None), |
| 140 |
IntegerPoint('10'), |
| 141 |
submit_num=3, |
| 142 |
) |
| 143 |
assert str(itask.job_tokens) == 'wflow//10/foo/03' |