Skip to content

Commit 7f5c2a2

Browse files
ABCDeathelprans
authored andcommitted
Fix Connection class _copy_in private method
Closes: #555
1 parent 1d9457f commit 7f5c2a2

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

asyncpg/connection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ async def _copy_in(self, copy_stmt, source, timeout):
813813

814814
if path is not None:
815815
# a path
816-
f = await run_in_executor(None, open, path, 'wb')
816+
f = await run_in_executor(None, open, path, 'rb')
817817
opened_by_us = True
818818
elif hasattr(source, 'read'):
819819
# file-like

tests/test_copy.py

+43
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import asyncio
99
import datetime
1010
import io
11+
import os
1112
import tempfile
1213

1314
import asyncpg
@@ -582,6 +583,48 @@ async def __anext__(self):
582583
finally:
583584
await self.con.execute('DROP TABLE copytab')
584585

586+
async def test_copy_to_table_from_file_path(self):
587+
await self.con.execute('''
588+
CREATE TABLE copytab(a text, "b~" text, i int);
589+
''')
590+
591+
f = tempfile.NamedTemporaryFile(delete=False)
592+
try:
593+
f.write(
594+
'\n'.join([
595+
'a1\tb1\t1',
596+
'a2\tb2\t2',
597+
'a3\tb3\t3',
598+
'a4\tb4\t4',
599+
'a5\tb5\t5',
600+
'*\t\\N\t\\N',
601+
''
602+
]).encode('utf-8')
603+
)
604+
f.close()
605+
606+
res = await self.con.copy_to_table('copytab', source=f.name)
607+
self.assertEqual(res, 'COPY 6')
608+
609+
output = await self.con.fetch("""
610+
SELECT * FROM copytab ORDER BY a
611+
""")
612+
self.assertEqual(
613+
output,
614+
[
615+
('*', None, None),
616+
('a1', 'b1', 1),
617+
('a2', 'b2', 2),
618+
('a3', 'b3', 3),
619+
('a4', 'b4', 4),
620+
('a5', 'b5', 5),
621+
]
622+
)
623+
624+
finally:
625+
await self.con.execute('DROP TABLE public.copytab')
626+
os.unlink(f.name)
627+
585628
async def test_copy_records_to_table_1(self):
586629
await self.con.execute('''
587630
CREATE TABLE copytab(a text, b int, c timestamptz);

0 commit comments

Comments
 (0)