Skip to content

Commit d702a70

Browse files
committed
Improved tests for from_binary methods
1 parent a755137 commit d702a70

4 files changed

Lines changed: 58 additions & 11 deletions

File tree

‎tests/test_bit.py‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pgvector import Bit
22
import pytest
33
import random
4-
from struct import pack
4+
import struct
55
from .conftest import numpy as np
66

77

@@ -104,14 +104,22 @@ def test_from_text(self) -> None:
104104
assert vec.to_text() == '101'
105105

106106
def test_from_binary(self) -> None:
107-
data = pack('>iB', 3, 5 << 5)
107+
data = struct.pack('>iB', 3, 5 << 5)
108108
vec = Bit.from_binary(data)
109109
assert vec.to_list() == [True, False, True]
110110
if np is not None:
111111
assert np.array_equal(vec.to_numpy(), [True, False, True])
112112
assert vec.to_binary() == data
113113

114+
def test_from_binary_zero_length(self) -> None:
115+
# TODO change to ValueError('invalid length') in 0.6.0
116+
with pytest.raises(struct.error, match='unpack_from requires a buffer of at least 4 bytes'):
117+
Bit.from_binary(b'')
118+
114119
def test_from_binary_negative_length(self) -> None:
115-
with pytest.raises(ValueError) as error:
116-
Bit.from_binary(pack('>i', -1))
117-
assert str(error.value) == 'invalid length'
120+
with pytest.raises(ValueError, match='invalid length'):
121+
Bit.from_binary(struct.pack('>i', -1))
122+
123+
def test_from_binary_invalid_length(self) -> None:
124+
with pytest.raises(ValueError, match='invalid length'):
125+
Bit.from_binary(struct.pack('>i', 1))

‎tests/test_half_vector.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pgvector import HalfVector
22
import pytest
3-
from struct import pack
3+
import struct
44
from .conftest import numpy as np
55

66

@@ -68,9 +68,22 @@ def test_from_text(self) -> None:
6868
assert vec.to_text() == '[1.5,2.0,3.0]'
6969

7070
def test_from_binary(self) -> None:
71-
data = pack('>HH3e', 3, 0, 1.5, 2, 3)
71+
data = struct.pack('>HH3e', 3, 0, 1.5, 2, 3)
7272
vec = HalfVector.from_binary(data)
7373
assert vec.to_list() == [1.5, 2, 3]
7474
if np is not None:
7575
assert np.array_equal(vec.to_numpy(), [1.5, 2, 3])
7676
assert vec.to_binary() == data
77+
78+
def test_from_binary_zero_length(self) -> None:
79+
# TODO change to ValueError('invalid length') in 0.6.0
80+
with pytest.raises(struct.error, match='unpack_from requires a buffer of at least 4 bytes'):
81+
HalfVector.from_binary(b'')
82+
83+
def test_from_binary_negative_dimensions(self) -> None:
84+
with pytest.raises(ValueError, match='invalid length'):
85+
HalfVector.from_binary(struct.pack('>hh', -1, 0))
86+
87+
def test_from_binary_non_zero_unused(self) -> None:
88+
with pytest.raises(ValueError, match='expected unused to be 0'):
89+
HalfVector.from_binary(struct.pack('>hh', 0, 1))

‎tests/test_sparse_vector.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pgvector import SparseVector
22
import pytest
3-
from struct import pack
3+
import struct
44
from .conftest import numpy as np
55

66
try:
@@ -180,7 +180,7 @@ def test_from_text(self) -> None:
180180
# SparseVector.from_text('{2:1,2:1}/3')
181181

182182
def test_from_binary(self) -> None:
183-
data = pack('>iii3i3f', 6, 3, 0, 0, 2, 4, 1.5, 2, 3)
183+
data = struct.pack('>iii3i3f', 6, 3, 0, 0, 2, 4, 1.5, 2, 3)
184184
vec = SparseVector.from_binary(data)
185185
assert vec.dimensions() == 6
186186
assert vec.indices() == [0, 2, 4]
@@ -189,3 +189,16 @@ def test_from_binary(self) -> None:
189189
if np is not None:
190190
assert np.array_equal(vec.to_numpy(), [1.5, 0, 2, 0, 3, 0])
191191
assert vec.to_binary() == data
192+
193+
def test_from_binary_zero_length(self) -> None:
194+
# TODO change to ValueError('invalid length') in 0.6.0
195+
with pytest.raises(struct.error, match='unpack_from requires a buffer of at least 12 bytes'):
196+
SparseVector.from_binary(b'')
197+
198+
def test_from_binary_negative_nnz(self) -> None:
199+
with pytest.raises(ValueError, match='invalid length'):
200+
SparseVector.from_binary(struct.pack('>iii', 0, -1, 0))
201+
202+
def test_from_binary_non_zero_unused(self) -> None:
203+
with pytest.raises(ValueError, match='expected unused to be 0'):
204+
SparseVector.from_binary(struct.pack('>iii', 0, 0, 1))

‎tests/test_vector.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pgvector import Vector
22
import pytest
3-
from struct import pack
3+
import struct
44
from .conftest import numpy as np
55

66

@@ -68,9 +68,22 @@ def test_from_text(self) -> None:
6868
assert vec.to_text() == '[1.5,2.0,3.0]'
6969

7070
def test_from_binary(self) -> None:
71-
data = pack('>HH3f', 3, 0, 1.5, 2, 3)
71+
data = struct.pack('>hh3f', 3, 0, 1.5, 2, 3)
7272
vec = Vector.from_binary(data)
7373
assert vec.to_list() == [1.5, 2, 3]
7474
if np is not None:
7575
assert np.array_equal(vec.to_numpy(), [1.5, 2, 3])
7676
assert vec.to_binary() == data
77+
78+
def test_from_binary_zero_length(self) -> None:
79+
# TODO change to ValueError('invalid length') in 0.6.0
80+
with pytest.raises(struct.error, match='unpack_from requires a buffer of at least 4 bytes'):
81+
Vector.from_binary(b'')
82+
83+
def test_from_binary_negative_dimensions(self) -> None:
84+
with pytest.raises(ValueError, match='invalid length'):
85+
Vector.from_binary(struct.pack('>hh', -1, 0))
86+
87+
def test_from_binary_non_zero_unused(self) -> None:
88+
with pytest.raises(ValueError, match='expected unused to be 0'):
89+
Vector.from_binary(struct.pack('>hh', 0, 1))

0 commit comments

Comments
 (0)