Skip to content

Commit c6d2ddd

Browse files
committed
Improved sparsevec tests [skip ci]
1 parent 2ba2a85 commit c6d2ddd

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

pgvector/utils/sparsevec.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def from_binary(cls, value):
108108
dim, nnz, unused = unpack_from('>iii', value)
109109
indices = unpack_from(f'>{nnz}i', value, 12)
110110
values = unpack_from(f'>{nnz}f', value, 12 + nnz * 4)
111+
# TODO convert indices and values to lists in 0.4.0
111112
return cls._from_parts(int(dim), indices, values)
112113

113114
@classmethod

tests/test_psycopg.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,19 @@ def test_sparsevec(self):
110110
def test_sparsevec_binary_format(self):
111111
embedding = SparseVector([1.5, 0, 2, 0, 3, 0])
112112
res = conn.execute('SELECT %b::sparsevec', (embedding,), binary=True).fetchone()[0]
113+
assert res.dimensions() == 6
114+
# TODO convert indices and values to lists in 0.4.0
115+
assert res.indices() == (0, 2, 4)
116+
assert res.values() == (1.5, 2, 3)
113117
assert res.to_list() == [1.5, 0, 2, 0, 3, 0]
114118
assert np.array_equal(res.to_numpy(), np.array([1.5, 0, 2, 0, 3, 0]))
115119

116120
def test_sparsevec_text_format(self):
117121
embedding = SparseVector([1.5, 0, 2, 0, 3, 0])
118122
res = conn.execute('SELECT %t::sparsevec', (embedding,)).fetchone()[0]
123+
assert res.dimensions() == 6
124+
assert res.indices() == [0, 2, 4]
125+
assert res.values() == [1.5, 2, 3]
119126
assert res.to_list() == [1.5, 0, 2, 0, 3, 0]
120127
assert np.array_equal(res.to_numpy(), np.array([1.5, 0, 2, 0, 3, 0]))
121128

0 commit comments

Comments
 (0)