-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathtest_config.py
More file actions
97 lines (71 loc) · 2.97 KB
/
test_config.py
File metadata and controls
97 lines (71 loc) · 2.97 KB
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
import shutil
from configparser import ConfigParser
import pytest
import deprecation
from aw_core import dirs
from aw_core.config import load_config, save_config, load_config_toml, save_config_toml
appname = "aw-core-test"
section = "section"
config_dir = dirs.get_config_dir(appname)
default_config_str = f"""# A default config file, with comments!
[{section}]
somestring = "Hello World!" # A comment
somevalue = 12.3 # Another comment
somearray = ["asd", 123]"""
@pytest.fixture(autouse=True)
def clean_config():
# Remove test config file if it already exists
shutil.rmtree(config_dir, ignore_errors=True)
# Rerun get_config dir to create config directory
dirs.get_config_dir(appname)
yield
# Remove test config file if it already exists
shutil.rmtree(config_dir)
def test_create():
appname = "aw-core-test"
section = "section"
config_dir = dirs.get_config_dir(appname)
def test_config_defaults():
# Load non-existing config (will create a out-commented default config file)
config = load_config_toml(appname, default_config_str)
# Check that load_config used defaults
assert config[section]["somestring"] == "Hello World!"
assert config[section]["somevalue"] == 12.3
assert config[section]["somearray"] == ["asd", 123]
def test_config_no_defaults():
# Write defaults to file
save_config_toml(appname, default_config_str)
# Load written defaults without defaults
config = load_config_toml(appname, "")
assert config[section]["somestring"] == "Hello World!"
assert config[section]["somevalue"] == 12.3
assert config[section]["somearray"] == ["asd", 123]
def test_config_override():
# Create a minimal config file with one overridden value
config = """[section]
somevalue = 1000.1"""
save_config_toml(appname, config)
# Open non-default config file and verify that values are correct
config = load_config_toml(appname, default_config_str)
assert config[section]["somevalue"] == 1000.1
@deprecation.fail_if_not_removed
def test_config_ini():
# Create default config
default_config = ConfigParser()
default_config[section] = {"somestring": "Hello World!", "somevalue": 12.3}
# Load non-existing config (will create a default config file)
config = load_config(appname, default_config)
# Check that current config file is same as default config file
assert config[section]["somestring"] == default_config[section]["somestring"]
assert config[section].getfloat("somevalue") == default_config[section].getfloat(
"somevalue"
)
# Modify and save config file
config[section]["somevalue"] = "1000.1"
save_config(appname, config)
# Open non-default config file and verify that values are correct
new_config = load_config(appname, default_config)
assert new_config[section]["somestring"] == config[section]["somestring"]
assert new_config[section].getfloat("somevalue") == config[section].getfloat(
"somevalue"
)