Skip to content

Commit a55d2c2

Browse files
committed
Added test for COPY TO with Psycopg 3
1 parent 25074e0 commit a55d2c2

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

tests/test_psycopg.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,34 @@ def test_sparsevec_text_format(self):
118118
assert res.to_list() == [1.5, 0, 2, 0, 3, 0]
119119
assert np.array_equal(res.to_numpy(), np.array([1.5, 0, 2, 0, 3, 0]))
120120

121-
def test_text_copy(self):
121+
def test_text_copy_from(self):
122122
embedding = np.array([1.5, 2, 3])
123123
cur = conn.cursor()
124124
with cur.copy("COPY psycopg_items (embedding, half_embedding, binary_embedding, sparse_embedding) FROM STDIN") as copy:
125125
copy.write_row([embedding, HalfVector(embedding), '101', SparseVector(embedding)])
126126

127-
def test_binary_copy(self):
127+
def test_binary_copy_from(self):
128128
embedding = np.array([1.5, 2, 3])
129129
cur = conn.cursor()
130130
with cur.copy("COPY psycopg_items (embedding, half_embedding, binary_embedding, sparse_embedding) FROM STDIN WITH (FORMAT BINARY)") as copy:
131131
copy.write_row([embedding, HalfVector(embedding), Bit('101'), SparseVector(embedding)])
132132

133-
def test_binary_copy_set_types(self):
133+
def test_binary_copy_from_set_types(self):
134134
embedding = np.array([1.5, 2, 3])
135135
cur = conn.cursor()
136136
with cur.copy("COPY psycopg_items (id, embedding, half_embedding, binary_embedding, sparse_embedding) FROM STDIN WITH (FORMAT BINARY)") as copy:
137137
copy.set_types(['int8', 'vector', 'halfvec', 'bit', 'sparsevec'])
138138
copy.write_row([1, embedding, HalfVector(embedding), Bit('101'), SparseVector(embedding)])
139139

140+
def test_binary_copy_to_set_types(self):
141+
embedding = np.array([1.5, 2, 3])
142+
conn.execute('INSERT INTO psycopg_items (embedding) VALUES (%s)', (embedding,))
143+
cur = conn.cursor()
144+
with cur.copy("COPY psycopg_items (embedding) TO STDOUT WITH (FORMAT BINARY)") as copy:
145+
copy.set_types(['vector'])
146+
for row in copy.rows():
147+
assert np.array_equal(row[0], embedding)
148+
140149
@pytest.mark.asyncio
141150
async def test_async(self):
142151
conn = await psycopg.AsyncConnection.connect(dbname='pgvector_python_test', autocommit=True)

0 commit comments

Comments
 (0)