Debian Codegraph @

tests/u/test_task_pool.py

raw
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
18 from typing import List
19 from unittest.mock import Mock
20
21 import pytest
22
23 from cylc.flow.flow_mgr import FlowNums
24 from cylc.flow.prerequisite import SatisfiedState
25 from cylc.flow.task_pool import TaskPool
26
27
28 @pytest.mark.parametrize('pool, db_fnums, expected', [
29 pytest.param(
30 [{1, 2}, {2, 3}],
31 {5, 6},
32 {1, 2, 3},
33 id="all-active"
34 ),
35 pytest.param(
36 [set(), set()],
37 {5, 6},
38 {5, 6},
39 id="from-db"
40 ),
41 pytest.param(
42 [set()],
43 set(),
44 {1},
45 id="fallback" # see https://github.com/cylc/cylc-flow/pull/6445
46 ),
47 ])
48 def test_get_active_flow_nums(
49 pool: List[FlowNums], db_fnums: FlowNums, expected
50 ):
51 mock_task_pool = Mock(
52 get_tasks=lambda: [Mock(flow_nums=fnums) for fnums in pool],
53 )
54 mock_task_pool.workflow_db_mgr.pri_dao.select_latest_flow_nums = (
55 lambda: db_fnums
56 )
57
58 assert TaskPool._get_active_flow_nums(mock_task_pool) == expected
59
60
61 @pytest.mark.parametrize('output_msg, flow_nums, db_flow_nums, expected', [
62 ('foo', set(), {1}, False),
63 ('foo', set(), set(), False),
64 ('foo', {1, 3}, {1}, 'satisfied from database'),
65 ('goo', {1, 3}, {1, 2}, 'satisfied from database'),
66 ('foo', {1, 3}, set(), False),
67 ('foo', {2}, {1}, False),
68 ('foo', {2}, {1, 2}, 'satisfied from database'),
69 ('f', {1}, {1}, False),
70 ])
71 def test_check_output(
72 output_msg: str,
73 flow_nums: set,
74 db_flow_nums: set,
75 expected: SatisfiedState,
76 ):
77 mock_task_pool = Mock()
78 mock_task_pool.workflow_db_mgr.pri_dao.select_task_outputs.return_value = {
79 '{"f": "foo", "g": "goo"}': db_flow_nums,
80 }
81
82 assert TaskPool.check_task_output(
83 mock_task_pool, '2000', 'haddock', output_msg, flow_nums
84 ) == expected