Skip to content

Commit 51dd27f

Browse files
committed
improve test
1 parent c66b1a7 commit 51dd27f

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

examples/server/tests/unit/test_embedding.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ def test_embedding_openai_library_base64():
202202
server.start()
203203
test_input = "Test base64 embedding output"
204204

205+
# get embedding in default format
206+
res = server.make_request("POST", "/v1/embeddings", data={
207+
"input": test_input
208+
})
209+
assert res.status_code == 200
210+
vec0 = res.body["data"][0]["embedding"]
211+
212+
# get embedding in base64 format
205213
res = server.make_request("POST", "/v1/embeddings", data={
206214
"input": test_input,
207215
"encoding_format": "base64"
@@ -216,12 +224,14 @@ def test_embedding_openai_library_base64():
216224
assert isinstance(embedding_data["embedding"], str)
217225

218226
# Verify embedding is valid base64
219-
try:
220-
decoded = base64.b64decode(embedding_data["embedding"])
221-
# Verify decoded data can be converted back to float array
222-
float_count = len(decoded) // 4 # 4 bytes per float
223-
floats = struct.unpack(f'{float_count}f', decoded)
224-
assert len(floats) > 0
225-
assert all(isinstance(x, float) for x in floats)
226-
except Exception as e:
227-
pytest.fail(f"Invalid base64 format: {str(e)}")
227+
decoded = base64.b64decode(embedding_data["embedding"])
228+
# Verify decoded data can be converted back to float array
229+
float_count = len(decoded) // 4 # 4 bytes per float
230+
floats = struct.unpack(f'{float_count}f', decoded)
231+
assert len(floats) > 0
232+
assert all(isinstance(x, float) for x in floats)
233+
assert len(floats) == len(vec0)
234+
235+
# make sure the decoded data is the same as the original
236+
for x, y in zip(floats, vec0):
237+
assert abs(x - y) < EPSILON

0 commit comments

Comments
 (0)