|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import pytest
|
| 4 | +import tomli_w |
4 | 5 |
|
5 |
| -from shelloracle.config import get_config, initialize_config |
| 6 | +from shelloracle.config import Configuration |
6 | 7 |
|
7 | 8 |
|
8 | 9 | class TestConfiguration:
|
9 | 10 | @pytest.fixture
|
10 |
| - def default_config(self, set_config): |
11 |
| - config = { |
12 |
| - "shelloracle": {"provider": "Ollama", "spinner_style": "earth"}, |
13 |
| - "provider": {"Ollama": {"host": "localhost", "port": 11434, "model": "dolphin-mistral"}}, |
14 |
| - } |
15 |
| - set_config(config) |
16 |
| - return config |
17 |
| - |
18 |
| - def test_initialize_config(self, default_config): |
19 |
| - with pytest.raises(RuntimeError): |
20 |
| - initialize_config() |
21 |
| - |
22 |
| - def test_from_file(self, default_config): |
23 |
| - assert get_config() == default_config |
| 11 | + def default_config(self): |
| 12 | + return Configuration( |
| 13 | + { |
| 14 | + "shelloracle": {"provider": "Ollama", "spinner_style": "earth"}, |
| 15 | + "provider": {"Ollama": {"host": "localhost", "port": 11434, "model": "dolphin-mistral"}}, |
| 16 | + } |
| 17 | + ) |
| 18 | + |
| 19 | + def test_from_file(self, default_config, tmp_path): |
| 20 | + config_path = tmp_path / "config.toml" |
| 21 | + with config_path.open("wb") as f: |
| 22 | + tomli_w.dump(default_config.raw_config, f) |
| 23 | + assert Configuration.from_file(config_path) == default_config |
24 | 24 |
|
25 | 25 | def test_getitem(self, default_config):
|
26 | 26 | for key in default_config:
|
27 |
| - assert default_config[key] == get_config()[key] |
| 27 | + assert default_config[key] == default_config.raw_config[key] |
28 | 28 |
|
29 | 29 | def test_len(self, default_config):
|
30 |
| - assert len(default_config) == len(get_config()) |
| 30 | + assert len(default_config) == len(default_config.raw_config) |
31 | 31 |
|
32 | 32 | def test_iter(self, default_config):
|
33 |
| - assert list(iter(default_config)) == list(iter(get_config())) |
34 |
| - |
35 |
| - def test_str(self, default_config): |
36 |
| - assert str(get_config()) == f"Configuration({default_config})" |
37 |
| - |
38 |
| - def test_repr(self, default_config): |
39 |
| - assert repr(default_config) == str(default_config) |
| 33 | + assert list(iter(default_config)) == list(iter(default_config.raw_config)) |
40 | 34 |
|
41 | 35 | def test_provider(self, default_config):
|
42 |
| - assert get_config().provider == "Ollama" |
| 36 | + assert default_config.provider == "Ollama" |
43 | 37 |
|
44 | 38 | def test_spinner_style(self, default_config):
|
45 |
| - assert get_config().spinner_style == "earth" |
46 |
| - |
47 |
| - def test_no_spinner_style(self, caplog, set_config): |
48 |
| - config_dict = { |
49 |
| - "shelloracle": {"provider": "Ollama"}, |
50 |
| - "provider": {"Ollama": {"host": "localhost", "port": 11434, "model": "dolphin-mistral"}}, |
51 |
| - } |
52 |
| - set_config(config_dict) |
53 |
| - assert get_config().spinner_style is None |
| 39 | + assert default_config.spinner_style == "earth" |
| 40 | + |
| 41 | + def test_no_spinner_style(self, caplog): |
| 42 | + config = Configuration( |
| 43 | + { |
| 44 | + "shelloracle": {"provider": "Ollama"}, |
| 45 | + "provider": {"Ollama": {"host": "localhost", "port": 11434, "model": "dolphin-mistral"}}, |
| 46 | + } |
| 47 | + ) |
| 48 | + assert config.spinner_style is None |
54 | 49 | assert "invalid spinner style" not in caplog.text
|
55 | 50 |
|
56 |
| - def test_invalid_spinner_style(self, caplog, set_config): |
57 |
| - config_dict = { |
58 |
| - "shelloracle": {"provider": "Ollama", "spinner_style": "invalid"}, |
59 |
| - "provider": {"Ollama": {"host": "localhost", "port": 11434, "model": "dolphin-mistral"}}, |
60 |
| - } |
61 |
| - set_config(config_dict) |
62 |
| - assert get_config().spinner_style is None |
| 51 | + def test_invalid_spinner_style(self, caplog): |
| 52 | + config = Configuration( |
| 53 | + { |
| 54 | + "shelloracle": {"provider": "Ollama", "spinner_style": "invalid"}, |
| 55 | + "provider": {"Ollama": {"host": "localhost", "port": 11434, "model": "dolphin-mistral"}}, |
| 56 | + } |
| 57 | + ) |
| 58 | + assert config.spinner_style is None |
63 | 59 | assert "invalid spinner style" in caplog.text
|
0 commit comments