euci_test.py 5.26 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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
# 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


def test_boolean_get(tmpdir):
    'Test get_boolean'
    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'
""")
    u = euci.EUci(confdir=tmpdir.strpath)
    assert u.get_boolean('test', 'integer', 'true')
    assert u.get_boolean('test', 'word', 'true')
    assert u.get_boolean('test', 'state', 'true')
    assert u.get_boolean('test', 'bool', 'true')
    assert u.get_boolean('test', 'bled', 'true')
    assert not u.get_boolean('test', 'integer', 'false')
    assert not u.get_boolean('test', 'word', 'false')
    assert not u.get_boolean('test', 'state', 'false')
    assert not u.get_boolean('test', 'bool', 'false')
    assert not u.get_boolean('test', 'bled', 'false')


def test_boolean_set(tmpdir):
    'Test set_boolean'
    tmpdir.join('test').write("""
config testing 'testing'
""")
    u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
    u.set_boolean('test', 'testing', 'true', True)
    u.set_boolean('test', 'testing', 'false', False)
    assert u.get('test', 'testing', 'true') == '1'
    assert u.get('test', 'testing', 'false') == '0'


def test_integer_get(tmpdir):
    'Test get_integer'
    tmpdir.join('test').write("""
config integer 'integer'
    option plus '42'
    option minus '-42'
""")
    u = euci.EUci(confdir=tmpdir.strpath)
    assert u.get_integer('test', 'integer', 'plus') == 42
    assert u.get_integer('test', 'integer', 'minus') == -42


def test_integer_set(tmpdir):
    'Test set_integer'
    tmpdir.join('test').write("""
config testing 'testing'
""")
    u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
    u.set_integer('test', 'testing', 'plus', 42)
    u.set_integer('test', 'testing', 'minus', -42)
    assert u.get('test', 'testing', 'plus') == '42'
    assert u.get('test', 'testing', 'minus') == '-42'
Karel Koci's avatar
Karel Koci committed
91 92 93 94 95 96 97 98 99 100 101 102


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:
        assert not u.get_boolean('test', 'testing', 'one')
        assert u.get_boolean('test', 'testing', 'two')
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180


def test_get_t_string(tmpdir):
    'Test get_t method for str type'
    tmpdir.join('test').write("""
config str 'str'
    option foo 'value'
""")
    u = euci.EUci(confdir=tmpdir.strpath)
    v = u.get_t(str, 'test', 'str', 'foo')
    assert type(v) == str
    assert v == 'value'


def test_get_t_boolean(tmpdir):
    'Test get_t method for boolean type'
    tmpdir.join('test').write("""
config bool 'bool'
    option true 'true'
    option false 'false'
""")
    u = euci.EUci(confdir=tmpdir.strpath)
    v1 = u.get_t(bool, 'test', 'bool', 'true')
    assert type(v1) == bool
    assert v1
    v2 = u.get_t(bool, 'test', 'bool', 'false')
    assert type(v2) == bool
    assert not v2


def test_get_t_int(tmpdir):
    'Test get_t method for int type'
    tmpdir.join('test').write("""
config integer 'integer'
    option plus '42'
    option minus '-42'
""")
    u = euci.EUci(confdir=tmpdir.strpath)
    v1 = u.get_t(int, 'test', 'integer', 'plus')
    assert v1 == 42
    assert type(v1) == int
    v2 = u.get_t(int, 'test', 'integer', 'minus')
    assert v2 == -42
    assert type(v2) == int


def test_set_t(tmpdir):
    'Test set_integer'
    tmpdir.join('test').write("""
config testing 'testing'
""")
    u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
    u.set_t('test', 'testing', 'foo', 'value')
    assert u.get('test', 'testing', 'foo') == 'value'


def test_set_t_boolean(tmpdir):
    'Test set_t method for bool type'
    tmpdir.join('test').write("""
config testing 'testing'
""")
    u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
    u.set_t('test', 'testing', 'true', True)
    u.set_t('test', 'testing', 'false', False)
    assert u.get('test', 'testing', 'true') == '1'
    assert u.get('test', 'testing', 'false') == '0'


def test_set_t_integer(tmpdir):
    'Test set_integer'
    tmpdir.join('test').write("""
config testing 'testing'
""")
    u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
    u.set_t('test', 'testing', 'plus', 42)
    u.set_t('test', 'testing', 'minus', -42)
    assert u.get('test', 'testing', 'plus') == '42'
    assert u.get('test', 'testing', 'minus') == '-42'