Skip to content

Commit 536c3fb

Browse files
committed
DRYed tests [skip ci]
1 parent af6f9fc commit 536c3fb

1 file changed

Lines changed: 11 additions & 24 deletions

File tree

tests/test_asyncpg.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55

66

77
class TestAsyncpg:
8-
@pytest.mark.asyncio
9-
async def test_vector(self):
8+
async def setup_connection(self):
109
conn = await asyncpg.connect(database='pgvector_python_test')
1110
await conn.execute('CREATE EXTENSION IF NOT EXISTS vector')
11+
await register_vector(conn)
1212
await conn.execute('DROP TABLE IF EXISTS asyncpg_items')
13-
await conn.execute('CREATE TABLE asyncpg_items (id bigserial PRIMARY KEY, embedding vector(3))')
13+
return conn
1414

15-
await register_vector(conn)
15+
@pytest.mark.asyncio
16+
async def test_vector(self):
17+
conn = await self.setup_connection();
18+
await conn.execute('CREATE TABLE asyncpg_items (id bigserial PRIMARY KEY, embedding vector(3))')
1619

1720
embedding = Vector([1.5, 2, 3])
1821
embedding2 = [4.5, 5, 6]
@@ -31,13 +34,9 @@ async def test_vector(self):
3134

3235
@pytest.mark.asyncio
3336
async def test_halfvec(self):
34-
conn = await asyncpg.connect(database='pgvector_python_test')
35-
await conn.execute('CREATE EXTENSION IF NOT EXISTS vector')
36-
await conn.execute('DROP TABLE IF EXISTS asyncpg_items')
37+
conn = await self.setup_connection();
3738
await conn.execute('CREATE TABLE asyncpg_items (id bigserial PRIMARY KEY, embedding halfvec(3))')
3839

39-
await register_vector(conn)
40-
4140
embedding = HalfVector([1.5, 2, 3])
4241
embedding2 = [4.5, 5, 6]
4342
await conn.execute("INSERT INTO asyncpg_items (embedding) VALUES ($1), ($2), (NULL)", embedding, embedding2)
@@ -55,13 +54,9 @@ async def test_halfvec(self):
5554

5655
@pytest.mark.asyncio
5756
async def test_bit(self):
58-
conn = await asyncpg.connect(database='pgvector_python_test')
59-
await conn.execute('CREATE EXTENSION IF NOT EXISTS vector')
60-
await conn.execute('DROP TABLE IF EXISTS asyncpg_items')
57+
conn = await self.setup_connection();
6158
await conn.execute('CREATE TABLE asyncpg_items (id bigserial PRIMARY KEY, embedding bit(3))')
6259

63-
await register_vector(conn)
64-
6560
embedding = asyncpg.BitString('101') # type: ignore
6661
await conn.execute("INSERT INTO asyncpg_items (embedding) VALUES ($1), (NULL)", embedding)
6762

@@ -78,13 +73,9 @@ async def test_bit(self):
7873

7974
@pytest.mark.asyncio
8075
async def test_sparsevec(self):
81-
conn = await asyncpg.connect(database='pgvector_python_test')
82-
await conn.execute('CREATE EXTENSION IF NOT EXISTS vector')
83-
await conn.execute('DROP TABLE IF EXISTS asyncpg_items')
76+
conn = await self.setup_connection();
8477
await conn.execute('CREATE TABLE asyncpg_items (id bigserial PRIMARY KEY, embedding sparsevec(3))')
8578

86-
await register_vector(conn)
87-
8879
embedding = SparseVector([1.5, 2, 3])
8980
await conn.execute("INSERT INTO asyncpg_items (embedding) VALUES ($1), (NULL)", embedding)
9081

@@ -100,13 +91,9 @@ async def test_sparsevec(self):
10091

10192
@pytest.mark.asyncio
10293
async def test_vector_array(self):
103-
conn = await asyncpg.connect(database='pgvector_python_test')
104-
await conn.execute('CREATE EXTENSION IF NOT EXISTS vector')
105-
await conn.execute('DROP TABLE IF EXISTS asyncpg_items')
94+
conn = await self.setup_connection();
10695
await conn.execute('CREATE TABLE asyncpg_items (id bigserial PRIMARY KEY, embeddings vector[])')
10796

108-
await register_vector(conn)
109-
11097
embeddings = [Vector([1.5, 2, 3]), Vector([4.5, 5, 6])]
11198
await conn.execute("INSERT INTO asyncpg_items (embeddings) VALUES ($1)", embeddings)
11299

0 commit comments

Comments
 (0)