euci_test.py 5.67 KB
Newer Older
Karel Koci's avatar
Karel Koci committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# Copyright 2019, CZ.NIC z.s.p.o. (http://www.nic.cz/)
#
# This file is part of the PyUCI.
#
# PyUCI is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
# PyUCI is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyUCI.  If not, see <http://www.gnu.org/licenses/>.
import pytest
import euci


21 22 23 24 25
def test_get_string(tmpdir):
    'Test get for no dtype (string in default)'
    tmpdir.join('test').write("""
config str 'str'
    option foo 'value'
26 27
    list list 'value1'
    list list 'value2'
28 29 30
""")
    u = euci.EUci(confdir=tmpdir.strpath)
    assert u.get('test', 'str', 'foo') == 'value'
31
    assert u.get('test', 'str', 'list') == ('value1', 'value2')
32 33 34 35 36 37 38 39 40


def test_set_string(tmpdir):
    'Test set for type string'
    tmpdir.join('test').write("""
config testing 'testing'
""")
    u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
    u.set('test', 'testing', 'foo', 'value')
41
    u.set('test', 'testing', 'list', ('f', 'o', 'o'))
42
    assert u.get('test', 'testing', 'foo') == 'value'
43
    assert u.get('test', 'testing', 'list') == ('f', 'o', 'o')
44 45 46 47


def test_get_boolean(tmpdir):
    'Test get for dtype boolean'
Karel Koci's avatar
Karel Koci committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    tmpdir.join('test').write("""
config integer 'integer'
    option true '1'
    option false '0'

config word 'word'
    option true 'yes'
    option false 'no'

config state 'state'
    option true 'on'
    option false 'off'

config bool 'bool'
    option true 'true'
    option false 'false'

config bled 'bled'
    option true 'enabled'
    option false 'disabled'
68 69 70 71 72 73

config list 'list'
    list true '1'
    list true 'yes'
    list false '0'
    list false 'no'
Karel Koci's avatar
Karel Koci committed
74 75
""")
    u = euci.EUci(confdir=tmpdir.strpath)
76 77 78 79 80
    assert u.get('test', 'integer', 'true', dtype=bool)
    assert u.get('test', 'word', 'true', dtype=bool)
    assert u.get('test', 'state', 'true', dtype=bool)
    assert u.get('test', 'bool', 'true', dtype=bool)
    assert u.get('test', 'bled', 'true', dtype=bool)
81
    assert u.get('test', 'list', 'true', dtype=bool) == (True, True)
82 83 84 85 86
    assert not u.get('test', 'integer', 'false', dtype=bool)
    assert not u.get('test', 'word', 'false', dtype=bool)
    assert not u.get('test', 'state', 'false', dtype=bool)
    assert not u.get('test', 'bool', 'false', dtype=bool)
    assert not u.get('test', 'bled', 'false', dtype=bool)
87
    assert u.get('test', 'list', 'false', dtype=bool) == (False, False)
88 89 90 91


def test_set_boolean(tmpdir):
    'Test set for type boolean'
Karel Koci's avatar
Karel Koci committed
92 93 94 95
    tmpdir.join('test').write("""
config testing 'testing'
""")
    u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
96 97
    u.set('test', 'testing', 'true', True)
    u.set('test', 'testing', 'false', False)
98
    u.set('test', 'testing', 'list', (True, False, True, False))
Karel Koci's avatar
Karel Koci committed
99 100
    assert u.get('test', 'testing', 'true') == '1'
    assert u.get('test', 'testing', 'false') == '0'
101
    assert u.get('test', 'testing', 'list') == ('1', '0', '1', '0')
Karel Koci's avatar
Karel Koci committed
102 103


104 105
def test_get_integer(tmpdir):
    'Test get for dtype int'
Karel Koci's avatar
Karel Koci committed
106 107 108 109
    tmpdir.join('test').write("""
config integer 'integer'
    option plus '42'
    option minus '-42'
110 111 112 113
    list primes '2'
    list primes '3'
    list primes '5'
    list primes '7'
Karel Koci's avatar
Karel Koci committed
114 115
""")
    u = euci.EUci(confdir=tmpdir.strpath)
116 117
    assert u.get('test', 'integer', 'plus', dtype=int) == 42
    assert u.get('test', 'integer', 'minus', dtype=int) == -42
118
    assert u.get('test', 'integer', 'primes', dtype=int) == (2, 3, 5, 7)
Karel Koci's avatar
Karel Koci committed
119 120


121 122
def test_set_integer(tmpdir):
    'Test set for type int'
Karel Koci's avatar
Karel Koci committed
123 124 125 126
    tmpdir.join('test').write("""
config testing 'testing'
""")
    u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
127 128
    u.set('test', 'testing', 'plus', 42)
    u.set('test', 'testing', 'minus', -42)
129
    u.set('test', 'testing', 'primes', (2, 3, 5, 7))
Karel Koci's avatar
Karel Koci committed
130 131
    assert u.get('test', 'testing', 'plus') == '42'
    assert u.get('test', 'testing', 'minus') == '-42'
132
    assert u.get('test', 'testing', 'primes') == ('2', '3', '5', '7')
Karel Koci's avatar
Karel Koci committed
133 134


135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
def test_get_default(tmpdir):
    'Test get method with default value'
    tmpdir.join('test').write("""
config testing 'testing'
""")
    u = euci.EUci(confdir=tmpdir.strpath)
    with pytest.raises(euci.UciExceptionNotFound):
        u.get('test', 'str', 'foo')
    assert u.get('test', 'str', 'foo', default='value') == 'value'
    assert u.get('test', 'str', 'foo', dtype=bool, default='true')
    assert u.get('test', 'str', 'foo', dtype=bool, default=True)
    assert u.get('test', 'str', 'foo', dtype=int, default=-42) == -42
    assert u.get('test', 'str', 'foo', dtype=int, default='-42') == -42


Karel Koci's avatar
Karel Koci committed
150 151 152 153 154 155 156 157
def test_context(tmpdir):
    'Test context with EUci'
    tmpdir.join('test').write("""
config testing 'testing'
    option one '0'
    option two '1'
""")
    with euci.EUci(confdir=tmpdir.strpath) as u:
158 159
        assert not u.get('test', 'testing', 'one', dtype=bool)
        assert u.get('test', 'testing', 'two', dtype=bool)
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174


def test_get_list(tmpdir):
    'Test get with list'
    tmpdir.join('test').write("""
config testing 'testing'
    option option '0'
    list list '0'
    list list '1'
""")
    u = euci.EUci(confdir=tmpdir.strpath)
    assert u.get('test', 'testing', 'option', dtype=bool, list=True) == (False,)
    assert not u.get('test', 'testing', 'option', dtype=bool, list=False)
    assert u.get('test', 'testing', 'list', dtype=bool, list=True) == (False, True)
    assert not u.get('test', 'testing', 'list', dtype=bool, list=False)