Add tests to prove it works.
The types must be specified as a sequence of oid or PostgreSQL type
names (e.g. ``int4``, ``timestamptz[]``).
-
- .. admonition:: TODO
-
- Only builtin names are supprted for the moment. In order to specify
- custom data types you must use their oid.
-
"""
registry = self.cursor.adapters.types
oids = [
want = [m.args[0] for m in function.pytestmark if m.name == "pg"]
if want:
return check_server_version(got, want[0])
+
+
+@pytest.fixture
+def hstore(svcconn):
+ from psycopg import Error
+
+ try:
+ with svcconn.transaction():
+ svcconn.execute("create extension if not exists hstore")
+ except Error as e:
+ pytest.skip(str(e))
from psycopg import errors as e
from psycopg.pq import Format
from psycopg.adapt import PyFormat as PgFormat
+from psycopg.types import TypeInfo
+from psycopg.types.hstore import register_adapters as register_hstore
from psycopg.types.numeric import Int4
from .utils import gc_collect
assert conn.pgconn.transaction_status == conn.TransactionStatus.INTRANS
+def test_set_custom_type(conn, hstore):
+ command = """copy (select '"a"=>"1", "b"=>"2"'::hstore) to stdout"""
+ cur = conn.cursor()
+
+ with cur.copy(command) as copy:
+ rows = list(copy.rows())
+
+ assert rows == [('"a"=>"1", "b"=>"2"',)]
+
+ register_hstore(TypeInfo.fetch(conn, "hstore"), cur)
+ with cur.copy(command) as copy:
+ copy.set_types(["hstore"])
+ rows = list(copy.rows())
+
+ assert rows == [({"a": "1", "b": "2"},)]
+
+
@pytest.mark.parametrize("format", [Format.TEXT, Format.BINARY])
def test_copy_out_allchars(conn, format):
cur = conn.cursor()
from psycopg import sql
from psycopg import errors as e
from psycopg.pq import Format
+from psycopg.types import TypeInfo
from psycopg.adapt import PyFormat as PgFormat
+from psycopg.types.hstore import register_adapters as register_hstore
from .utils import gc_collect
from .test_copy import sample_text, sample_binary, sample_binary_rows # noqa
assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INTRANS
+async def test_set_custom_type(aconn, hstore):
+ command = """copy (select '"a"=>"1", "b"=>"2"'::hstore) to stdout"""
+ cur = aconn.cursor()
+
+ async with cur.copy(command) as copy:
+ rows = [row async for row in copy.rows()]
+
+ assert rows == [('"a"=>"1", "b"=>"2"',)]
+
+ register_hstore(await TypeInfo.fetch_async(aconn, "hstore"), cur)
+ async with cur.copy(command) as copy:
+ copy.set_types(["hstore"])
+ rows = [row async for row in copy.rows()]
+
+ assert rows == [({"a": "1", "b": "2"},)]
+
+
@pytest.mark.parametrize("format", [Format.TEXT, Format.BINARY])
async def test_copy_out_allchars(aconn, format):
cur = aconn.cursor()
register_adapters(TypeInfo.fetch(conn, "hstore"), conn)
samp1 = conn.execute("select %s", (samp,)).fetchone()[0]
assert samp1 == samp
-
-
-@pytest.fixture
-def hstore(svcconn):
- try:
- with svcconn.transaction():
- svcconn.execute("create extension if not exists hstore")
- except psycopg.Error as e:
- pytest.skip(str(e))