File: test_econf_setters.py

package info (click to toggle)
libeconf 0.7.7%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,636 kB
  • sloc: ansic: 7,440; python: 1,204; cs: 651; sh: 214; xml: 182; makefile: 7
file content (116 lines) | stat: -rw-r--r-- 4,102 bytes parent folder | download | duplicates (2)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import pytest
import econf
from contextlib import contextmanager
from pathlib import Path
from ctypes import *


FILE = econf.read_file("test/testdata/examples/example.conf", "=", ";")
FILE2 = econf.read_file("test/testdata/examples2/example.conf", "=", "#")


@contextmanager
def does_not_raise():
    yield


@pytest.mark.parametrize(
    "file,context,group,key,value",
    [
        (FILE, does_not_raise(), "Group", "Bla", 1),
        (FILE, does_not_raise(), "Group", "Welcome", 1),
        (FILE, does_not_raise(), None, "foo2", 1),
        (FILE, pytest.raises(ValueError), "Group", "Bla", 99999999999999999999),
        (FILE, does_not_raise(), "New Group", "Bla", 1),
        (FILE, pytest.raises(TypeError), 7, 2, 1),
        (FILE, pytest.raises(TypeError), "Group", "Bla", "Invalid Value"),
        (FILE, pytest.raises(TypeError), "Invalid Group", "Bla", "Invalid Value"),
    ],
)
def test_set_int_value(file, context, group, key, value):
    with context:
        econf.set_int_value(file, group, key, value)
        result = econf.get_int_value(file, group, key)

        assert result == value


@pytest.mark.parametrize(
    "file,context,group,key,value",
    [
        (FILE, does_not_raise(), "Group", "Bla", 1),
        (FILE, does_not_raise(), "Group", "Welcome", 1),
        (FILE, does_not_raise(), "New Group", "Bla", 1),
        (FILE, does_not_raise(), None, "foo2", 1),
        (FILE, pytest.raises(TypeError), "Group", "Bla", -1),
        (FILE, pytest.raises(ValueError), "Group", "Bla", 99999999999999999999),
        (FILE, pytest.raises(TypeError), 7, 2, 1),
        (FILE, pytest.raises(TypeError), "Group", "Bla", "Invalid Value"),
        (FILE, pytest.raises(TypeError), "Invalid Group", "Bal", "Invalid Value"),
    ],
)
def test_set_uint_value(file, context, group, key, value):
    with context:
        econf.set_uint_value(file, group, key, value)
        result = econf.get_uint_value(file, group, key)

        assert result == value


@pytest.mark.parametrize(
    "file,context,group,key,value",
    [
        (FILE, does_not_raise(), None, "foo", 1.5),
        (FILE, does_not_raise(), "Group", "Welcome", 1.5),
        (FILE, does_not_raise(), "New Group", "Bla", 1.5),
        (FILE, does_not_raise(), "Group", "Bla", -1.5),
        (FILE, pytest.raises(TypeError), 7, 2, 1),
        (FILE, pytest.raises(TypeError), "Group", "Bla", "Invalid Value"),
        (FILE, pytest.raises(TypeError), "Group", "Bla", 1),
    ],
)
def test_set_float_value(file, context, group, key, value):
    with context:
        econf.set_float_value(file, group, key, value)
        result = econf.get_float_value(file, group, key)

        assert result == value


@pytest.mark.parametrize(
    "file,context,group,key,value",
    [
        (FILE, does_not_raise(), "Group", "Welcome", "Bye"),
        (FILE, does_not_raise(), "Group", "Bla", "1"),
        (FILE, does_not_raise(), "New Group", "Welcome", "Bye"),
        (FILE, does_not_raise(), "First Group", "Name", "\nNoname"),
        (FILE, pytest.raises(TypeError), 7, 2, 1),
        (FILE, pytest.raises(TypeError), "Group", "Bla", 1.5),
        (FILE, pytest.raises(TypeError), "Group", "Bla", True),
    ],
)
def test_set_string_value(file, context, group, key, value):
    with context:
        econf.set_string_value(file, group, key, value)
        result = econf.get_string_value(file, group, key)

        assert result == value


@pytest.mark.parametrize(
    "file,context,group,key,value",
    [
        (FILE, does_not_raise(), "Another Group", "Booleans", False),
        (FILE, does_not_raise(), "Group", "Bla", True),
        (FILE, does_not_raise(), "New Group", "Welcome", True),
        (FILE, pytest.raises(TypeError), 7, 2, 1),
        (FILE, pytest.raises(TypeError), "Group", "Bla", "Invalid Value"),
        (FILE, pytest.raises(TypeError), "Group", "Bla", ""),
    ],
)
def test_set_bool_value(file, context, group, key, value):
    with context:
        econf.set_bool_value(file, group, key, value)
        result = econf.get_bool_value(file, group, key)

        assert result == value