summaryrefslogtreecommitdiff
path: root/test/test_config.py
blob: 0a1c80ce8183905a4fdcf4e229f3485f58b14024 (plain)
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
"""
Test the config module
"""

import os
import tempfile
from pathlib import Path

import pytest

from poezio import config


@pytest.fixture(scope="module")
def config_obj():
    file_ = tempfile.NamedTemporaryFile(delete=False)
    conf = config.Config(file_name=Path(file_.name))
    yield conf
    del conf
    os.unlink(file_.name)


class TestConfigSimple(object):
    def test_get_set(self, config_obj):
        config_obj.set_and_save('test', value='coucou')
        config_obj.set_and_save('test2', value='true')
        assert config_obj.get('test') == 'coucou'
        assert config_obj.get('test2') == 'true'
        assert config_obj.get('toto') == ''

    def test_file_content(self, config_obj):
        with open(str(config_obj.file_name), 'r') as fd:
            data = fd.read()
        supposed_content = '[Poezio]\ntest = coucou\ntest2 = true\n'
        assert data == supposed_content

    def test_get_types(self, config_obj):

        config_obj.set_and_save('test_int', '99')
        config_obj.set_and_save('test_int_neg', '-1')
        config_obj.set_and_save('test_bool_t', 'true')
        config_obj.set_and_save('test_bool_f', 'false')
        config_obj.set_and_save('test_float', '1.5')

        assert config_obj.get('test_int', default=0) == 99
        assert config_obj.get('test_int_neg', default=0) == -1
        assert config_obj.get('test_bool_t', default=False) == True
        assert config_obj.get('test_bool_f', default=True) == False
        assert config_obj.get('test_float', default=1.0) == 1.5

    def test_remove(self, config_obj):
        with open(str(config_obj.file_name), 'r') as fd:
            data = fd.read()

        supposed_content = ('[Poezio]\ntest = coucou\ntest2 = true\n'
                            'test_int = 99\ntest_int_neg = -1\ntest_bool_t ='
                            ' true\ntest_bool_f = false\ntest_float = 1.5\n')

        assert data == supposed_content

        config_obj.remove_and_save('test_int')
        config_obj.remove_and_save('test_int_neg')
        config_obj.remove_and_save('test_bool_t')
        config_obj.remove_and_save('test_bool_f')
        config_obj.remove_and_save('test_float')

        with open(str(config_obj.file_name), 'r') as fd:
            data = fd.read()

        supposed_content = '[Poezio]\ntest = coucou\ntest2 = true\n'

        assert data == supposed_content


    def test_toggle(self, config_obj):
        config_obj.set_and_save('test2', value='toggle')
        assert config_obj.get('test2') == 'false'
        config_obj.set_and_save('test2', value='toggle')
        assert config_obj.get('test2') == 'true'

    def test_get_set_default(self, config_obj):
        assert config_obj.get('doesnotexist', 'toto@tata') == 'toto@tata'
        assert config_obj.get('doesnotexist2', '1234') == '1234'

class TestConfigSections(object):
    def test_set_section(self, config_obj):
        config_obj.set_and_save('option1', 'test', section='NotPoezio')
        config_obj.set_and_save('option2', 'test2', section='NotPoezio')

        assert config_obj.get('option1', section='NotPoezio') == 'test'
        assert config_obj.get('option2', section='NotPoezio') == 'test2'

    def test_file_content(self, config_obj):
        with open(str(config_obj.file_name), 'r') as fd:
            data = fd.read()
        supposed_content = ('[Poezio]\ntest = coucou\ntest2 = true\n'
                            '[NotPoezio]\noption1 = test\noption2 = test2\n')
        assert data == supposed_content

class TestTabNames(object):
    def test_get_tabname(self, config_obj):
        config.post_logging_setup()
        config_obj.set_and_save('test', value='value.toto@toto.com',
                                section='toto@toto.com')
        config_obj.set_and_save('test2', value='value2@toto.com',
                                section='@toto.com')

        assert config_obj.get_by_tabname('test', 'toto@toto.com') == 'value.toto@toto.com'
        assert config_obj.get_by_tabname('test2', 'toto@toto.com') == 'value2@toto.com'
        assert config_obj.get_by_tabname('test2', 'toto@toto.com', fallback=False) == 'value2@toto.com'
        assert config_obj.get_by_tabname('test2', 'toto@toto.com', fallback_server=False) == 'true'
        assert config_obj.get_by_tabname('test_int', 'toto@toto.com', fallback=False) == ''