euci_test.py 6.67 KB
Newer Older
1
# Copyright 2020, CZ.NIC z.s.p.o. (http://www.nic.cz/)
Karel Koci's avatar
Karel Koci committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# 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

20 21
from ipaddress import IPv4Address, IPv6Address

Karel Koci's avatar
Karel Koci committed
22

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


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')
43
    u.set('test', 'testing', 'list', ('f', 'o', 'o'))
44
    assert u.get('test', 'testing', 'foo') == 'value'
45
    assert u.get('test', 'testing', 'list') == ('f', 'o', 'o')
46 47 48 49


def test_get_boolean(tmpdir):
    'Test get for dtype boolean'
Karel Koci's avatar
Karel Koci committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    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'
70 71 72 73 74 75

config list 'list'
    list true '1'
    list true 'yes'
    list false '0'
    list false 'no'
Karel Koci's avatar
Karel Koci committed
76 77
""")
    u = euci.EUci(confdir=tmpdir.strpath)
78 79 80 81 82
    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)
83
    assert u.get('test', 'list', 'true', dtype=bool) == (True, True)
84 85 86 87 88
    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)
89
    assert u.get('test', 'list', 'false', dtype=bool) == (False, False)
90 91 92 93


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


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


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


137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
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
152 153 154 155 156 157 158 159
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:
160 161
        assert not u.get('test', 'testing', 'one', dtype=bool)
        assert u.get('test', 'testing', 'two', dtype=bool)
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176


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)
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201


def test_ip_address_type(tmpdir):
    'Test ip address type'
    tmpdir.join('test').write(
        """
config testing 'testing'
    option option '0'
    option one '192.168.1.1'
    option two '::1'
    list three '::2'
    list three '10.0.0.1'
"""
    )
    with euci.EUci(confdir=tmpdir.strpath) as u:
        assert u.get('test', 'testing', 'one', dtype=IPv4Address) == IPv4Address('192.168.1.1')
        assert u.get('test', 'testing', 'one', dtype=IPv6Address) == IPv4Address('192.168.1.1')
        assert u.get('test', 'testing', 'two', dtype=IPv4Address) == IPv6Address('::1')
        assert u.get('test', 'testing', 'two', dtype=IPv6Address) == IPv6Address('::1')
        assert u.get('test', 'testing', 'three', dtype=IPv4Address, list=True) == (
            IPv6Address('::2'), IPv4Address('10.0.0.1')
        )
        assert u.get('test', 'testing', 'three', dtype=IPv6Address, list=True) == (
            IPv6Address('::2'), IPv4Address('10.0.0.1')
        )