-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_vectors.py
More file actions
58 lines (44 loc) · 1.88 KB
/
test_vectors.py
File metadata and controls
58 lines (44 loc) · 1.88 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
# Copyright (c) 2023-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
import math
import ast
from marklogic.vectors import base64_encode, base64_decode
from marklogic import Client
VECTOR = [3.14, 1.59, 2.65]
EXPECTED_BASE64 = "AAAAAAMAAADD9UhAH4XLP5qZKUA="
ACCEPTABLE_DELTA = 0.0001
def test_encode_and_decode_with_python():
encoded = base64_encode(VECTOR)
assert encoded == EXPECTED_BASE64
decoded = base64_decode(encoded)
assert len(decoded) == len(VECTOR)
for a, b in zip(decoded, VECTOR):
assert abs(a - b) < ACCEPTABLE_DELTA
def test_decode_known_base64():
decoded = base64_decode(EXPECTED_BASE64)
assert len(decoded) == len(VECTOR)
for a, b in zip(decoded, VECTOR):
assert abs(a - b) < ACCEPTABLE_DELTA
def test_encode_and_decode_with_server(client: Client):
"""
Encode a vector in Python, decode it on the MarkLogic server, and check the result.
"""
encoded = base64_encode(VECTOR)
assert encoded == EXPECTED_BASE64
# Use MarkLogic's eval endpoint to decode the vector on the server
xquery = f"vec:base64-decode('{encoded}')"
binary_result = client.eval(xquery=xquery)
float_list = ast.literal_eval(binary_result[0].decode("utf-8"))
assert len(float_list) == len(VECTOR)
for a, b in zip(float_list, VECTOR):
assert math.isclose(a, b, abs_tol=ACCEPTABLE_DELTA)
def test_encode_with_server_and_decode_with_python(client: Client):
"""
Encode a vector on the MarkLogic server, decode it in Python, and check the result.
"""
xquery = "vec:base64-encode(vec:vector((3.14, 1.59, 2.65)))"
encoded = client.eval(xquery=xquery)[0]
assert encoded == EXPECTED_BASE64
decoded = base64_decode(encoded)
assert len(decoded) == len(VECTOR)
for a, b in zip(decoded, VECTOR):
assert math.isclose(a, b, abs_tol=ACCEPTABLE_DELTA)