Skip to content

Commit cdb9e87

Browse files
authored
Merge pull request #39 from tisnik/refactored-config-loader
Refactored config loader
2 parents 9b40966 + b81635e commit cdb9e87

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

src/configuration.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ def load_configuration(self, filename: str) -> None:
2525
with open(filename, encoding="utf-8") as fin:
2626
config_dict = yaml.safe_load(fin)
2727
logger.info("Loaded configuration: %s", config_dict)
28-
self._configuration = Configuration(**config_dict)
28+
self.init_from_dict(config_dict)
29+
30+
def init_from_dict(self, config_dict: dict[Any, Any]):
31+
self._configuration = Configuration(**config_dict)
2932

3033
@property
3134
def configuration(self) -> Configuration:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Integration tests for configuration loading and handling."""
2+
3+
import pytest
4+
from src.configuration import configuration
5+
6+
7+
@pytest.fixture
8+
def configuration_filename():
9+
return "tests/configuration/lightspeed-stack.yaml"
10+
11+
12+
def test_default_configuration():
13+
cfg = configuration
14+
assert cfg is not None
15+
with pytest.raises(Exception, match="logic error: configuration is not loaded"):
16+
configuration.configuration
17+
18+
19+
def test_loading_proper_configuration(configuration_filename):
20+
cfg = configuration
21+
cfg.load_configuration(configuration_filename)
22+
assert cfg is not None
23+
assert cfg.configuration is not None
24+
assert cfg.llama_stack_configuration is not None
25+
name = cfg.configuration.name
26+
assert name == "foo bar baz"
27+
ls_config = cfg.llama_stack_configuration
28+
assert ls_config.url == "http://localhost:8321"

tests/unit/test_configuration.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,58 @@
11
"""Unit tests for functions defined in src/configuration.py."""
22

3-
from src.configuration import configuration
3+
import pytest
4+
from src.configuration import AppConfig
45

56

67
def test_default_configuration():
7-
cfg = configuration
8+
cfg = AppConfig()
89
assert cfg is not None
10+
11+
# configuration is not loaded
12+
with pytest.raises(Exception, match="logic error: configuration is not loaded"):
13+
# try to read property
14+
cfg.configuration
15+
16+
with pytest.raises(Exception, match="logic error: configuration is not loaded"):
17+
# try to read property
18+
cfg.llama_stack_configuration
19+
20+
21+
def test_configuration_is_singleton():
22+
cfg1 = AppConfig()
23+
cfg2 = AppConfig()
24+
assert cfg1 == cfg2
25+
26+
27+
def test_init_from_dict():
28+
config_dict = {
29+
"name": "foo",
30+
"llama_stack": {
31+
"api_key": "xyzzy",
32+
"url": "http://x.y.com:1234",
33+
"use_as_library_client": False,
34+
},
35+
}
36+
cfg = AppConfig()
37+
cfg.init_from_dict(config_dict)
38+
assert cfg.configuration is not None
39+
assert cfg.llama_stack_configuration is not None
40+
41+
42+
def test_load_proper_configuration(tmpdir):
43+
cfg_filename = tmpdir / "config.yaml"
44+
with open(cfg_filename, "w") as fout:
45+
fout.write(
46+
"""
47+
name: foo bar baz
48+
llama_stack:
49+
use_as_library_client: false
50+
url: http://localhost:8321
51+
api_key: xyzzy
52+
"""
53+
)
54+
55+
cfg = AppConfig()
56+
cfg.load_configuration(cfg_filename)
57+
assert cfg.configuration is not None
58+
assert cfg.llama_stack_configuration is not None

0 commit comments

Comments
 (0)