-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathtest_service_config.py
More file actions
84 lines (68 loc) · 3.18 KB
/
test_service_config.py
File metadata and controls
84 lines (68 loc) · 3.18 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""Tests for libs.application.service_config (LLM service configuration)."""
from __future__ import annotations
from libs.application.service_config import ServiceConfig
# ── TestServiceConfig ───────────────────────────────────────────────────
class TestServiceConfig:
"""Construction, validation, and serialisation of ServiceConfig."""
def _make_env(self, **overrides):
base = {
"AZURE_OPENAI_API_VERSION": "2024-02-01",
"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME": "gpt-4",
"AZURE_OPENAI_ENDPOINT": "https://myoai.openai.azure.com",
"AZURE_OPENAI_API_KEY": "secret-key",
}
base.update(overrides)
return base
def test_construction_from_env_vars(self):
env = self._make_env()
cfg = ServiceConfig("default", "AZURE_OPENAI", env)
assert cfg.service_id == "default"
assert cfg.api_version == "2024-02-01"
assert cfg.chat_deployment_name == "gpt-4"
assert cfg.endpoint == "https://myoai.openai.azure.com"
def test_is_valid_with_entra_id(self):
env = self._make_env()
cfg = ServiceConfig("svc", "AZURE_OPENAI", env, use_entra_id=True)
assert cfg.is_valid() is True
def test_is_valid_without_entra_id_requires_api_key(self):
env = self._make_env()
cfg = ServiceConfig("svc", "AZURE_OPENAI", env, use_entra_id=False)
assert cfg.is_valid() is True
def test_is_invalid_missing_endpoint(self):
env = self._make_env()
del env["AZURE_OPENAI_ENDPOINT"]
cfg = ServiceConfig("svc", "AZURE_OPENAI", env, use_entra_id=True)
assert cfg.is_valid() is False
def test_is_invalid_missing_deployment(self):
env = self._make_env()
del env["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"]
cfg = ServiceConfig("svc", "AZURE_OPENAI", env, use_entra_id=True)
assert cfg.is_valid() is False
def test_is_invalid_no_entra_no_key(self):
env = self._make_env()
del env["AZURE_OPENAI_API_KEY"]
cfg = ServiceConfig("svc", "AZURE_OPENAI", env, use_entra_id=False)
assert cfg.is_valid() is False
def test_to_dict_keys(self):
env = self._make_env()
cfg = ServiceConfig("svc", "AZURE_OPENAI", env)
d = cfg.to_dict()
assert d["endpoint"] == "https://myoai.openai.azure.com"
assert d["chat_deployment_name"] == "gpt-4"
assert d["api_key"] == "secret-key"
def test_to_dict_empty_fields_become_none(self):
cfg = ServiceConfig("svc", "MISSING_PREFIX", {})
d = cfg.to_dict()
assert d["endpoint"] is None
assert d["chat_deployment_name"] is None
def test_custom_prefix(self):
env = {
"MY_LLM_ENDPOINT": "https://custom.api",
"MY_LLM_CHAT_DEPLOYMENT_NAME": "model-v2",
}
cfg = ServiceConfig("custom", "MY_LLM", env, use_entra_id=True)
assert cfg.endpoint == "https://custom.api"
assert cfg.chat_deployment_name == "model-v2"
assert cfg.is_valid() is True