-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathtest_asyncpg.py
More file actions
147 lines (114 loc) · 6.46 KB
/
Copy pathtest_asyncpg.py
File metadata and controls
147 lines (114 loc) · 6.46 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import asyncpg
from asyncpg import Connection
from pgvector import HalfVector, SparseVector, Vector
from pgvector.asyncpg import register_vector
import pytest
from .conftest import numpy as np
class TestAsyncpg:
async def setup_connection(self) -> Connection:
conn = await asyncpg.connect(database='pgvector_python_test')
await conn.execute('CREATE EXTENSION IF NOT EXISTS vector')
await register_vector(conn)
return conn
@pytest.mark.asyncio
async def test_vector(self) -> None:
conn = await self.setup_connection()
await conn.execute('DROP TABLE IF EXISTS asyncpg_items')
await conn.execute('CREATE TABLE asyncpg_items (id bigserial PRIMARY KEY, embedding vector(3))')
embedding = Vector([1.5, 2, 3])
embedding2 = [4.5, 5, 6]
embedding3 = np.array([7.5, 8, 9]) if np is not None else [7.5, 8, 9]
embedding4 = None
await conn.execute('INSERT INTO asyncpg_items (embedding) VALUES ($1), ($2), ($3), ($4)', embedding, embedding2, embedding3, embedding4)
res = await conn.fetch('SELECT * FROM asyncpg_items ORDER BY id')
assert res[0]['embedding'] == embedding
assert res[1]['embedding'] == Vector(embedding2)
assert res[2]['embedding'] == Vector(embedding3)
assert res[3]['embedding'] is None
# ensures binary format is correct
text_res = await conn.fetch('SELECT embedding::text FROM asyncpg_items ORDER BY id LIMIT 1')
assert text_res[0]['embedding'] == '[1.5,2,3]'
await conn.close()
@pytest.mark.asyncio
async def test_halfvec(self) -> None:
conn = await self.setup_connection()
await conn.execute('DROP TABLE IF EXISTS asyncpg_items')
await conn.execute('CREATE TABLE asyncpg_items (id bigserial PRIMARY KEY, embedding halfvec(3))')
embedding = HalfVector([1.5, 2, 3])
embedding2 = [4.5, 5, 6]
embedding3 = None
await conn.execute('INSERT INTO asyncpg_items (embedding) VALUES ($1), ($2), ($3)', embedding, embedding2, embedding3)
res = await conn.fetch('SELECT * FROM asyncpg_items ORDER BY id')
assert res[0]['embedding'] == embedding
assert res[1]['embedding'] == HalfVector(embedding2)
assert res[2]['embedding'] is None
# ensures binary format is correct
text_res = await conn.fetch('SELECT embedding::text FROM asyncpg_items ORDER BY id LIMIT 1')
assert text_res[0]['embedding'] == '[1.5,2,3]'
await conn.close()
@pytest.mark.asyncio
async def test_bit(self) -> None:
conn = await self.setup_connection()
await conn.execute('DROP TABLE IF EXISTS asyncpg_items')
await conn.execute('CREATE TABLE asyncpg_items (id bigserial PRIMARY KEY, embedding bit(3))')
# typing issue
# https://github.com/MagicStack/py-pgproto/pull/32
embedding = asyncpg.BitString('101') # type: ignore
embedding2 = None
await conn.execute('INSERT INTO asyncpg_items (embedding) VALUES ($1), ($2)', embedding, embedding2)
res = await conn.fetch('SELECT * FROM asyncpg_items ORDER BY id')
assert res[0]['embedding'].as_string() == '101'
assert res[0]['embedding'].to_int() == 5
assert res[1]['embedding'] is None
# ensures binary format is correct
text_res = await conn.fetch('SELECT embedding::text FROM asyncpg_items ORDER BY id LIMIT 1')
assert text_res[0]['embedding'] == '101'
await conn.close()
@pytest.mark.asyncio
async def test_sparsevec(self) -> None:
conn = await self.setup_connection()
await conn.execute('DROP TABLE IF EXISTS asyncpg_items')
await conn.execute('CREATE TABLE asyncpg_items (id bigserial PRIMARY KEY, embedding sparsevec(3))')
embedding = SparseVector([1.5, 2, 3])
embedding2 = None
await conn.execute('INSERT INTO asyncpg_items (embedding) VALUES ($1), ($2)', embedding, embedding2)
res = await conn.fetch('SELECT * FROM asyncpg_items ORDER BY id')
assert res[0]['embedding'] == embedding
assert res[1]['embedding'] is None
# ensures binary format is correct
text_res = await conn.fetch('SELECT embedding::text FROM asyncpg_items ORDER BY id LIMIT 1')
assert text_res[0]['embedding'] == '{1:1.5,2:2,3:3}/3'
await conn.close()
@pytest.mark.asyncio
async def test_vector_array(self) -> None:
conn = await self.setup_connection()
await conn.execute('DROP TABLE IF EXISTS asyncpg_items')
await conn.execute('CREATE TABLE asyncpg_items (id bigserial PRIMARY KEY, embeddings vector[])')
embeddings = [Vector([1.5, 2, 3]), Vector([4.5, 5, 6])]
await conn.execute('INSERT INTO asyncpg_items (embeddings) VALUES ($1)', embeddings)
embeddings2 = [[1.5, 2, 3], [4.5, 5, 6]]
await conn.execute('INSERT INTO asyncpg_items (embeddings) VALUES (ARRAY[$1, $2]::vector[])', embeddings2[0], embeddings2[1])
embeddings3 = [np.array([1.5, 2, 3]), np.array([4.5, 5, 6])] if np is not None else [[1.5, 2, 3], [4.5, 5, 6]]
await conn.execute('INSERT INTO asyncpg_items (embeddings) VALUES (ARRAY[$1, $2]::vector[])', embeddings3[0], embeddings3[1])
res = await conn.fetch('SELECT * FROM asyncpg_items ORDER BY id')
assert res[0]['embeddings'] == embeddings
assert res[1]['embeddings'] == [Vector(e) for e in embeddings2]
assert res[2]['embeddings'] == [Vector(e) for e in embeddings3]
await conn.close()
@pytest.mark.asyncio
async def test_pool(self) -> None:
async def init(conn: Connection) -> None:
await register_vector(conn)
pool = await asyncpg.create_pool(database='pgvector_python_test', init=init)
async with pool.acquire() as conn:
await conn.execute('CREATE EXTENSION IF NOT EXISTS vector')
await conn.execute('DROP TABLE IF EXISTS asyncpg_items')
await conn.execute('CREATE TABLE asyncpg_items (id bigserial PRIMARY KEY, embedding vector(3))')
embedding = Vector([1.5, 2, 3])
embedding2 = [1.5, 2, 3]
embedding3 = None
await conn.execute('INSERT INTO asyncpg_items (embedding) VALUES ($1), ($2), ($3)', embedding, embedding2, embedding3)
res = await conn.fetch('SELECT * FROM asyncpg_items ORDER BY id')
assert res[0]['embedding'] == embedding
assert res[1]['embedding'] == Vector(embedding2)
assert res[2]['embedding'] is None