-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathtest_settings.py
158 lines (119 loc) · 4.35 KB
/
test_settings.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from io import StringIO
import json
from os.path import expanduser, expandvars, join
from os import getenv
from pathlib import Path
from subprocess import check_output
import sys
from unittest.mock import patch, MagicMock
import pytest
from utils.settings import (
Loader,
Settings,
get_git_commit,
get_package_version,
get_version,
)
@pytest.fixture
def expected_config_location():
xdg_config_home = getenv("XDG_CONFIG_HOME")
if xdg_config_home:
return join(xdg_config_home, "gpt-pilot", "config.json")
elif sys.platform in ["darwin", "linux"]:
return expanduser("~/.gpt-pilot/config.json")
elif sys.platform == "win32":
return expandvars("%APPDATA%\\GPT Pilot\\config.json")
else:
raise RuntimeError(f"Unknown platform: {sys.platform}")
def test_settings_initializes_known_variables():
settings = Settings()
assert settings.openai_api_key is None
assert settings.telemetry is None
def test_settings_init_ignores_unknown_variables():
settings = Settings(unknown="value")
assert not hasattr(settings, "unknown")
def test_settings_forbids_saving_unknown_variables():
settings = Settings()
with pytest.raises(AttributeError):
settings.unknown = "value"
def test_settings_update():
settings = Settings()
settings.update(openai_api_key="test_key")
assert settings.openai_api_key == "test_key"
def test_settings_to_dict():
settings = Settings()
settings.update(openai_api_key="test_key")
assert dict(settings) == {
"openai_api_key": "test_key",
"telemetry": None,
}
def test_loader_config_file_location(expected_config_location):
settings = Settings()
Loader(settings).config_path == expected_config_location
@patch("utils.settings.open")
@patch("utils.settings.Loader.update_settings_from_env")
def test_loader_load_config_file(_mock_from_env, mock_open, expected_config_location):
settings = Settings()
fake_config = json.dumps(
{
"openai_api_key": "test_key",
"telemetry": {
"id": "fake-id",
"endpoint": "https://example.com",
},
}
)
mock_open.return_value.__enter__.return_value = StringIO(fake_config)
loader = Loader(settings)
assert loader.config_path == Path(expected_config_location)
loader.config_path = MagicMock()
loader.load()
loader.config_path.exists.assert_called_once_with()
mock_open.assert_called_once_with(loader.config_path, "r", encoding="utf-8")
assert settings.openai_api_key == "test_key"
assert settings.telemetry["id"] == "fake-id"
assert settings.telemetry["endpoint"] == "https://example.com"
@patch("utils.settings.open")
@patch("utils.settings.Loader.update_settings_from_env")
def test_loader_load_no_config_file(_mock_from_env, mock_open, expected_config_location):
settings = Settings()
loader = Loader(settings)
assert loader.config_path == Path(expected_config_location)
loader.config_path = MagicMock()
loader.config_path.exists.return_value = False
loader.load()
loader.config_path.exists.assert_called_once_with()
mock_open.assert_not_called()
assert settings.openai_api_key is None
assert settings.telemetry is None
@patch("utils.settings.getenv")
def test_loader_load_from_env(mock_getenv):
settings = Settings()
mock_getenv.side_effect = lambda key: {
"TELEMETRY_ID": "fake-id",
"TELEMETRY_ENDPOINT": "https://example.com",
"OPENAI_API_KEY": "test_key",
}.get(key)
Loader(settings).update_settings_from_env(settings)
assert settings.openai_api_key == "test_key"
assert settings.telemetry["id"] == "fake-id"
assert settings.telemetry["endpoint"] == "https://example.com"
def test_get_git_commit():
try:
expected_commit_hash = check_output(
["git", "rev-parse", "HEAD"], encoding="ascii"
).strip()
except Exception:
expected_commit_hash = None
assert get_git_commit() == expected_commit_hash
def test_get_package_version():
assert get_package_version().startswith("0.")
def test_get_version():
try:
commit_suffix = (
"-git"
+ check_output(["git", "rev-parse", "HEAD"], encoding="ascii").strip()[:7]
)
except Exception:
commit_suffix = ""
assert get_version().endswith(commit_suffix)