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
42 lines (32 loc) · 1.29 KB
/
Copy pathtest_vector.py
File metadata and controls
42 lines (32 loc) · 1.29 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
import numpy as np
from pgvector.utils import Vector
import pytest
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_dimensions(self):
assert Vector([1, 2, 3]).dimensions() == 3