Added test to verify sql objects work with copy_expert()

I'll be honest: I lucked out, I didn't think about this combination. But
maybe sheer luck, maybe using common code paths, it just works. Let's
make it stays so.
This commit is contained in:
Daniele Varrazzo 2017-03-16 00:24:47 +00:00
parent 7187d6408a
commit 3bfbd3a0a5
2 changed files with 26 additions and 2 deletions

View File

@ -1,7 +1,7 @@
"""SQL composition utility module
"""
# psycopg/sql.py - Implementation of the JSON adaptation objects
# psycopg/sql.py - SQL composition utility module
#
# Copyright (C) 2016 Daniele Varrazzo <daniele.varrazzo@gmail.com>
#

View File

@ -23,7 +23,9 @@
# License for more details.
import datetime as dt
from testutils import unittest, ConnectingTestCase, skip_before_python
from cStringIO import StringIO
from testutils import (unittest, ConnectingTestCase,
skip_before_postgres, skip_before_python, skip_copy_if_green)
import psycopg2
from psycopg2 import sql
@ -149,6 +151,28 @@ class SqlFormatTests(ConnectingTestCase):
self.assertEqual(cur.fetchall(),
[(10, 'a', 'b', 'c'), (20, 'd', 'e', 'f')])
@skip_copy_if_green
@skip_before_postgres(8, 2)
def test_copy(self):
cur = self.conn.cursor()
cur.execute("""
create table test_compose (
id serial primary key,
foo text, bar text, "ba'z" text)
""")
s = StringIO("10\ta\tb\tc\n20\td\te\tf\n")
cur.copy_expert(
sql.SQL("copy {t} (id, foo, bar, {f}) from stdin").format(
t=sql.Identifier("test_compose"), f=sql.Identifier("ba'z")), s)
s1 = StringIO()
cur.copy_expert(
sql.SQL("copy (select {f} from {t} order by id) to stdout").format(
t=sql.Identifier("test_compose"), f=sql.Identifier("ba'z")), s1)
s1.seek(0)
self.assertEqual(s1.read(), 'c\nf\n')
class IdentifierTests(ConnectingTestCase):
def test_class(self):