-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtest_config.py
100 lines (73 loc) · 3.28 KB
/
test_config.py
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
"""Unit tests for configuration handling in Annif"""
import logging
import pytest
import annif.config
from annif.exception import ConfigurationException
def test_check_config_exists_cfg():
cfg = annif.config.check_config("tests/projects.cfg")
assert cfg == "tests/projects.cfg"
def test_check_config_exists_toml():
cfg = annif.config.check_config("tests/projects.toml")
assert cfg == "tests/projects.toml"
def test_check_config_exists_directory():
cfg = annif.config.check_config("tests/projects.d")
assert cfg == "tests/projects.d"
def test_check_config_not_exists(caplog):
with caplog.at_level(logging.WARNING):
cfg = annif.config.check_config("tests/notfound.cfg")
assert cfg is None
assert (
'Project configuration file or directory "tests/notfound.cfg" ' + "is missing"
in caplog.text
)
def test_find_config_exists_default(monkeypatch):
# temporarily chdir to the tests directory
monkeypatch.chdir("tests")
cfg = annif.config.find_config()
assert cfg == "projects.cfg"
def test_find_config_not_exists_default(monkeypatch, caplog):
# temporarily chdir to the tests/corpora directory
monkeypatch.chdir("tests/corpora")
with caplog.at_level(logging.WARNING):
cfg = annif.config.find_config()
assert cfg is None
assert "Could not find project configuration " in caplog.text
def test_parse_config_cfg_nondefault():
cfg = annif.config.parse_config("tests/projects.cfg")
assert isinstance(cfg, annif.config.AnnifConfigCFG)
assert len(cfg.project_ids) == 18
assert cfg["dummy-fi"] is not None
def test_parse_config_cfg_default(monkeypatch):
# temporarily chdir to the tests directory
monkeypatch.chdir("tests")
cfg = annif.config.parse_config("")
assert isinstance(cfg, annif.config.AnnifConfigCFG)
assert len(cfg.project_ids) == 18
assert cfg["dummy-fi"] is not None
def test_parse_config_toml():
cfg = annif.config.parse_config("tests/projects.toml")
assert isinstance(cfg, annif.config.AnnifConfigTOML)
assert len(cfg.project_ids) == 2
assert cfg["dummy-fi-toml"] is not None
def test_parse_config_toml_failed(tmpdir):
conffile = tmpdir.join("projects.toml")
conffile.write("[section]\nkey=unquoted\n")
with pytest.raises(ConfigurationException) as excinfo:
annif.config.parse_config(str(conffile))
assert "Invalid value" in str(excinfo.value)
def test_parse_config_directory():
cfg = annif.config.parse_config("tests/projects.d")
assert isinstance(cfg, annif.config.AnnifConfigDirectory)
assert len(cfg.project_ids) == 2 + 18 # 0-projects.toml + 1-projects.cfg
assert cfg["dummy-fi"] is not None
assert cfg["dummy-fi-toml"] is not None
assert list(cfg.project_ids)[0] == "dummy-fi-toml" # First in 0-projects.toml
assert list(cfg.project_ids)[2] == "dummy-fi" # First in 1-projects.cfg
def test_parse_config_directory_duplicated_project(tmpdir):
conffile = tmpdir.join("projects-1.toml")
conffile.write("[duplicated]\nkey='value'\n")
conffile = tmpdir.join("projects-2.cfg")
conffile.write("[duplicated]\nkey=value\n")
with pytest.raises(ConfigurationException) as excinfo:
annif.config.parse_config(str(tmpdir))
assert 'project ID "duplicated" already exists' in str(excinfo.value)