| 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 |
|
| 19 |
import pytest |
| 20 |
|
| 21 |
try: |
| 22 |
from cylc.flow.main_loop.log_data_store import ( |
| 23 |
_dump, |
| 24 |
_plot, |
| 25 |
_iter_data_store |
| 26 |
) |
| 27 |
except ModuleNotFoundError as exc: |
| 28 |
if exc.name == 'pympler': |
| 29 |
pytest.skip( |
| 30 |
'pympler required for these tests', |
| 31 |
allow_module_level=True |
| 32 |
) |
| 33 |
else: |
| 34 |
raise |
| 35 |
|
| 36 |
|
| 37 |
@pytest.fixture() |
| 38 |
def test_data(): |
| 39 |
return { |
| 40 |
'times': [1, 2, 3], |
| 41 |
'objects': { |
| 42 |
'foo': [4, 5, 6] |
| 43 |
}, |
| 44 |
'size': { |
| 45 |
'foo': [7, 8, 9] |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
def test_dump(test_data, tmp_path): |
| 51 |
"""Ensure the data is serialiseable.""" |
| 52 |
assert _dump(test_data, tmp_path) |
| 53 |
assert list(tmp_path.iterdir()) == [ |
| 54 |
Path(tmp_path, 'cylc.flow.main_loop.log_data_store.json') |
| 55 |
] |
| 56 |
|
| 57 |
|
| 58 |
def test_plot_no_data(tmp_path): |
| 59 |
"""Ensure plotting skips if data insufficient.""" |
| 60 |
assert not _plot({'times': [1]}, tmp_path) |
| 61 |
|
| 62 |
|
| 63 |
def test_plot(test_data, tmp_path): |
| 64 |
"""Ensure the plotting mechanism doesn't raise errors.""" |
| 65 |
pytest.importorskip('matplotlib', reason='requires matplotlib') |
| 66 |
assert _plot(test_data, tmp_path) |
| 67 |
assert list(tmp_path.iterdir()) == [ |
| 68 |
Path(tmp_path, 'cylc.flow.main_loop.log_data_store.pdf') |
| 69 |
] |
| 70 |
|
| 71 |
|
| 72 |
def test_iter_data_store(): |
| 73 |
class DataStore: |
| 74 |
tracker = {'this': 'that'} |
| 75 |
data = {'x': {'a': 1, 'workflow': 2, 'c': 3}} |
| 76 |
ds = DataStore() |
| 77 |
assert ( |
| 78 |
list(_iter_data_store(ds)) |
| 79 |
) == [ |
| 80 |
('data_store_mgr (total)', ds), |
| 81 |
('tracker', {'this': 'that'}), |
| 82 |
('data.a', 1), |
| 83 |
('data.workflow', [2]), |
| 84 |
('data.c', 3) |
| 85 |
] |