Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Connection class _copy_in private method #555

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion asyncpg/connection.py
Expand Up @@ -813,7 +813,7 @@ async def _copy_in(self, copy_stmt, source, timeout):

if path is not None:
# a path
f = await run_in_executor(None, open, path, 'wb')
f = await run_in_executor(None, open, path, 'rb')
opened_by_us = True
elif hasattr(source, 'read'):
# file-like
Expand Down
41 changes: 41 additions & 0 deletions tests/test_copy.py
Expand Up @@ -582,6 +582,47 @@ async def __anext__(self):
finally:
await self.con.execute('DROP TABLE copytab')

async def test_copy_to_table_from_file_path(self):
await self.con.execute('''
CREATE TABLE copytab(a text, "b~" text, i int);
''')

with tempfile.NamedTemporaryFile() as f:
try:
f.write(
'\n'.join([
'a1\tb1\t1',
'a2\tb2\t2',
'a3\tb3\t3',
'a4\tb4\t4',
'a5\tb5\t5',
'*\t\\N\t\\N',
''
]).encode('utf-8')
)
f.seek(0)

res = await self.con.copy_to_table('copytab', source=f.name)
self.assertEqual(res, 'COPY 6')

output = await self.con.fetch("""
SELECT * FROM copytab ORDER BY a
""")
self.assertEqual(
output,
[
('*', None, None),
('a1', 'b1', 1),
('a2', 'b2', 2),
('a3', 'b3', 3),
('a4', 'b4', 4),
('a5', 'b5', 5),
]
)

finally:
await self.con.execute('DROP TABLE public.copytab')

async def test_copy_records_to_table_1(self):
await self.con.execute('''
CREATE TABLE copytab(a text, b int, c timestamptz);
Expand Down