| 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 pathlib import Path |
| 18 |
from typing import ( |
| 19 |
List, |
| 20 |
Set, |
| 21 |
) |
| 22 |
from unittest.mock import Mock |
| 23 |
|
| 24 |
import pytest |
| 25 |
from pytest import param |
| 26 |
|
| 27 |
from cylc.flow.cycling.integer import IntegerPoint |
| 28 |
from cylc.flow.flow_mgr import FlowNums |
| 29 |
from cylc.flow.id import Tokens |
| 30 |
from cylc.flow.task_proxy import TaskProxy |
| 31 |
from cylc.flow.taskdef import TaskDef |
| 32 |
from cylc.flow.util import serialise_set |
| 33 |
from cylc.flow.workflow_db_mgr import WorkflowDatabaseManager |
| 34 |
|
| 35 |
|
| 36 |
@pytest.mark.parametrize('flow_nums, expected_removed', [ |
| 37 |
param(set(), {1, 2, 5}, id='all'), |
| 38 |
param({1}, {1}, id='subset'), |
| 39 |
param({1, 2, 5}, {1, 2, 5}, id='complete-set'), |
| 40 |
param({1, 3, 5}, {1, 5}, id='intersect'), |
| 41 |
param({3, 4}, set(), id='disjoint'), |
| 42 |
]) |
| 43 |
def test_remove_task_from_flows( |
| 44 |
tmp_path: Path, flow_nums: FlowNums, expected_removed: FlowNums |
| 45 |
): |
| 46 |
db_flows: List[FlowNums] = [ |
| 47 |
{1, 2}, |
| 48 |
{5}, |
| 49 |
set(), # FLOW_NONE |
| 50 |
] |
| 51 |
expected_remaining = { |
| 52 |
serialise_set(flow - expected_removed) for flow in db_flows |
| 53 |
} |
| 54 |
db_mgr = WorkflowDatabaseManager(tmp_path) |
| 55 |
schd_tokens = Tokens('~asterix/gaul') |
| 56 |
tdef = TaskDef('a', rtcfg={}, start_point=None, initial_point=None) |
| 57 |
with db_mgr.get_pri_dao() as dao: |
| 58 |
db_mgr.pri_dao = dao |
| 59 |
db_mgr.pub_dao = Mock() |
| 60 |
for flow in db_flows: |
| 61 |
itask = TaskProxy( |
| 62 |
schd_tokens, tdef, IntegerPoint('1'), flow_nums=flow |
| 63 |
) |
| 64 |
db_mgr.put_insert_task_states(itask) |
| 65 |
db_mgr.put_insert_task_outputs(itask) |
| 66 |
db_mgr.process_queued_ops() |
| 67 |
|
| 68 |
removed_fnums = db_mgr.remove_task_from_flows('1', 'a', flow_nums) |
| 69 |
assert removed_fnums == expected_removed |
| 70 |
|
| 71 |
db_mgr.process_queued_ops() |
| 72 |
for table in ('task_states', 'task_outputs'): |
| 73 |
remaining_fnums: Set[str] = { |
| 74 |
fnums_str |
| 75 |
for fnums_str, *_ in dao.connect().execute( |
| 76 |
f'SELECT flow_nums FROM {table}' |
| 77 |
) |
| 78 |
} |
| 79 |
assert remaining_fnums == expected_remaining |