Skip to content

Commit b57a2e9

Browse files
committed
Improved tests [skip ci]
1 parent e6edb2a commit b57a2e9

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

tests/test_half_vector.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
from pgvector import HalfVector
33
import pytest
4+
from struct import pack
45

56

67
class TestHalfVector:
@@ -49,3 +50,10 @@ def test_from_text(self):
4950
vec = HalfVector.from_text('[1.5,2,3]')
5051
assert vec.to_list() == [1.5, 2, 3]
5152
assert np.array_equal(vec.to_numpy(), np.array([1.5, 2, 3]))
53+
54+
def test_from_binary(self):
55+
data = pack('>HH3e', 3, 0, *[1.5, 2, 3])
56+
vec = HalfVector.from_binary(data)
57+
assert vec.to_list() == [1.5, 2, 3]
58+
assert np.array_equal(vec.to_numpy(), np.array([1.5, 2, 3]))
59+
assert vec.to_binary() == data

tests/test_psycopg.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ def test_halfvec_binary_format(self):
7575
embedding = HalfVector([1.5, 2, 3])
7676
res = conn.execute('SELECT %b::halfvec', (embedding,), binary=True).fetchone()[0]
7777
assert res == HalfVector([1.5, 2, 3])
78-
# TODO move
79-
assert res.to_list() == [1.5, 2, 3]
80-
assert np.array_equal(res.to_numpy(), np.array([1.5, 2, 3]))
8178

8279
def test_halfvec_text_format(self):
8380
embedding = HalfVector([1.5, 2, 3])
@@ -113,12 +110,6 @@ def test_sparsevec_binary_format(self):
113110
embedding = SparseVector([1.5, 0, 2, 0, 3, 0])
114111
res = conn.execute('SELECT %b::sparsevec', (embedding,), binary=True).fetchone()[0]
115112
assert res == embedding
116-
# TODO move
117-
assert res.dimensions() == 6
118-
assert res.indices() == [0, 2, 4]
119-
assert res.values() == [1.5, 2, 3]
120-
assert res.to_list() == [1.5, 0, 2, 0, 3, 0]
121-
assert np.array_equal(res.to_numpy(), np.array([1.5, 0, 2, 0, 3, 0]))
122113

123114
def test_sparsevec_text_format(self):
124115
embedding = SparseVector([1.5, 0, 2, 0, 3, 0])

tests/test_sparse_vector.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from pgvector import SparseVector
33
import pytest
44
from scipy.sparse import coo_array
5+
from struct import pack
56

67

78
class TestSparseVector:
@@ -81,3 +82,13 @@ def test_from_text(self):
8182
assert vec.values() == [1.5, 2, 3]
8283
assert vec.to_list() == [1.5, 0, 2, 0, 3, 0]
8384
assert np.array_equal(vec.to_numpy(), np.array([1.5, 0, 2, 0, 3, 0]))
85+
86+
def test_from_binary(self):
87+
data = pack('>iii3i3f', 6, 3, 0, *[0, 2, 4], *[1.5, 2, 3])
88+
vec = SparseVector.from_binary(data)
89+
assert vec.dimensions() == 6
90+
assert vec.indices() == [0, 2, 4]
91+
assert vec.values() == [1.5, 2, 3]
92+
assert vec.to_list() == [1.5, 0, 2, 0, 3, 0]
93+
assert np.array_equal(vec.to_numpy(), np.array([1.5, 0, 2, 0, 3, 0]))
94+
assert vec.to_binary() == data

tests/test_vector.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
from pgvector import Vector
33
import pytest
4+
from struct import pack
45

56

67
class TestVector:
@@ -44,3 +45,15 @@ def test_equality(self):
4445

4546
def test_dimensions(self):
4647
assert Vector([1, 2, 3]).dimensions() == 3
48+
49+
def test_from_text(self):
50+
vec = Vector.from_text('[1.5,2,3]')
51+
assert vec.to_list() == [1.5, 2, 3]
52+
assert np.array_equal(vec.to_numpy(), np.array([1.5, 2, 3]))
53+
54+
def test_from_binary(self):
55+
data = pack('>HH3f', 3, 0, *[1.5, 2, 3])
56+
vec = Vector.from_binary(data)
57+
assert vec.to_list() == [1.5, 2, 3]
58+
assert np.array_equal(vec.to_numpy(), np.array([1.5, 2, 3]))
59+
assert vec.to_binary() == data

0 commit comments

Comments
 (0)