forked from pgvector/pgvector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_vector.py
More file actions
117 lines (89 loc) · 3.68 KB
/
test_vector.py
File metadata and controls
117 lines (89 loc) · 3.68 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
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
import numpy as np
from pgvector import Vector
import pytest
from struct import pack
class TestVector:
def test_list(self):
assert Vector([1, 2, 3]).to_list() == [1, 2, 3]
def test_list_str(self):
with pytest.raises(ValueError, match='could not convert string to float'):
Vector([1, 'two', 3])
def test_tuple(self):
assert Vector((1, 2, 3)).to_list() == [1, 2, 3]
def test_ndarray(self):
arr = np.array([1, 2, 3])
assert Vector(arr).to_list() == [1, 2, 3]
assert Vector(arr).to_numpy() is not arr
def test_ndarray_same_object(self):
arr = np.array([1, 2, 3], dtype='>f4')
assert Vector(arr).to_list() == [1, 2, 3]
assert Vector(arr).to_numpy() is arr
def test_ndim_two(self):
with pytest.raises(ValueError) as error:
Vector([[1, 2], [3, 4]])
assert str(error.value) == 'expected ndim to be 1'
def test_ndim_zero(self):
with pytest.raises(ValueError) as error:
Vector(1)
assert str(error.value) == 'expected ndim to be 1'
def test_repr(self):
assert repr(Vector([1, 2, 3])) == 'Vector([1.0, 2.0, 3.0])'
assert str(Vector([1, 2, 3])) == 'Vector([1.0, 2.0, 3.0])'
def test_equality(self):
assert Vector([1, 2, 3]) == Vector([1, 2, 3])
assert Vector([1, 2, 3]) != Vector([1, 2, 4])
def test_dimensions(self):
assert Vector([1, 2, 3]).dimensions() == 3
def test_from_text(self):
vec = Vector.from_text('[1.5,2,3]')
assert vec.to_list() == [1.5, 2, 3]
assert np.array_equal(vec.to_numpy(), [1.5, 2, 3])
def test_from_binary(self):
data = pack('>HH3f', 3, 0, 1.5, 2, 3)
vec = Vector.from_binary(data)
assert vec.to_list() == [1.5, 2, 3]
assert np.array_equal(vec.to_numpy(), [1.5, 2, 3])
assert vec.to_binary() == data
def test_to_text(self):
vec = Vector([1.5, 2, 3])
assert vec.to_text() == '[1.5,2.0,3.0]'
def test_to_db(self):
vec = Vector([1, 2, 3])
assert Vector._to_db(vec) == '[1.0,2.0,3.0]'
def test_to_db_list(self):
assert Vector._to_db([1, 2, 3]) == '[1.0,2.0,3.0]'
def test_to_db_none(self):
assert Vector._to_db(None) is None
def test_to_db_dim(self):
vec = Vector([1, 2, 3])
assert Vector._to_db(vec, 3) == '[1.0,2.0,3.0]'
def test_to_db_dim_invalid(self):
vec = Vector([1, 2, 3])
with pytest.raises(ValueError, match='expected 2 dimensions, not 3'):
Vector._to_db(vec, 2)
def test_to_db_binary(self):
vec = Vector([1, 2, 3])
result = Vector._to_db_binary(vec)
assert result == vec.to_binary()
def test_to_db_binary_list(self):
result = Vector._to_db_binary([1, 2, 3])
assert result == Vector([1, 2, 3]).to_binary()
def test_to_db_binary_none(self):
assert Vector._to_db_binary(None) is None
def test_from_db_text(self):
result = Vector._from_db('[1.5,2,3]')
assert np.array_equal(result, np.array([1.5, 2, 3], dtype=np.float32))
def test_from_db_none(self):
assert Vector._from_db(None) is None
def test_from_db_ndarray(self):
arr = np.array([1, 2, 3])
assert Vector._from_db(arr) is arr
def test_from_db_binary(self):
data = pack('>HH3f', 3, 0, 1.5, 2, 3)
result = Vector._from_db_binary(data)
assert np.array_equal(result, np.array([1.5, 2, 3], dtype=np.float32))
def test_from_db_binary_none(self):
assert Vector._from_db_binary(None) is None
def test_from_db_binary_ndarray(self):
arr = np.array([1, 2, 3])
assert Vector._from_db_binary(arr) is arr