|
1
|
|
|
# -*- coding: utf-8 - |
|
2
|
|
|
|
|
3
|
|
|
"""Tests of the component module. |
|
4
|
|
|
|
|
5
|
|
|
SPDX-License-Identifier: MIT |
|
6
|
|
|
""" |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
from configparser import NoOptionError, NoSectionError |
|
10
|
|
|
import os |
|
11
|
|
|
|
|
12
|
|
|
import pytest |
|
13
|
|
|
|
|
14
|
|
|
from oemof.db import config |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
TEST_INI = os.path.join(os.path.dirname(__file__), "config_test.ini") |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def test_main_basic(): |
|
21
|
|
|
config.main() |
|
22
|
|
|
config.load_config(None) |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def test_correct_filename(): |
|
26
|
|
|
fn = "dahfausf.ini" |
|
27
|
|
|
f = open(fn, "w+") |
|
28
|
|
|
f.write("[type_tester]\n") |
|
29
|
|
|
f.write("asd = hello blubb") |
|
30
|
|
|
f.close() |
|
31
|
|
|
config.load_config(fn) |
|
32
|
|
|
assert config.get("type_tester", "asd") == "hello blubb" |
|
33
|
|
|
os.remove(fn) |
|
34
|
|
|
config.FILE = "" |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
def test_init_wrong_filename(caplog): |
|
38
|
|
|
config.load_config("wrong_filename") |
|
39
|
|
|
assert "For further advice, see in the docs" in caplog.text |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def test_init_wrong_filename_with_correct_file(caplog): |
|
43
|
|
|
config.FILE = TEST_INI |
|
44
|
|
|
config.load_config("wrong_filename") |
|
45
|
|
|
assert len(caplog.text) == 0 |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
def test_init_wrong_fidsflename_with_correct_file(caplog): |
|
49
|
|
|
config.FILE = "dsfa" |
|
50
|
|
|
config.load_config(None) |
|
51
|
|
|
# assert len(caplog.text) == 0 |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def test_get_function(): |
|
55
|
|
|
"""Read config file.""" |
|
56
|
|
|
config.FILE = TEST_INI |
|
57
|
|
|
config.load_config(TEST_INI) |
|
58
|
|
|
assert config.get("type_tester", "my_bool") is True |
|
59
|
|
|
assert isinstance(config.get("type_tester", "my_int"), int) |
|
60
|
|
|
assert config.get("type_tester", "my_int") == 5 |
|
61
|
|
|
assert isinstance(config.get("type_tester", "my_float"), float) |
|
62
|
|
|
assert config.get("type_tester", "my_float") == 4.5 |
|
63
|
|
|
assert isinstance(config.get("type_tester", "my_string"), str) |
|
64
|
|
|
config._loaded = False |
|
65
|
|
|
assert config.get("type_tester", "my_string") == "hallo" |
|
66
|
|
|
assert isinstance(config.get("type_tester", "my_None"), type(None)) |
|
67
|
|
|
assert config.get("type_tester", "my_none") is None |
|
68
|
|
|
assert isinstance(config.get("type_tester", "my_list"), str) |
|
69
|
|
|
assert config.get("type_tester", "my_list") == "4,6,7,9" |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
def test_missing_value(): |
|
73
|
|
|
config.FILE = TEST_INI |
|
74
|
|
|
with pytest.raises( |
|
75
|
|
|
NoOptionError, match="No option 'blubb' in section: 'type_tester'" |
|
76
|
|
|
): |
|
77
|
|
|
config.get("type_tester", "blubb") |
|
78
|
|
|
with pytest.raises(NoSectionError, match="No section: 'typetester'"): |
|
79
|
|
|
config.get("typetester", "my_bool") |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def test_set_temp_value(): |
|
83
|
|
|
|
|
84
|
|
|
config.FILE = TEST_INI |
|
85
|
|
|
with pytest.raises( |
|
86
|
|
|
NoOptionError, match="No option 'blubb' in section: 'type_tester'" |
|
87
|
|
|
): |
|
88
|
|
|
config.get("type_tester", "blubb") |
|
89
|
|
|
config.set("type_tester", "blubb", "None") |
|
90
|
|
|
assert config.get("type_tester", "blubb") is None |
|
91
|
|
|
config.set("type_tester", "blubb", "5.5") |
|
92
|
|
|
assert config.get("type_tester", "blubb") == 5.5 |
|
93
|
|
|
remove_line() |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
def test_set_temp_without_init(): |
|
97
|
|
|
config._loaded = False |
|
98
|
|
|
config.set("type_tester", "blubb", "None") |
|
99
|
|
|
assert config.get("type_tester", "blubb") is None |
|
100
|
|
|
config.set("blubb_tester", "blubb", "None") |
|
101
|
|
|
remove_line() |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
def remove_line(): |
|
105
|
|
|
with open(TEST_INI, "r") as f: |
|
106
|
|
|
lines = f.readlines() |
|
107
|
|
|
with open(TEST_INI, "w") as f: |
|
108
|
|
|
for line in lines: |
|
109
|
|
|
print(line) |
|
110
|
|
|
if "blubb" not in line: |
|
111
|
|
|
f.write(line) |
|
112
|
|
|
|