Debian Codegraph @

tests/u/test_flow_mgr.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 """Unit tests for FlowManager."""
18
19 import pytest
20 import datetime
21 import logging
22
23 from cylc.flow.flow_mgr import FlowMgr
24 from cylc.flow.workflow_db_mgr import WorkflowDatabaseManager
25 from cylc.flow import CYLC_LOG
26
27
28 FAKE_NOW = datetime.datetime(2020, 12, 25, 17, 5, 55)
29 FAKE_NOW_ISO = FAKE_NOW.isoformat()
30
31
32 @pytest.fixture
33 def patch_datetime_now(monkeypatch):
34
35 class mydatetime:
36 @classmethod
37 def now(cls, tz=None):
38 return FAKE_NOW
39
40 monkeypatch.setattr(datetime, 'datetime', mydatetime)
41
42
43 def test_all(
44 patch_datetime_now,
45 caplog: pytest.LogCaptureFixture,
46 ):
47 """Test flow number management."""
48
49 db_mgr = WorkflowDatabaseManager()
50 flow_mgr = FlowMgr(db_mgr)
51 caplog.set_level(logging.DEBUG, CYLC_LOG)
52
53 meta = "the quick brown fox"
54 assert flow_mgr.get_flow(None, meta) == 1
55 msg1 = f"flow: 1 ({meta}) {FAKE_NOW_ISO}"
56 assert f"New {msg1}" in caplog.messages
57
58 # automatic: expect 2
59 meta = "jumped over the lazy dog"
60 assert flow_mgr.get_flow(None, meta) == 2
61 msg2 = f"flow: 2 ({meta}) {FAKE_NOW_ISO}"
62 assert f"New {msg2}" in caplog.messages
63
64 # give flow 2: not a new flow
65 meta = "jumped over the moon"
66 assert flow_mgr.get_flow(2, meta) == 2
67 msg3 = f"flow: 2 ({meta}) {FAKE_NOW_ISO}"
68 assert f"New {msg3}" not in caplog.messages
69 assert (
70 f"Ignoring flow metadata \"{meta}\": 2 is not a new flow"
71 in caplog.messages
72 )
73
74 # give flow 4: new flow
75 meta = "jumped over the cheese"
76 assert flow_mgr.get_flow(4, meta) == 4
77 msg4 = f"flow: 4 ({meta}) {FAKE_NOW_ISO}"
78 assert f"New {msg4}" in caplog.messages
79
80 # automatic: expect 3
81 meta = "jumped over the log"
82 assert flow_mgr.get_flow(None, meta) == 3
83 msg5 = f"flow: 3 ({meta}) {FAKE_NOW_ISO}"
84 assert f"New {msg5}" in caplog.messages
85
86 # automatic: expect 5 (skip over 4)
87 meta = "crawled under the log"
88 assert flow_mgr.get_flow(None, meta) == 5
89 msg6 = f"flow: 5 ({meta}) {FAKE_NOW_ISO}"
90 assert f"New {msg6}" in caplog.messages
91 flow_mgr._log()
92 assert (
93 "Flows:\n"
94 f"{msg1}\n"
95 f"{msg2}\n"
96 f"{msg4}\n"
97 f"{msg5}\n"
98 f"{msg6}"
99 ) in caplog.messages