2010-04-08 00:56:19 +04:00
|
|
|
#!/usr/bin/env python
|
2011-01-07 04:44:19 +03:00
|
|
|
|
|
|
|
# test_copy.py - unit test for COPY support
|
|
|
|
#
|
2019-02-17 04:34:52 +03:00
|
|
|
# Copyright (C) 2010-2019 Daniele Varrazzo <daniele.varrazzo@gmail.com>
|
2020-01-18 00:10:44 +03:00
|
|
|
# Copyright (C) 2020 The Psycopg Team
|
2011-01-07 04:44:19 +03:00
|
|
|
#
|
|
|
|
# psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# In addition, as a special exception, the copyright holders give
|
|
|
|
# permission to link this program with the OpenSSL library (or with
|
|
|
|
# modified versions of OpenSSL that use the same license as OpenSSL),
|
|
|
|
# and distribute linked combinations including the two.
|
|
|
|
#
|
|
|
|
# You must obey the GNU Lesser General Public License in all respects for
|
|
|
|
# all of the code used other than OpenSSL.
|
|
|
|
#
|
|
|
|
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
# License for more details.
|
|
|
|
|
2019-03-16 21:56:56 +03:00
|
|
|
import io
|
2011-02-05 17:12:37 +03:00
|
|
|
import sys
|
2010-04-08 00:56:19 +04:00
|
|
|
import string
|
2017-12-02 04:59:53 +03:00
|
|
|
import unittest
|
2018-10-30 03:23:56 +03:00
|
|
|
from .testutils import (ConnectingTestCase, skip_before_postgres, slow, StringIO)
|
2017-12-04 05:47:19 +03:00
|
|
|
from itertools import cycle
|
2014-06-06 23:21:39 +04:00
|
|
|
from subprocess import Popen, PIPE
|
2010-04-08 00:56:19 +04:00
|
|
|
|
|
|
|
import psycopg2
|
|
|
|
import psycopg2.extensions
|
2019-03-16 20:15:16 +03:00
|
|
|
from .testutils import skip_copy_if_green, PY2, TextIOBase
|
2017-12-04 05:47:19 +03:00
|
|
|
from .testconfig import dsn
|
2014-06-06 23:21:39 +04:00
|
|
|
|
2010-04-08 00:56:19 +04:00
|
|
|
|
2019-03-16 21:41:59 +03:00
|
|
|
class MinimalRead(TextIOBase):
|
2010-04-08 00:56:19 +04:00
|
|
|
"""A file wrapper exposing the minimal interface to copy from."""
|
|
|
|
def __init__(self, f):
|
|
|
|
self.f = f
|
|
|
|
|
|
|
|
def read(self, size):
|
|
|
|
return self.f.read(size)
|
|
|
|
|
|
|
|
def readline(self):
|
|
|
|
return self.f.readline()
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2019-03-16 21:41:59 +03:00
|
|
|
class MinimalWrite(TextIOBase):
|
2010-04-08 00:56:19 +04:00
|
|
|
"""A file wrapper exposing the minimal interface to copy to."""
|
|
|
|
def __init__(self, f):
|
|
|
|
self.f = f
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
return self.f.write(data)
|
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
|
2018-10-30 03:23:56 +03:00
|
|
|
@skip_copy_if_green
|
2013-04-07 03:23:30 +04:00
|
|
|
class CopyTests(ConnectingTestCase):
|
2010-04-08 00:56:19 +04:00
|
|
|
|
|
|
|
def setUp(self):
|
2013-04-07 03:23:30 +04:00
|
|
|
ConnectingTestCase.setUp(self)
|
2011-02-05 17:12:37 +03:00
|
|
|
self._create_temp_table()
|
|
|
|
|
|
|
|
def _create_temp_table(self):
|
2010-04-08 00:56:19 +04:00
|
|
|
curs = self.conn.cursor()
|
|
|
|
curs.execute('''
|
|
|
|
CREATE TEMPORARY TABLE tcopy (
|
2011-02-19 17:28:59 +03:00
|
|
|
id serial PRIMARY KEY,
|
2010-04-08 00:56:19 +04:00
|
|
|
data text
|
|
|
|
)''')
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2010-04-08 00:56:19 +04:00
|
|
|
def test_copy_from(self):
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
try:
|
2016-10-11 02:10:53 +03:00
|
|
|
self._copy_from(curs, nrecs=1024, srec=10 * 1024, copykw={})
|
2010-04-08 00:56:19 +04:00
|
|
|
finally:
|
|
|
|
curs.close()
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2010-04-08 00:56:19 +04:00
|
|
|
def test_copy_from_insane_size(self):
|
|
|
|
# Trying to trigger a "would block" error
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
try:
|
2016-10-11 02:10:53 +03:00
|
|
|
self._copy_from(curs, nrecs=10 * 1024, srec=10 * 1024,
|
|
|
|
copykw={'size': 20 * 1024 * 1024})
|
2010-04-08 00:56:19 +04:00
|
|
|
finally:
|
|
|
|
curs.close()
|
|
|
|
|
2010-07-10 03:04:54 +04:00
|
|
|
def test_copy_from_cols(self):
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
f = StringIO()
|
2017-12-04 05:47:19 +03:00
|
|
|
for i in range(10):
|
2010-07-10 03:04:54 +04:00
|
|
|
f.write("%s\n" % (i,))
|
|
|
|
|
|
|
|
f.seek(0)
|
|
|
|
curs.copy_from(MinimalRead(f), "tcopy", columns=['id'])
|
|
|
|
|
|
|
|
curs.execute("select * from tcopy order by id")
|
|
|
|
self.assertEqual([(i, None) for i in range(10)], curs.fetchall())
|
|
|
|
|
2010-07-10 03:12:50 +04:00
|
|
|
def test_copy_from_cols_err(self):
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
f = StringIO()
|
2017-12-04 05:47:19 +03:00
|
|
|
for i in range(10):
|
2010-07-10 03:12:50 +04:00
|
|
|
f.write("%s\n" % (i,))
|
|
|
|
|
|
|
|
f.seek(0)
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2010-07-10 03:12:50 +04:00
|
|
|
def cols():
|
|
|
|
raise ZeroDivisionError()
|
|
|
|
yield 'id'
|
|
|
|
|
|
|
|
self.assertRaises(ZeroDivisionError,
|
|
|
|
curs.copy_from, MinimalRead(f), "tcopy", columns=cols())
|
|
|
|
|
2017-02-02 04:53:50 +03:00
|
|
|
@slow
|
2010-04-08 00:56:19 +04:00
|
|
|
def test_copy_to(self):
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
try:
|
2016-10-11 02:10:53 +03:00
|
|
|
self._copy_from(curs, nrecs=1024, srec=10 * 1024, copykw={})
|
|
|
|
self._copy_to(curs, srec=10 * 1024)
|
2010-04-08 00:56:19 +04:00
|
|
|
finally:
|
|
|
|
curs.close()
|
|
|
|
|
2011-02-05 17:12:37 +03:00
|
|
|
def test_copy_text(self):
|
|
|
|
self.conn.set_client_encoding('latin1')
|
|
|
|
self._create_temp_table() # the above call closed the xn
|
|
|
|
|
2019-03-16 20:15:16 +03:00
|
|
|
if PY2:
|
2011-02-05 17:12:37 +03:00
|
|
|
abin = ''.join(map(chr, range(32, 127) + range(160, 256)))
|
|
|
|
about = abin.decode('latin1').replace('\\', '\\\\')
|
|
|
|
|
|
|
|
else:
|
2018-10-23 02:39:14 +03:00
|
|
|
abin = bytes(list(range(32, 127))
|
|
|
|
+ list(range(160, 256))).decode('latin1')
|
2011-02-05 17:12:37 +03:00
|
|
|
about = abin.replace('\\', '\\\\')
|
|
|
|
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
curs.execute('insert into tcopy values (%s, %s)',
|
|
|
|
(42, abin))
|
|
|
|
|
|
|
|
f = io.StringIO()
|
|
|
|
curs.copy_to(f, 'tcopy', columns=('data',))
|
|
|
|
f.seek(0)
|
|
|
|
self.assertEqual(f.readline().rstrip(), about)
|
|
|
|
|
|
|
|
def test_copy_bytes(self):
|
|
|
|
self.conn.set_client_encoding('latin1')
|
|
|
|
self._create_temp_table() # the above call closed the xn
|
|
|
|
|
2019-03-16 20:15:16 +03:00
|
|
|
if PY2:
|
2011-02-05 17:12:37 +03:00
|
|
|
abin = ''.join(map(chr, range(32, 127) + range(160, 255)))
|
|
|
|
about = abin.replace('\\', '\\\\')
|
|
|
|
else:
|
2018-10-23 02:39:14 +03:00
|
|
|
abin = bytes(list(range(32, 127))
|
|
|
|
+ list(range(160, 255))).decode('latin1')
|
2011-02-05 17:12:37 +03:00
|
|
|
about = abin.replace('\\', '\\\\').encode('latin1')
|
|
|
|
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
curs.execute('insert into tcopy values (%s, %s)',
|
|
|
|
(42, abin))
|
|
|
|
|
|
|
|
f = io.BytesIO()
|
|
|
|
curs.copy_to(f, 'tcopy', columns=('data',))
|
|
|
|
f.seek(0)
|
|
|
|
self.assertEqual(f.readline().rstrip(), about)
|
|
|
|
|
2011-02-19 17:28:59 +03:00
|
|
|
def test_copy_expert_textiobase(self):
|
|
|
|
self.conn.set_client_encoding('latin1')
|
|
|
|
self._create_temp_table() # the above call closed the xn
|
|
|
|
|
2019-03-16 20:15:16 +03:00
|
|
|
if PY2:
|
2011-02-19 17:28:59 +03:00
|
|
|
abin = ''.join(map(chr, range(32, 127) + range(160, 256)))
|
|
|
|
abin = abin.decode('latin1')
|
|
|
|
about = abin.replace('\\', '\\\\')
|
|
|
|
|
|
|
|
else:
|
2018-10-23 02:39:14 +03:00
|
|
|
abin = bytes(list(range(32, 127))
|
|
|
|
+ list(range(160, 256))).decode('latin1')
|
2011-02-19 17:28:59 +03:00
|
|
|
about = abin.replace('\\', '\\\\')
|
|
|
|
|
|
|
|
f = io.StringIO()
|
|
|
|
f.write(about)
|
|
|
|
f.seek(0)
|
|
|
|
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
psycopg2.extensions.register_type(
|
|
|
|
psycopg2.extensions.UNICODE, curs)
|
|
|
|
|
|
|
|
curs.copy_expert('COPY tcopy (data) FROM STDIN', f)
|
|
|
|
curs.execute("select data from tcopy;")
|
|
|
|
self.assertEqual(curs.fetchone()[0], abin)
|
|
|
|
|
|
|
|
f = io.StringIO()
|
|
|
|
curs.copy_expert('COPY tcopy (data) TO STDOUT', f)
|
|
|
|
f.seek(0)
|
|
|
|
self.assertEqual(f.readline().rstrip(), about)
|
|
|
|
|
2013-05-06 13:48:10 +04:00
|
|
|
# same tests with setting size
|
|
|
|
f = io.StringIO()
|
|
|
|
f.write(about)
|
|
|
|
f.seek(0)
|
|
|
|
exp_size = 123
|
|
|
|
# hack here to leave file as is, only check size when reading
|
|
|
|
real_read = f.read
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2013-05-06 13:48:10 +04:00
|
|
|
def read(_size, f=f, exp_size=exp_size):
|
|
|
|
self.assertEqual(_size, exp_size)
|
|
|
|
return real_read(_size)
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2013-05-06 13:48:10 +04:00
|
|
|
f.read = read
|
|
|
|
curs.copy_expert('COPY tcopy (data) FROM STDIN', f, size=exp_size)
|
|
|
|
curs.execute("select data from tcopy;")
|
|
|
|
self.assertEqual(curs.fetchone()[0], abin)
|
2011-02-19 17:28:59 +03:00
|
|
|
|
2010-04-08 00:56:19 +04:00
|
|
|
def _copy_from(self, curs, nrecs, srec, copykw):
|
|
|
|
f = StringIO()
|
2017-12-04 05:47:19 +03:00
|
|
|
for i, c in zip(range(nrecs), cycle(string.ascii_letters)):
|
2010-04-08 00:56:19 +04:00
|
|
|
l = c * srec
|
2016-10-11 02:10:53 +03:00
|
|
|
f.write("%s\t%s\n" % (i, l))
|
2010-04-08 00:56:19 +04:00
|
|
|
|
|
|
|
f.seek(0)
|
|
|
|
curs.copy_from(MinimalRead(f), "tcopy", **copykw)
|
|
|
|
|
|
|
|
curs.execute("select count(*) from tcopy")
|
|
|
|
self.assertEqual(nrecs, curs.fetchone()[0])
|
|
|
|
|
|
|
|
curs.execute("select data from tcopy where id < %s order by id",
|
2010-12-28 17:12:43 +03:00
|
|
|
(len(string.ascii_letters),))
|
2010-04-08 00:56:19 +04:00
|
|
|
for i, (l,) in enumerate(curs):
|
2010-12-28 17:12:43 +03:00
|
|
|
self.assertEqual(l, string.ascii_letters[i] * srec)
|
2010-04-08 00:56:19 +04:00
|
|
|
|
|
|
|
def _copy_to(self, curs, srec):
|
|
|
|
f = StringIO()
|
|
|
|
curs.copy_to(MinimalWrite(f), "tcopy")
|
|
|
|
|
|
|
|
f.seek(0)
|
|
|
|
ntests = 0
|
|
|
|
for line in f:
|
|
|
|
n, s = line.split()
|
2010-12-28 17:12:43 +03:00
|
|
|
if int(n) < len(string.ascii_letters):
|
|
|
|
self.assertEqual(s, string.ascii_letters[int(n)] * srec)
|
2010-04-08 00:56:19 +04:00
|
|
|
ntests += 1
|
|
|
|
|
2010-12-28 17:12:43 +03:00
|
|
|
self.assertEqual(ntests, len(string.ascii_letters))
|
2010-04-08 00:56:19 +04:00
|
|
|
|
2011-06-07 04:20:25 +04:00
|
|
|
def test_copy_expert_file_refcount(self):
|
|
|
|
class Whatever(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
f = Whatever()
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
self.assertRaises(TypeError,
|
|
|
|
curs.copy_expert, 'COPY tcopy (data) FROM STDIN', f)
|
|
|
|
|
2011-09-12 05:21:59 +04:00
|
|
|
def test_copy_no_column_limit(self):
|
2016-10-11 02:10:53 +03:00
|
|
|
cols = ["c%050d" % i for i in range(200)]
|
2011-09-12 05:21:59 +04:00
|
|
|
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
curs.execute('CREATE TEMPORARY TABLE manycols (%s)' % ',\n'.join(
|
2016-10-11 02:10:53 +03:00
|
|
|
["%s int" % c for c in cols]))
|
2011-09-12 05:21:59 +04:00
|
|
|
curs.execute("INSERT INTO manycols DEFAULT VALUES")
|
|
|
|
|
|
|
|
f = StringIO()
|
2016-10-11 02:10:53 +03:00
|
|
|
curs.copy_to(f, "manycols", columns=cols)
|
2011-09-12 05:21:59 +04:00
|
|
|
f.seek(0)
|
|
|
|
self.assertEqual(f.read().split(), ['\\N'] * len(cols))
|
|
|
|
|
|
|
|
f.seek(0)
|
2016-10-11 02:10:53 +03:00
|
|
|
curs.copy_from(f, "manycols", columns=cols)
|
2011-09-12 05:21:59 +04:00
|
|
|
curs.execute("select count(*) from manycols;")
|
|
|
|
self.assertEqual(curs.fetchone()[0], 2)
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
@skip_before_postgres(8, 2) # they don't send the count
|
2014-05-06 02:52:41 +04:00
|
|
|
def test_copy_rowcount(self):
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
|
|
|
|
curs.copy_from(StringIO('aaa\nbbb\nccc\n'), 'tcopy', columns=['data'])
|
|
|
|
self.assertEqual(curs.rowcount, 3)
|
|
|
|
|
|
|
|
curs.copy_expert(
|
|
|
|
"copy tcopy (data) from stdin",
|
|
|
|
StringIO('ddd\neee\n'))
|
|
|
|
self.assertEqual(curs.rowcount, 2)
|
|
|
|
|
|
|
|
curs.copy_to(StringIO(), "tcopy")
|
|
|
|
self.assertEqual(curs.rowcount, 5)
|
|
|
|
|
|
|
|
curs.execute("insert into tcopy (data) values ('fff')")
|
|
|
|
curs.copy_expert("copy tcopy to stdout", StringIO())
|
|
|
|
self.assertEqual(curs.rowcount, 6)
|
|
|
|
|
|
|
|
def test_copy_rowcount_error(self):
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
|
|
|
|
curs.execute("insert into tcopy (data) values ('fff')")
|
|
|
|
self.assertEqual(curs.rowcount, 1)
|
|
|
|
|
|
|
|
self.assertRaises(psycopg2.DataError,
|
|
|
|
curs.copy_from, StringIO('aaa\nbbb\nccc\n'), 'tcopy')
|
|
|
|
self.assertEqual(curs.rowcount, -1)
|
|
|
|
|
2017-02-02 05:58:22 +03:00
|
|
|
@slow
|
2014-06-06 23:21:39 +04:00
|
|
|
def test_copy_from_segfault(self):
|
|
|
|
# issue #219
|
|
|
|
script = ("""\
|
|
|
|
import psycopg2
|
|
|
|
conn = psycopg2.connect(%(dsn)r)
|
|
|
|
curs = conn.cursor()
|
|
|
|
curs.execute("create table copy_segf (id int)")
|
|
|
|
try:
|
|
|
|
curs.execute("copy copy_segf from stdin")
|
|
|
|
except psycopg2.ProgrammingError:
|
|
|
|
pass
|
|
|
|
conn.close()
|
2016-10-11 02:10:53 +03:00
|
|
|
""" % {'dsn': dsn})
|
2014-06-06 23:21:39 +04:00
|
|
|
|
2017-11-29 07:58:41 +03:00
|
|
|
proc = Popen([sys.executable, '-c', script])
|
2014-06-06 23:21:39 +04:00
|
|
|
proc.communicate()
|
|
|
|
self.assertEqual(0, proc.returncode)
|
|
|
|
|
2017-02-02 05:58:22 +03:00
|
|
|
@slow
|
2014-06-06 23:21:39 +04:00
|
|
|
def test_copy_to_segfault(self):
|
|
|
|
# issue #219
|
|
|
|
script = ("""\
|
|
|
|
import psycopg2
|
|
|
|
conn = psycopg2.connect(%(dsn)r)
|
|
|
|
curs = conn.cursor()
|
|
|
|
curs.execute("create table copy_segf (id int)")
|
|
|
|
try:
|
|
|
|
curs.execute("copy copy_segf to stdout")
|
|
|
|
except psycopg2.ProgrammingError:
|
|
|
|
pass
|
|
|
|
conn.close()
|
2016-10-11 02:10:53 +03:00
|
|
|
""" % {'dsn': dsn})
|
2014-06-06 23:21:39 +04:00
|
|
|
|
2017-11-29 07:58:41 +03:00
|
|
|
proc = Popen([sys.executable, '-c', script], stdout=PIPE)
|
2014-06-06 23:21:39 +04:00
|
|
|
proc.communicate()
|
|
|
|
self.assertEqual(0, proc.returncode)
|
2011-09-12 05:21:59 +04:00
|
|
|
|
2015-02-08 04:42:21 +03:00
|
|
|
def test_copy_from_propagate_error(self):
|
2019-03-16 21:41:59 +03:00
|
|
|
class BrokenRead(TextIOBase):
|
2015-02-08 04:42:21 +03:00
|
|
|
def read(self, size):
|
2016-10-11 02:10:53 +03:00
|
|
|
return 1 / 0
|
2015-02-08 04:42:21 +03:00
|
|
|
|
|
|
|
def readline(self):
|
2016-10-11 02:10:53 +03:00
|
|
|
return 1 / 0
|
2015-02-08 04:42:21 +03:00
|
|
|
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
# It seems we cannot do this, but now at least we propagate the error
|
|
|
|
# self.assertRaises(ZeroDivisionError,
|
|
|
|
# curs.copy_from, BrokenRead(), "tcopy")
|
|
|
|
try:
|
|
|
|
curs.copy_from(BrokenRead(), "tcopy")
|
2017-11-21 07:00:35 +03:00
|
|
|
except Exception as e:
|
2015-02-08 04:42:21 +03:00
|
|
|
self.assert_('ZeroDivisionError' in str(e))
|
|
|
|
|
|
|
|
def test_copy_to_propagate_error(self):
|
2019-03-16 21:41:59 +03:00
|
|
|
class BrokenWrite(TextIOBase):
|
2015-02-08 04:42:21 +03:00
|
|
|
def write(self, data):
|
2016-10-11 02:10:53 +03:00
|
|
|
return 1 / 0
|
2015-02-08 04:42:21 +03:00
|
|
|
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
curs.execute("insert into tcopy values (10, 'hi')")
|
|
|
|
self.assertRaises(ZeroDivisionError,
|
|
|
|
curs.copy_to, BrokenWrite(), "tcopy")
|
|
|
|
|
|
|
|
|
2010-04-08 00:56:19 +04:00
|
|
|
def test_suite():
|
|
|
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
|
|
|
|
2018-10-23 02:39:14 +03:00
|
|
|
|
2010-04-08 00:56:19 +04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|