Skip to content

Commit 86e21de

Browse files
committed
Added unit tests for the new code
1 parent 5fbd57c commit 86e21de

1 file changed

Lines changed: 379 additions & 0 deletions

File tree

Lines changed: 379 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,379 @@
1+
"""Unit tests for functions defined in src/llama_stack_configuration.py."""
2+
3+
from pathlib import Path
4+
5+
import pytest
6+
import yaml
7+
8+
from models.config import (
9+
ByokRag,
10+
Configuration,
11+
ServiceConfiguration,
12+
LlamaStackConfiguration,
13+
UserDataCollection,
14+
)
15+
16+
from constants import (
17+
DEFAULT_EMBEDDING_MODEL,
18+
DEFAULT_EMBEDDING_DIMENSION,
19+
)
20+
21+
from llama_stack_configuration import (
22+
generate_configuration,
23+
construct_vector_dbs_section,
24+
construct_vector_io_providers_section,
25+
)
26+
27+
28+
def test_construct_vector_dbs_section_init() -> None:
29+
"""Test the function construct_vector_dbs_section for no vector_dbs configured before."""
30+
ls_config = {}
31+
byok_rag = []
32+
output = construct_vector_dbs_section(ls_config, byok_rag)
33+
assert len(output) == 0
34+
35+
36+
def test_construct_vector_dbs_section_init_with_existing_data() -> None:
37+
"""Test the function construct_vector_dbs_section for vector_dbs configured before."""
38+
ls_config = {
39+
"vector_dbs": [
40+
{
41+
"vector_db_id": "vector_db_id_1",
42+
"provider_id": "provier_id_1",
43+
"embedding_model": "embedding_model_1",
44+
"embedding_dimension": 1,
45+
},
46+
{
47+
"vector_db_id": "vector_db_id_2",
48+
"provider_id": "provier_id_2",
49+
"embedding_model": "embedding_model_2",
50+
"embedding_dimension": 2,
51+
},
52+
]
53+
}
54+
byok_rag = []
55+
output = construct_vector_dbs_section(ls_config, byok_rag)
56+
assert len(output) == 2
57+
assert output[0] == {
58+
"vector_db_id": "vector_db_id_1",
59+
"provider_id": "provier_id_1",
60+
"embedding_model": "embedding_model_1",
61+
"embedding_dimension": 1,
62+
}
63+
assert output[1] == {
64+
"vector_db_id": "vector_db_id_2",
65+
"provider_id": "provier_id_2",
66+
"embedding_model": "embedding_model_2",
67+
"embedding_dimension": 2,
68+
}
69+
70+
71+
def test_construct_vector_dbs_section_append() -> None:
72+
"""Test the function construct_vector_dbs_section for no vector_dbs configured before."""
73+
ls_config = {}
74+
byok_rag = [
75+
ByokRag(
76+
rag_id="rag_id_1",
77+
vector_db_id="vector_db_id_1",
78+
db_path="tests/configuration/rag.txt",
79+
),
80+
ByokRag(
81+
rag_id="rag_id_2",
82+
vector_db_id="vector_db_id_2",
83+
db_path="tests/configuration/rag.txt",
84+
),
85+
]
86+
output = construct_vector_dbs_section(ls_config, byok_rag)
87+
assert len(output) == 2
88+
assert output[0] == {
89+
"vector_db_id": "vector_db_id_1",
90+
"provider_id": "byok_vector_db_id_1",
91+
"embedding_model": DEFAULT_EMBEDDING_MODEL,
92+
"embedding_dimension": DEFAULT_EMBEDDING_DIMENSION,
93+
}
94+
assert output[1] == {
95+
"vector_db_id": "vector_db_id_2",
96+
"provider_id": "byok_vector_db_id_2",
97+
"embedding_model": DEFAULT_EMBEDDING_MODEL,
98+
"embedding_dimension": DEFAULT_EMBEDDING_DIMENSION,
99+
}
100+
101+
102+
def test_construct_vector_dbs_section_full_merge() -> None:
103+
"""Test the function construct_vector_dbs_section for vector_dbs configured before."""
104+
ls_config = {
105+
"vector_dbs": [
106+
{
107+
"vector_db_id": "vector_db_id_1",
108+
"provider_id": "provier_id_1",
109+
"embedding_model": "embedding_model_1",
110+
"embedding_dimension": 1,
111+
},
112+
{
113+
"vector_db_id": "vector_db_id_2",
114+
"provider_id": "provier_id_2",
115+
"embedding_model": "embedding_model_2",
116+
"embedding_dimension": 2,
117+
},
118+
]
119+
}
120+
byok_rag = [
121+
ByokRag(
122+
rag_id="rag_id_1",
123+
vector_db_id="vector_db_id_1",
124+
db_path="tests/configuration/rag.txt",
125+
),
126+
ByokRag(
127+
rag_id="rag_id_2",
128+
vector_db_id="vector_db_id_2",
129+
db_path="tests/configuration/rag.txt",
130+
),
131+
]
132+
output = construct_vector_dbs_section(ls_config, byok_rag)
133+
assert len(output) == 4
134+
assert output[0] == {
135+
"vector_db_id": "vector_db_id_1",
136+
"provider_id": "provier_id_1",
137+
"embedding_model": "embedding_model_1",
138+
"embedding_dimension": 1,
139+
}
140+
assert output[1] == {
141+
"vector_db_id": "vector_db_id_2",
142+
"provider_id": "provier_id_2",
143+
"embedding_model": "embedding_model_2",
144+
"embedding_dimension": 2,
145+
}
146+
assert output[2] == {
147+
"vector_db_id": "vector_db_id_1",
148+
"provider_id": "byok_vector_db_id_1",
149+
"embedding_model": DEFAULT_EMBEDDING_MODEL,
150+
"embedding_dimension": DEFAULT_EMBEDDING_DIMENSION,
151+
}
152+
assert output[3] == {
153+
"vector_db_id": "vector_db_id_2",
154+
"provider_id": "byok_vector_db_id_2",
155+
"embedding_model": DEFAULT_EMBEDDING_MODEL,
156+
"embedding_dimension": DEFAULT_EMBEDDING_DIMENSION,
157+
}
158+
159+
160+
def test_construct_vector_io_providers_section_init() -> None:
161+
"""Test construct_vector_io_providers_section for no vector_io_providers configured before."""
162+
ls_config = {"providers": {}}
163+
byok_rag = []
164+
output = construct_vector_io_providers_section(ls_config, byok_rag)
165+
assert len(output) == 0
166+
167+
168+
def test_construct_vector_io_providers_section_init_with_existing_data() -> None:
169+
"""Test construct_vector_io_providers_section for vector_io_providers configured before."""
170+
ls_config = {
171+
"providers": {
172+
"vector_io": [
173+
{
174+
"provider_id": "faiss_1",
175+
"provider_type": "inline::faiss",
176+
},
177+
{
178+
"provider_id": "faiss_2",
179+
"provider_type": "inline::faiss",
180+
},
181+
]
182+
}
183+
}
184+
byok_rag = []
185+
output = construct_vector_io_providers_section(ls_config, byok_rag)
186+
assert len(output) == 2
187+
assert output[0] == {
188+
"provider_id": "faiss_1",
189+
"provider_type": "inline::faiss",
190+
}
191+
assert output[1] == {
192+
"provider_id": "faiss_2",
193+
"provider_type": "inline::faiss",
194+
}
195+
196+
197+
def test_construct_vector_io_providers_section_append() -> None:
198+
"""Test construct_vector_io_providers_section for no vector_io_providers configured before."""
199+
ls_config = {"providers": {}}
200+
byok_rag = [
201+
ByokRag(
202+
rag_id="rag_id_1",
203+
vector_db_id="vector_db_id_1",
204+
db_path="tests/configuration/rag.txt",
205+
),
206+
ByokRag(
207+
rag_id="rag_id_2",
208+
vector_db_id="vector_db_id_2",
209+
db_path="tests/configuration/rag.txt",
210+
),
211+
]
212+
output = construct_vector_io_providers_section(ls_config, byok_rag)
213+
assert len(output) == 2
214+
assert output[0] == {
215+
"provider_id": "byok_vector_db_id_1",
216+
"provider_type": "inline::faiss",
217+
"config": {
218+
"kvstore": {
219+
"db_path": ".llama/vector_db_id_1.db",
220+
"namespace": None,
221+
"type": "sqlite",
222+
},
223+
},
224+
}
225+
assert output[1] == {
226+
"provider_id": "byok_vector_db_id_2",
227+
"provider_type": "inline::faiss",
228+
"config": {
229+
"kvstore": {
230+
"db_path": ".llama/vector_db_id_2.db",
231+
"namespace": None,
232+
"type": "sqlite",
233+
},
234+
},
235+
}
236+
237+
238+
def test_construct_vector_io_providers_section_full_merge() -> None:
239+
"""Test construct_vector_io_providers_section for vector_io_providers configured before."""
240+
ls_config = {
241+
"providers": {
242+
"vector_io": [
243+
{
244+
"provider_id": "faiss_1",
245+
"provider_type": "inline::faiss",
246+
},
247+
{
248+
"provider_id": "faiss_2",
249+
"provider_type": "inline::faiss",
250+
},
251+
]
252+
}
253+
}
254+
byok_rag = [
255+
ByokRag(
256+
rag_id="rag_id_1",
257+
vector_db_id="vector_db_id_1",
258+
db_path="tests/configuration/rag.txt",
259+
),
260+
ByokRag(
261+
rag_id="rag_id_2",
262+
vector_db_id="vector_db_id_2",
263+
db_path="tests/configuration/rag.txt",
264+
),
265+
]
266+
output = construct_vector_io_providers_section(ls_config, byok_rag)
267+
assert len(output) == 4
268+
assert output[0] == {
269+
"provider_id": "faiss_1",
270+
"provider_type": "inline::faiss",
271+
}
272+
assert output[1] == {
273+
"provider_id": "faiss_2",
274+
"provider_type": "inline::faiss",
275+
}
276+
assert output[2] == {
277+
"provider_id": "byok_vector_db_id_1",
278+
"provider_type": "inline::faiss",
279+
"config": {
280+
"kvstore": {
281+
"db_path": ".llama/vector_db_id_1.db",
282+
"namespace": None,
283+
"type": "sqlite",
284+
},
285+
},
286+
}
287+
assert output[3] == {
288+
"provider_id": "byok_vector_db_id_2",
289+
"provider_type": "inline::faiss",
290+
"config": {
291+
"kvstore": {
292+
"db_path": ".llama/vector_db_id_2.db",
293+
"namespace": None,
294+
"type": "sqlite",
295+
},
296+
},
297+
}
298+
299+
300+
def test_generate_configuration_no_input_file(tmpdir: Path) -> None:
301+
"""Test the function to generate configuration when input file does not exist."""
302+
cfg = Configuration(
303+
name="test_name",
304+
service=ServiceConfiguration(),
305+
llama_stack=LlamaStackConfiguration(
306+
use_as_library_client=True,
307+
library_client_config_path="tests/configuration/run.yaml",
308+
api_key="whatever",
309+
),
310+
user_data_collection=UserDataCollection(
311+
feedback_enabled=False, feedback_storage=None
312+
),
313+
)
314+
outfile = tmpdir / "run.xml"
315+
# try to generate new configuration file
316+
with pytest.raises(FileNotFoundError, match="No such file"):
317+
generate_configuration("/does/not/exist", outfile, cfg)
318+
319+
320+
def test_generate_configuration_proper_input_file_no_byok(tmpdir: Path) -> None:
321+
"""Test the function to generate configuration when input file exists."""
322+
cfg = Configuration(
323+
name="test_name",
324+
service=ServiceConfiguration(),
325+
llama_stack=LlamaStackConfiguration(
326+
use_as_library_client=True,
327+
library_client_config_path="tests/configuration/run.yaml",
328+
api_key="whatever",
329+
),
330+
user_data_collection=UserDataCollection(
331+
feedback_enabled=False, feedback_storage=None
332+
),
333+
)
334+
outfile = tmpdir / "run.xml"
335+
# try to generate new configuration file
336+
generate_configuration("tests/configuration/run.yaml", outfile, cfg)
337+
338+
with open(outfile, "r", encoding="utf-8") as fin:
339+
generated = yaml.safe_load(fin)
340+
assert "vector_dbs" in generated
341+
assert "providers" in generated
342+
assert "vector_io" in generated["providers"]
343+
344+
345+
def test_generate_configuration_proper_input_file_configured_byok(tmpdir: Path) -> None:
346+
"""Test the function to generate configuration when BYOK RAG should be added."""
347+
cfg = Configuration(
348+
name="test_name",
349+
service=ServiceConfiguration(),
350+
llama_stack=LlamaStackConfiguration(
351+
use_as_library_client=True,
352+
library_client_config_path="tests/configuration/run.yaml",
353+
api_key="whatever",
354+
),
355+
user_data_collection=UserDataCollection(
356+
feedback_enabled=False, feedback_storage=None
357+
),
358+
byok_rag=[
359+
ByokRag(
360+
rag_id="rag_id_1",
361+
vector_db_id="vector_db_id_1",
362+
db_path="tests/configuration/rag.txt",
363+
),
364+
ByokRag(
365+
rag_id="rag_id_2",
366+
vector_db_id="vector_db_id_2",
367+
db_path="tests/configuration/rag.txt",
368+
),
369+
],
370+
)
371+
outfile = tmpdir / "run.xml"
372+
# try to generate new configuration file
373+
generate_configuration("tests/configuration/run.yaml", outfile, cfg)
374+
375+
with open(outfile, "r", encoding="utf-8") as fin:
376+
generated = yaml.safe_load(fin)
377+
assert "vector_dbs" in generated
378+
assert "providers" in generated
379+
assert "vector_io" in generated["providers"]

0 commit comments

Comments
 (0)