| 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 |
import pytest |
| 18 |
|
| 19 |
from cylc.flow.parsec.config import ConfigNode as Conf |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture(scope='module') |
| 23 |
def basic_config(): |
| 24 |
"""A basic config with a file, section and setting.""" |
| 25 |
with Conf('file.cylc') as file_: |
| 26 |
with Conf('section') as section: |
| 27 |
setting = Conf('setting') |
| 28 |
return (file_, section, setting) |
| 29 |
|
| 30 |
|
| 31 |
def test_config_node(basic_config): |
| 32 |
"""It should associate parents & children in a tree.""" |
| 33 |
file_, section, setting = basic_config |
| 34 |
assert file_.name == 'file.cylc' |
| 35 |
assert file_._parent is None |
| 36 |
assert file_._children == {'section': section} |
| 37 |
|
| 38 |
assert section.name == 'section' |
| 39 |
assert section._parent == file_ |
| 40 |
assert section._children == {'setting': setting} |
| 41 |
|
| 42 |
assert setting.name == 'setting' |
| 43 |
assert setting._parent == section |
| 44 |
assert setting._children is None |
| 45 |
|
| 46 |
|
| 47 |
def test_config_str(basic_config): |
| 48 |
"""A node should str as a relative path from its parent node..""" |
| 49 |
file_, section, setting = basic_config |
| 50 |
assert str(file_) == 'file.cylc' |
| 51 |
assert str(section) == '[section]' |
| 52 |
assert str(setting) == 'setting' |
| 53 |
|
| 54 |
|
| 55 |
def test_config_repr(basic_config): |
| 56 |
"""A node should repr as a full path.""" |
| 57 |
file_, section, setting = basic_config |
| 58 |
assert repr(file_) == 'file.cylc' |
| 59 |
assert repr(section) == 'file.cylc[section]' |
| 60 |
assert repr(setting) == 'file.cylc[section]setting' |
| 61 |
|
| 62 |
|
| 63 |
@pytest.fixture(scope='module') |
| 64 |
def many_setting(): |
| 65 |
"""A config containing a user-definable setting.""" |
| 66 |
with Conf('file.cylc') as file_: |
| 67 |
Conf('<setting>') # __MANY__ |
| 68 |
return file_ |
| 69 |
|
| 70 |
|
| 71 |
def test_many_setting(many_setting): |
| 72 |
"""It should recognise this is a user-definable setting.""" |
| 73 |
setting = list(many_setting)[0] |
| 74 |
assert setting.name == '__MANY__' |
| 75 |
assert setting.display_name == '<setting>' |
| 76 |
assert str(setting) == '<setting>' |
| 77 |
assert repr(setting) == 'file.cylc|<setting>' |
| 78 |
|
| 79 |
|
| 80 |
@pytest.fixture(scope='module') |
| 81 |
def many_section(): |
| 82 |
"""A config containing a user-definable section.""" |
| 83 |
with Conf('file.cylc') as file_: |
| 84 |
with Conf('<section>'): |
| 85 |
Conf('setting') |
| 86 |
return file_ |
| 87 |
|
| 88 |
|
| 89 |
def test_many_section(many_section): |
| 90 |
"""It should recognise this is a user-definable section.""" |
| 91 |
section = list(many_section)[0] |
| 92 |
assert section.name == '__MANY__' |
| 93 |
assert section.display_name == '<section>' |
| 94 |
assert str(section) == '[<section>]' |
| 95 |
assert repr(section) == 'file.cylc[<section>]' |
| 96 |
setting = list(section)[0] |
| 97 |
assert str(setting) == 'setting' |
| 98 |
assert repr(setting) == 'file.cylc[<section>]setting' |
| 99 |
|
| 100 |
|
| 101 |
@pytest.fixture(scope='module') |
| 102 |
def meta_conf(): |
| 103 |
"""A config with an inherited section.""" |
| 104 |
with Conf('Foo') as spec: |
| 105 |
with Conf('<X>') as template: |
| 106 |
Conf('a', default='a') |
| 107 |
Conf('b', default='b') |
| 108 |
with Conf('y', meta=template) as copy: |
| 109 |
Conf('a', default='c') |
| 110 |
return spec, template, copy |
| 111 |
|
| 112 |
|
| 113 |
def test_meta(meta_conf): |
| 114 |
"""It should inherit sections using the meta kwarg.""" |
| 115 |
spec, template, copy = meta_conf |
| 116 |
assert template.meta is None |
| 117 |
assert copy.meta == template |
| 118 |
# make sure the template is unaffected |
| 119 |
assert template['a'].default == 'a' |
| 120 |
assert template['b'].default == 'b' |
| 121 |
# make sure the copy is affected |
| 122 |
assert copy['a'].default == 'c' |
| 123 |
assert copy['b'].default == 'b' |
| 124 |
# make sure inherited configurations are marked accordingly |
| 125 |
assert copy['a'].meta is None # not inherited |
| 126 |
assert copy['b'].meta is True # inherited |