2008-09-19 23:25:16 +04:00
|
|
|
#!/usr/bin/env python
|
2010-02-13 01:34:53 +03:00
|
|
|
#
|
2008-09-19 23:25:16 +04:00
|
|
|
# types_extras.py - tests for extras types conversions
|
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# Copyright (C) 2008-2010 Federico Di Gregorio <fog@debian.org>
|
2008-09-19 23:25:16 +04:00
|
|
|
#
|
2010-02-13 01:34:53 +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.
|
2008-09-19 23:25:16 +04:00
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# 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.
|
2008-09-19 23:25:16 +04:00
|
|
|
|
|
|
|
try:
|
|
|
|
import decimal
|
|
|
|
except:
|
|
|
|
pass
|
2010-09-26 02:13:51 +04:00
|
|
|
import re
|
2008-09-19 23:25:16 +04:00
|
|
|
import sys
|
2011-01-02 02:34:13 +03:00
|
|
|
from datetime import date
|
|
|
|
|
2011-09-22 21:53:21 +04:00
|
|
|
from testutils import unittest, skip_if_no_uuid, skip_before_postgres
|
2008-09-19 23:25:16 +04:00
|
|
|
|
|
|
|
import psycopg2
|
|
|
|
import psycopg2.extras
|
2011-02-11 01:59:31 +03:00
|
|
|
from psycopg2.extensions import b
|
|
|
|
|
|
|
|
from testconfig import dsn
|
2008-09-19 23:25:16 +04:00
|
|
|
|
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
def filter_scs(conn, s):
|
|
|
|
if conn.get_parameter_status("standard_conforming_strings") == 'off':
|
|
|
|
return s
|
|
|
|
else:
|
2011-02-11 01:59:31 +03:00
|
|
|
return s.replace(b("E'"), b("'"))
|
2010-11-19 06:55:37 +03:00
|
|
|
|
2009-03-02 12:59:52 +03:00
|
|
|
class TypesExtrasTests(unittest.TestCase):
|
2008-09-19 23:25:16 +04:00
|
|
|
"""Test that all type conversions are working."""
|
|
|
|
|
|
|
|
def setUp(self):
|
2011-02-11 01:59:31 +03:00
|
|
|
self.conn = psycopg2.connect(dsn)
|
2008-09-19 23:25:16 +04:00
|
|
|
|
2010-11-28 19:00:32 +03:00
|
|
|
def tearDown(self):
|
|
|
|
self.conn.close()
|
|
|
|
|
2008-09-19 23:25:16 +04:00
|
|
|
def execute(self, *args):
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
curs.execute(*args)
|
|
|
|
return curs.fetchone()[0]
|
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
@skip_if_no_uuid
|
2008-09-19 23:25:16 +04:00
|
|
|
def testUUID(self):
|
2010-11-19 06:55:37 +03:00
|
|
|
import uuid
|
|
|
|
psycopg2.extras.register_uuid()
|
2009-10-04 14:34:02 +04:00
|
|
|
u = uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e350')
|
2008-09-19 23:25:16 +04:00
|
|
|
s = self.execute("SELECT %s AS foo", (u,))
|
|
|
|
self.failUnless(u == s)
|
2008-09-23 11:11:11 +04:00
|
|
|
# must survive NULL cast to a uuid
|
|
|
|
s = self.execute("SELECT NULL::uuid AS foo")
|
|
|
|
self.failUnless(s is None)
|
2008-09-19 23:25:16 +04:00
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
@skip_if_no_uuid
|
2009-10-04 14:34:02 +04:00
|
|
|
def testUUIDARRAY(self):
|
2010-11-19 06:55:37 +03:00
|
|
|
import uuid
|
|
|
|
psycopg2.extras.register_uuid()
|
2009-10-04 14:34:02 +04:00
|
|
|
u = [uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e350'), uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e352')]
|
|
|
|
s = self.execute("SELECT %s AS foo", (u,))
|
|
|
|
self.failUnless(u == s)
|
|
|
|
# array with a NULL element
|
|
|
|
u = [uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e350'), None]
|
|
|
|
s = self.execute("SELECT %s AS foo", (u,))
|
|
|
|
self.failUnless(u == s)
|
|
|
|
# must survive NULL cast to a uuid[]
|
|
|
|
s = self.execute("SELECT NULL::uuid[] AS foo")
|
|
|
|
self.failUnless(s is None)
|
|
|
|
# what about empty arrays?
|
|
|
|
s = self.execute("SELECT '{}'::uuid[] AS foo")
|
|
|
|
self.failUnless(type(s) == list and len(s) == 0)
|
|
|
|
|
2008-09-24 03:27:52 +04:00
|
|
|
def testINET(self):
|
|
|
|
psycopg2.extras.register_inet()
|
|
|
|
i = "192.168.1.0/24";
|
|
|
|
s = self.execute("SELECT %s AS foo", (i,))
|
|
|
|
self.failUnless(i == s)
|
|
|
|
# must survive NULL cast to inet
|
|
|
|
s = self.execute("SELECT NULL::inet AS foo")
|
|
|
|
self.failUnless(s is None)
|
|
|
|
|
2010-09-26 02:55:55 +04:00
|
|
|
def test_inet_conform(self):
|
|
|
|
from psycopg2.extras import Inet
|
|
|
|
i = Inet("192.168.1.0/24")
|
|
|
|
a = psycopg2.extensions.adapt(i)
|
|
|
|
a.prepare(self.conn)
|
2010-11-19 06:55:37 +03:00
|
|
|
self.assertEqual(
|
2011-02-11 01:59:31 +03:00
|
|
|
filter_scs(self.conn, b("E'192.168.1.0/24'::inet")),
|
2010-11-19 06:55:37 +03:00
|
|
|
a.getquoted())
|
2010-09-26 02:55:55 +04:00
|
|
|
|
|
|
|
# adapts ok with unicode too
|
|
|
|
i = Inet(u"192.168.1.0/24")
|
|
|
|
a = psycopg2.extensions.adapt(i)
|
|
|
|
a.prepare(self.conn)
|
2010-11-19 06:55:37 +03:00
|
|
|
self.assertEqual(
|
2011-02-11 01:59:31 +03:00
|
|
|
filter_scs(self.conn, b("E'192.168.1.0/24'::inet")),
|
2010-11-19 06:55:37 +03:00
|
|
|
a.getquoted())
|
2010-09-26 02:55:55 +04:00
|
|
|
|
2009-11-25 13:51:54 +03:00
|
|
|
def test_adapt_fail(self):
|
|
|
|
class Foo(object): pass
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError,
|
|
|
|
psycopg2.extensions.adapt, Foo(), psycopg2.extensions.ISQLQuote, None)
|
|
|
|
try:
|
|
|
|
psycopg2.extensions.adapt(Foo(), psycopg2.extensions.ISQLQuote, None)
|
|
|
|
except psycopg2.ProgrammingError, err:
|
|
|
|
self.failUnless(str(err) == "can't adapt type 'Foo'")
|
|
|
|
|
2010-09-26 02:13:51 +04:00
|
|
|
|
2010-11-02 02:41:45 +03:00
|
|
|
def skip_if_no_hstore(f):
|
|
|
|
def skip_if_no_hstore_(self):
|
|
|
|
from psycopg2.extras import HstoreAdapter
|
|
|
|
oids = HstoreAdapter.get_oids(self.conn)
|
2011-02-25 04:37:02 +03:00
|
|
|
if oids is None or not oids[0]:
|
2010-11-19 06:55:37 +03:00
|
|
|
return self.skipTest("hstore not available in test database")
|
2010-11-02 02:41:45 +03:00
|
|
|
return f(self)
|
|
|
|
|
|
|
|
return skip_if_no_hstore_
|
|
|
|
|
2010-09-26 02:13:51 +04:00
|
|
|
class HstoreTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2011-02-11 01:59:31 +03:00
|
|
|
self.conn = psycopg2.connect(dsn)
|
2010-09-26 02:13:51 +04:00
|
|
|
|
2010-11-28 19:00:32 +03:00
|
|
|
def tearDown(self):
|
|
|
|
self.conn.close()
|
|
|
|
|
2010-09-26 02:13:51 +04:00
|
|
|
def test_adapt_8(self):
|
|
|
|
if self.conn.server_version >= 90000:
|
2010-11-19 06:55:37 +03:00
|
|
|
return self.skipTest("skipping dict adaptation with PG pre-9 syntax")
|
2010-09-26 02:13:51 +04:00
|
|
|
|
|
|
|
from psycopg2.extras import HstoreAdapter
|
|
|
|
|
2010-11-19 13:29:07 +03:00
|
|
|
o = {'a': '1', 'b': "'", 'c': None}
|
|
|
|
if self.conn.encoding == 'UTF8':
|
|
|
|
o['d'] = u'\xe0'
|
|
|
|
|
2010-09-26 02:13:51 +04:00
|
|
|
a = HstoreAdapter(o)
|
|
|
|
a.prepare(self.conn)
|
|
|
|
q = a.getquoted()
|
|
|
|
|
2011-02-11 01:59:31 +03:00
|
|
|
self.assert_(q.startswith(b("((")), q)
|
|
|
|
ii = q[1:-1].split(b("||"))
|
2010-09-26 02:13:51 +04:00
|
|
|
ii.sort()
|
|
|
|
|
2010-11-19 13:29:07 +03:00
|
|
|
self.assertEqual(len(ii), len(o))
|
2011-02-11 01:59:31 +03:00
|
|
|
self.assertEqual(ii[0], filter_scs(self.conn, b("(E'a' => E'1')")))
|
|
|
|
self.assertEqual(ii[1], filter_scs(self.conn, b("(E'b' => E'''')")))
|
|
|
|
self.assertEqual(ii[2], filter_scs(self.conn, b("(E'c' => NULL)")))
|
2010-11-19 13:29:07 +03:00
|
|
|
if 'd' in o:
|
|
|
|
encc = u'\xe0'.encode(psycopg2.extensions.encodings[self.conn.encoding])
|
2011-02-11 01:59:31 +03:00
|
|
|
self.assertEqual(ii[3], filter_scs(self.conn, b("(E'd' => E'") + encc + b("')")))
|
2010-09-26 02:13:51 +04:00
|
|
|
|
|
|
|
def test_adapt_9(self):
|
|
|
|
if self.conn.server_version < 90000:
|
2010-11-19 06:55:37 +03:00
|
|
|
return self.skipTest("skipping dict adaptation with PG 9 syntax")
|
2010-09-26 02:13:51 +04:00
|
|
|
|
|
|
|
from psycopg2.extras import HstoreAdapter
|
|
|
|
|
2010-11-19 13:29:07 +03:00
|
|
|
o = {'a': '1', 'b': "'", 'c': None}
|
|
|
|
if self.conn.encoding == 'UTF8':
|
|
|
|
o['d'] = u'\xe0'
|
|
|
|
|
2010-09-26 02:13:51 +04:00
|
|
|
a = HstoreAdapter(o)
|
|
|
|
a.prepare(self.conn)
|
|
|
|
q = a.getquoted()
|
|
|
|
|
2011-02-11 01:59:31 +03:00
|
|
|
m = re.match(b(r'hstore\(ARRAY\[([^\]]+)\], ARRAY\[([^\]]+)\]\)'), q)
|
2010-09-26 02:13:51 +04:00
|
|
|
self.assert_(m, repr(q))
|
|
|
|
|
2011-02-11 01:59:31 +03:00
|
|
|
kk = m.group(1).split(b(", "))
|
|
|
|
vv = m.group(2).split(b(", "))
|
2010-09-26 02:13:51 +04:00
|
|
|
ii = zip(kk, vv)
|
|
|
|
ii.sort()
|
|
|
|
|
2011-01-18 03:58:33 +03:00
|
|
|
def f(*args):
|
|
|
|
return tuple([filter_scs(self.conn, s) for s in args])
|
|
|
|
|
2010-11-19 13:29:07 +03:00
|
|
|
self.assertEqual(len(ii), len(o))
|
2011-02-11 01:59:31 +03:00
|
|
|
self.assertEqual(ii[0], f(b("E'a'"), b("E'1'")))
|
|
|
|
self.assertEqual(ii[1], f(b("E'b'"), b("E''''")))
|
|
|
|
self.assertEqual(ii[2], f(b("E'c'"), b("NULL")))
|
2010-11-19 13:29:07 +03:00
|
|
|
if 'd' in o:
|
|
|
|
encc = u'\xe0'.encode(psycopg2.extensions.encodings[self.conn.encoding])
|
2011-02-11 01:59:31 +03:00
|
|
|
self.assertEqual(ii[3], f(b("E'd'"), b("E'") + encc + b("'")))
|
2010-09-26 02:13:51 +04:00
|
|
|
|
2010-09-26 19:57:07 +04:00
|
|
|
def test_parse(self):
|
2010-09-27 00:59:54 +04:00
|
|
|
from psycopg2.extras import HstoreAdapter
|
2010-09-26 19:57:07 +04:00
|
|
|
|
|
|
|
def ok(s, d):
|
2010-09-27 00:59:54 +04:00
|
|
|
self.assertEqual(HstoreAdapter.parse(s, None), d)
|
2010-09-26 19:57:07 +04:00
|
|
|
|
|
|
|
ok(None, None)
|
|
|
|
ok('', {})
|
|
|
|
ok('"a"=>"1", "b"=>"2"', {'a': '1', 'b': '2'})
|
|
|
|
ok('"a" => "1" ,"b" => "2"', {'a': '1', 'b': '2'})
|
|
|
|
ok('"a"=>NULL, "b"=>"2"', {'a': None, 'b': '2'})
|
2010-09-27 01:53:02 +04:00
|
|
|
ok(r'"a"=>"\"", "\""=>"2"', {'a': '"', '"': '2'})
|
2010-09-26 19:57:07 +04:00
|
|
|
ok('"a"=>"\'", "\'"=>"2"', {'a': "'", "'": '2'})
|
|
|
|
ok('"a"=>"1", "b"=>NULL', {'a': '1', 'b': None})
|
|
|
|
ok(r'"a\\"=>"1"', {'a\\': '1'})
|
|
|
|
ok(r'"a\""=>"1"', {'a"': '1'})
|
|
|
|
ok(r'"a\\\""=>"1"', {r'a\"': '1'})
|
|
|
|
ok(r'"a\\\\\""=>"1"', {r'a\\"': '1'})
|
|
|
|
|
|
|
|
def ko(s):
|
2010-09-27 00:59:54 +04:00
|
|
|
self.assertRaises(psycopg2.InterfaceError,
|
|
|
|
HstoreAdapter.parse, s, None)
|
2010-09-26 19:57:07 +04:00
|
|
|
|
|
|
|
ko('a')
|
|
|
|
ko('"a"')
|
|
|
|
ko(r'"a\\""=>"1"')
|
|
|
|
ko(r'"a\\\\""=>"1"')
|
|
|
|
ko('"a=>"1"')
|
|
|
|
ko('"a"=>"1", "b"=>NUL')
|
2010-09-26 02:13:51 +04:00
|
|
|
|
2010-11-02 02:41:45 +03:00
|
|
|
@skip_if_no_hstore
|
2010-09-27 01:11:06 +04:00
|
|
|
def test_register_conn(self):
|
|
|
|
from psycopg2.extras import register_hstore
|
|
|
|
|
|
|
|
register_hstore(self.conn)
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select null::hstore, ''::hstore, 'a => b'::hstore")
|
|
|
|
t = cur.fetchone()
|
|
|
|
self.assert_(t[0] is None)
|
|
|
|
self.assertEqual(t[1], {})
|
|
|
|
self.assertEqual(t[2], {'a': 'b'})
|
|
|
|
|
2010-11-02 02:41:45 +03:00
|
|
|
@skip_if_no_hstore
|
2010-09-27 01:11:06 +04:00
|
|
|
def test_register_curs(self):
|
|
|
|
from psycopg2.extras import register_hstore
|
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
register_hstore(cur)
|
|
|
|
cur.execute("select null::hstore, ''::hstore, 'a => b'::hstore")
|
|
|
|
t = cur.fetchone()
|
|
|
|
self.assert_(t[0] is None)
|
|
|
|
self.assertEqual(t[1], {})
|
|
|
|
self.assertEqual(t[2], {'a': 'b'})
|
|
|
|
|
2010-11-02 02:41:45 +03:00
|
|
|
@skip_if_no_hstore
|
2010-09-27 03:49:31 +04:00
|
|
|
def test_register_unicode(self):
|
|
|
|
from psycopg2.extras import register_hstore
|
|
|
|
|
|
|
|
register_hstore(self.conn, unicode=True)
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select null::hstore, ''::hstore, 'a => b'::hstore")
|
|
|
|
t = cur.fetchone()
|
|
|
|
self.assert_(t[0] is None)
|
|
|
|
self.assertEqual(t[1], {})
|
|
|
|
self.assertEqual(t[2], {u'a': u'b'})
|
|
|
|
self.assert_(isinstance(t[2].keys()[0], unicode))
|
|
|
|
self.assert_(isinstance(t[2].values()[0], unicode))
|
|
|
|
|
2010-11-02 02:41:45 +03:00
|
|
|
@skip_if_no_hstore
|
2010-09-27 04:46:54 +04:00
|
|
|
def test_register_globally(self):
|
|
|
|
from psycopg2.extras import register_hstore, HstoreAdapter
|
|
|
|
|
|
|
|
oids = HstoreAdapter.get_oids(self.conn)
|
|
|
|
try:
|
|
|
|
register_hstore(self.conn, globally=True)
|
2011-02-11 01:59:31 +03:00
|
|
|
conn2 = psycopg2.connect(dsn)
|
2010-11-28 19:00:32 +03:00
|
|
|
try:
|
|
|
|
cur2 = self.conn.cursor()
|
|
|
|
cur2.execute("select 'a => b'::hstore")
|
|
|
|
r = cur2.fetchone()
|
|
|
|
self.assert_(isinstance(r[0], dict))
|
|
|
|
finally:
|
|
|
|
conn2.close()
|
2010-09-27 04:46:54 +04:00
|
|
|
finally:
|
2011-02-25 03:19:49 +03:00
|
|
|
psycopg2.extensions.string_types.pop(oids[0][0])
|
2010-09-27 04:46:54 +04:00
|
|
|
|
|
|
|
# verify the caster is not around anymore
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select 'a => b'::hstore")
|
|
|
|
r = cur.fetchone()
|
|
|
|
self.assert_(isinstance(r[0], str))
|
|
|
|
|
2010-11-02 02:41:45 +03:00
|
|
|
@skip_if_no_hstore
|
2010-09-27 02:03:55 +04:00
|
|
|
def test_roundtrip(self):
|
|
|
|
from psycopg2.extras import register_hstore
|
|
|
|
register_hstore(self.conn)
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
|
|
|
|
def ok(d):
|
|
|
|
cur.execute("select %s", (d,))
|
|
|
|
d1 = cur.fetchone()[0]
|
|
|
|
self.assertEqual(len(d), len(d1))
|
|
|
|
for k in d:
|
|
|
|
self.assert_(k in d1, k)
|
|
|
|
self.assertEqual(d[k], d1[k])
|
|
|
|
|
|
|
|
ok({})
|
|
|
|
ok({'a': 'b', 'c': None})
|
|
|
|
|
|
|
|
ab = map(chr, range(32, 128))
|
|
|
|
ok(dict(zip(ab, ab)))
|
|
|
|
ok({''.join(ab): ''.join(ab)})
|
|
|
|
|
|
|
|
self.conn.set_client_encoding('latin1')
|
2011-02-11 01:59:31 +03:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
ab = map(chr, range(32, 127) + range(160, 255))
|
|
|
|
else:
|
|
|
|
ab = bytes(range(32, 127) + range(160, 255)).decode('latin1')
|
|
|
|
|
2010-09-27 02:03:55 +04:00
|
|
|
ok({''.join(ab): ''.join(ab)})
|
|
|
|
ok(dict(zip(ab, ab)))
|
2010-09-27 01:11:06 +04:00
|
|
|
|
2010-11-02 02:41:45 +03:00
|
|
|
@skip_if_no_hstore
|
2010-09-27 03:49:31 +04:00
|
|
|
def test_roundtrip_unicode(self):
|
|
|
|
from psycopg2.extras import register_hstore
|
|
|
|
register_hstore(self.conn, unicode=True)
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
|
|
|
|
def ok(d):
|
|
|
|
cur.execute("select %s", (d,))
|
|
|
|
d1 = cur.fetchone()[0]
|
|
|
|
self.assertEqual(len(d), len(d1))
|
|
|
|
for k, v in d1.iteritems():
|
|
|
|
self.assert_(k in d, k)
|
|
|
|
self.assertEqual(d[k], v)
|
|
|
|
self.assert_(isinstance(k, unicode))
|
|
|
|
self.assert_(v is None or isinstance(v, unicode))
|
|
|
|
|
|
|
|
ok({})
|
|
|
|
ok({'a': 'b', 'c': None, 'd': u'\u20ac', u'\u2603': 'e'})
|
|
|
|
|
|
|
|
ab = map(unichr, range(1, 1024))
|
|
|
|
ok({u''.join(ab): u''.join(ab)})
|
|
|
|
ok(dict(zip(ab, ab)))
|
|
|
|
|
2011-02-21 03:58:52 +03:00
|
|
|
@skip_if_no_hstore
|
|
|
|
def test_oid(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select 'hstore'::regtype::oid")
|
|
|
|
oid = cur.fetchone()[0]
|
|
|
|
|
|
|
|
# Note: None as conn_or_cursor is just for testing: not public
|
|
|
|
# interface and it may break in future.
|
|
|
|
from psycopg2.extras import register_hstore
|
|
|
|
register_hstore(None, globally=True, oid=oid)
|
|
|
|
try:
|
|
|
|
cur.execute("select null::hstore, ''::hstore, 'a => b'::hstore")
|
|
|
|
t = cur.fetchone()
|
|
|
|
self.assert_(t[0] is None)
|
|
|
|
self.assertEqual(t[1], {})
|
|
|
|
self.assertEqual(t[2], {'a': 'b'})
|
|
|
|
|
|
|
|
finally:
|
|
|
|
psycopg2.extensions.string_types.pop(oid)
|
|
|
|
|
2011-09-22 21:53:21 +04:00
|
|
|
@skip_if_no_hstore
|
|
|
|
@skip_before_postgres(8, 3)
|
|
|
|
def test_roundtrip_array(self):
|
|
|
|
from psycopg2.extras import register_hstore
|
|
|
|
register_hstore(self.conn)
|
|
|
|
|
|
|
|
ds = []
|
|
|
|
ds.append({})
|
|
|
|
ds.append({'a': 'b', 'c': None})
|
|
|
|
|
|
|
|
ab = map(chr, range(32, 128))
|
|
|
|
ds.append(dict(zip(ab, ab)))
|
|
|
|
ds.append({''.join(ab): ''.join(ab)})
|
|
|
|
|
|
|
|
self.conn.set_client_encoding('latin1')
|
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
ab = map(chr, range(32, 127) + range(160, 255))
|
|
|
|
else:
|
|
|
|
ab = bytes(range(32, 127) + range(160, 255)).decode('latin1')
|
|
|
|
|
|
|
|
ds.append({''.join(ab): ''.join(ab)})
|
|
|
|
ds.append(dict(zip(ab, ab)))
|
|
|
|
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select %s", (ds,))
|
|
|
|
ds1 = cur.fetchone()[0]
|
|
|
|
self.assertEqual(ds, ds1)
|
|
|
|
|
|
|
|
@skip_if_no_hstore
|
|
|
|
@skip_before_postgres(8, 3)
|
|
|
|
def test_array_cast(self):
|
|
|
|
from psycopg2.extras import register_hstore
|
|
|
|
register_hstore(self.conn)
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select array['a=>1'::hstore, 'b=>2'::hstore];")
|
|
|
|
a = cur.fetchone()[0]
|
|
|
|
self.assertEqual(a, [{'a': '1'}, {'b': '2'}])
|
|
|
|
|
|
|
|
@skip_if_no_hstore
|
|
|
|
def test_array_cast_oid(self):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select 'hstore'::regtype::oid, 'hstore[]'::regtype::oid")
|
|
|
|
oid, aoid = cur.fetchone()
|
|
|
|
|
|
|
|
from psycopg2.extras import register_hstore
|
|
|
|
register_hstore(None, globally=True, oid=oid, array_oid=aoid)
|
|
|
|
try:
|
|
|
|
cur.execute("select null::hstore, ''::hstore, 'a => b'::hstore, '{a=>b}'::hstore[]")
|
|
|
|
t = cur.fetchone()
|
|
|
|
self.assert_(t[0] is None)
|
|
|
|
self.assertEqual(t[1], {})
|
|
|
|
self.assertEqual(t[2], {'a': 'b'})
|
|
|
|
self.assertEqual(t[3], [{'a': 'b'}])
|
|
|
|
|
|
|
|
finally:
|
|
|
|
psycopg2.extensions.string_types.pop(oid)
|
|
|
|
psycopg2.extensions.string_types.pop(aoid)
|
2010-09-27 03:49:31 +04:00
|
|
|
|
2011-01-18 04:54:33 +03:00
|
|
|
def skip_if_no_composite(f):
|
|
|
|
def skip_if_no_composite_(self):
|
|
|
|
if self.conn.server_version < 80000:
|
|
|
|
return self.skipTest(
|
|
|
|
"server version %s doesn't support composite types"
|
|
|
|
% self.conn.server_version)
|
|
|
|
|
|
|
|
return f(self)
|
|
|
|
|
|
|
|
skip_if_no_composite_.__name__ = f.__name__
|
|
|
|
return skip_if_no_composite_
|
|
|
|
|
2010-12-31 20:40:28 +03:00
|
|
|
class AdaptTypeTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2011-02-11 01:59:31 +03:00
|
|
|
self.conn = psycopg2.connect(dsn)
|
2010-12-31 20:40:28 +03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.conn.close()
|
|
|
|
|
2011-01-18 04:54:33 +03:00
|
|
|
@skip_if_no_composite
|
2010-12-31 20:40:28 +03:00
|
|
|
def test_none_in_record(self):
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
s = curs.mogrify("SELECT %s;", [(42, None)])
|
2011-02-11 01:59:31 +03:00
|
|
|
self.assertEqual(b("SELECT (42, NULL);"), s)
|
2010-12-31 20:40:28 +03:00
|
|
|
curs.execute("SELECT %s;", [(42, None)])
|
|
|
|
d = curs.fetchone()[0]
|
|
|
|
self.assertEqual("(42,)", d)
|
|
|
|
|
2010-12-31 21:21:44 +03:00
|
|
|
def test_none_fast_path(self):
|
|
|
|
# the None adapter is not actually invoked in regular adaptation
|
|
|
|
ext = psycopg2.extensions
|
|
|
|
|
|
|
|
class WonkyAdapter(object):
|
|
|
|
def __init__(self, obj): pass
|
|
|
|
def getquoted(self): return "NOPE!"
|
|
|
|
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
|
|
|
|
orig_adapter = ext.adapters[type(None), ext.ISQLQuote]
|
|
|
|
try:
|
|
|
|
ext.register_adapter(type(None), WonkyAdapter)
|
|
|
|
self.assertEqual(ext.adapt(None).getquoted(), "NOPE!")
|
|
|
|
|
|
|
|
s = curs.mogrify("SELECT %s;", (None,))
|
2011-02-11 01:59:31 +03:00
|
|
|
self.assertEqual(b("SELECT NULL;"), s)
|
2010-12-31 21:21:44 +03:00
|
|
|
|
|
|
|
finally:
|
|
|
|
ext.register_adapter(type(None), orig_adapter)
|
2010-12-31 20:40:28 +03:00
|
|
|
|
2011-01-02 02:34:13 +03:00
|
|
|
def test_tokenization(self):
|
|
|
|
from psycopg2.extras import CompositeCaster
|
|
|
|
def ok(s, v):
|
|
|
|
self.assertEqual(CompositeCaster.tokenize(s), v)
|
|
|
|
|
|
|
|
ok("(,)", [None, None])
|
|
|
|
ok('(hello,,10.234,2010-11-11)', ['hello', None, '10.234', '2010-11-11'])
|
|
|
|
ok('(10,"""")', ['10', '"'])
|
|
|
|
ok('(10,",")', ['10', ','])
|
|
|
|
ok(r'(10,"\\")', ['10', '\\'])
|
|
|
|
ok(r'''(10,"\\',""")''', ['10', '''\\',"'''])
|
|
|
|
ok('(10,"(20,""(30,40)"")")', ['10', '(20,"(30,40)")'])
|
|
|
|
ok('(10,"(20,""(30,""""(40,50)"""")"")")', ['10', '(20,"(30,""(40,50)"")")'])
|
|
|
|
ok('(,"(,""(a\nb\tc)"")")', [None, '(,"(a\nb\tc)")'])
|
|
|
|
ok('(\x01,\x02,\x03,\x04,\x05,\x06,\x07,\x08,"\t","\n","\x0b",'
|
|
|
|
'"\x0c","\r",\x0e,\x0f,\x10,\x11,\x12,\x13,\x14,\x15,\x16,'
|
|
|
|
'\x17,\x18,\x19,\x1a,\x1b,\x1c,\x1d,\x1e,\x1f," ",!,"""",#,'
|
|
|
|
'$,%,&,\',"(",")",*,+,",",-,.,/,0,1,2,3,4,5,6,7,8,9,:,;,<,=,>,?,'
|
|
|
|
'@,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,[,"\\\\",],'
|
|
|
|
'^,_,`,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,{,|,},'
|
|
|
|
'~,\x7f)',
|
|
|
|
map(chr, range(1, 128)))
|
|
|
|
ok('(,"\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
|
|
|
|
'\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !'
|
|
|
|
'""#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]'
|
|
|
|
'^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f")',
|
|
|
|
[None, ''.join(map(chr, range(1, 128)))])
|
|
|
|
|
2011-01-18 04:54:33 +03:00
|
|
|
@skip_if_no_composite
|
2011-01-02 02:34:13 +03:00
|
|
|
def test_cast_composite(self):
|
|
|
|
oid = self._create_type("type_isd",
|
|
|
|
[('anint', 'integer'), ('astring', 'text'), ('adate', 'date')])
|
|
|
|
|
|
|
|
t = psycopg2.extras.register_composite("type_isd", self.conn)
|
|
|
|
self.assertEqual(t.name, 'type_isd')
|
|
|
|
self.assertEqual(t.oid, oid)
|
|
|
|
self.assert_(issubclass(t.type, tuple))
|
|
|
|
self.assertEqual(t.attnames, ['anint', 'astring', 'adate'])
|
|
|
|
self.assertEqual(t.atttypes, [23,25,1082])
|
|
|
|
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
r = (10, 'hello', date(2011,1,2))
|
|
|
|
curs.execute("select %s::type_isd;", (r,))
|
|
|
|
v = curs.fetchone()[0]
|
|
|
|
self.assert_(isinstance(v, t.type))
|
|
|
|
self.assertEqual(v[0], 10)
|
|
|
|
self.assertEqual(v[1], "hello")
|
|
|
|
self.assertEqual(v[2], date(2011,1,2))
|
|
|
|
|
|
|
|
try:
|
|
|
|
from collections import namedtuple
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.assert_(t.type is not tuple)
|
|
|
|
self.assertEqual(v.anint, 10)
|
|
|
|
self.assertEqual(v.astring, "hello")
|
|
|
|
self.assertEqual(v.adate, date(2011,1,2))
|
|
|
|
|
2011-01-18 04:54:33 +03:00
|
|
|
@skip_if_no_composite
|
2011-01-02 02:34:13 +03:00
|
|
|
def test_cast_nested(self):
|
|
|
|
self._create_type("type_is",
|
|
|
|
[("anint", "integer"), ("astring", "text")])
|
|
|
|
self._create_type("type_r_dt",
|
|
|
|
[("adate", "date"), ("apair", "type_is")])
|
|
|
|
self._create_type("type_r_ft",
|
|
|
|
[("afloat", "float8"), ("anotherpair", "type_r_dt")])
|
|
|
|
|
|
|
|
psycopg2.extras.register_composite("type_is", self.conn)
|
|
|
|
psycopg2.extras.register_composite("type_r_dt", self.conn)
|
|
|
|
psycopg2.extras.register_composite("type_r_ft", self.conn)
|
|
|
|
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
r = (0.25, (date(2011,1,2), (42, "hello")))
|
|
|
|
curs.execute("select %s::type_r_ft;", (r,))
|
|
|
|
v = curs.fetchone()[0]
|
|
|
|
|
|
|
|
self.assertEqual(r, v)
|
|
|
|
|
|
|
|
try:
|
|
|
|
from collections import namedtuple
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.assertEqual(v.anotherpair.apair.astring, "hello")
|
|
|
|
|
2011-01-18 04:54:33 +03:00
|
|
|
@skip_if_no_composite
|
2011-01-02 02:34:13 +03:00
|
|
|
def test_register_on_cursor(self):
|
|
|
|
self._create_type("type_ii", [("a", "integer"), ("b", "integer")])
|
|
|
|
|
|
|
|
curs1 = self.conn.cursor()
|
|
|
|
curs2 = self.conn.cursor()
|
|
|
|
psycopg2.extras.register_composite("type_ii", curs1)
|
|
|
|
curs1.execute("select (1,2)::type_ii")
|
|
|
|
self.assertEqual(curs1.fetchone()[0], (1,2))
|
|
|
|
curs2.execute("select (1,2)::type_ii")
|
|
|
|
self.assertEqual(curs2.fetchone()[0], "(1,2)")
|
|
|
|
|
2011-01-18 04:54:33 +03:00
|
|
|
@skip_if_no_composite
|
2011-01-02 02:34:13 +03:00
|
|
|
def test_register_on_connection(self):
|
|
|
|
self._create_type("type_ii", [("a", "integer"), ("b", "integer")])
|
|
|
|
|
2011-02-11 01:59:31 +03:00
|
|
|
conn1 = psycopg2.connect(dsn)
|
|
|
|
conn2 = psycopg2.connect(dsn)
|
2011-01-02 02:34:13 +03:00
|
|
|
try:
|
|
|
|
psycopg2.extras.register_composite("type_ii", conn1)
|
|
|
|
curs1 = conn1.cursor()
|
|
|
|
curs2 = conn2.cursor()
|
|
|
|
curs1.execute("select (1,2)::type_ii")
|
|
|
|
self.assertEqual(curs1.fetchone()[0], (1,2))
|
|
|
|
curs2.execute("select (1,2)::type_ii")
|
|
|
|
self.assertEqual(curs2.fetchone()[0], "(1,2)")
|
|
|
|
finally:
|
|
|
|
conn1.close()
|
|
|
|
conn2.close()
|
|
|
|
|
2011-01-18 04:54:33 +03:00
|
|
|
@skip_if_no_composite
|
2011-01-02 02:34:13 +03:00
|
|
|
def test_register_globally(self):
|
|
|
|
self._create_type("type_ii", [("a", "integer"), ("b", "integer")])
|
|
|
|
|
2011-02-11 01:59:31 +03:00
|
|
|
conn1 = psycopg2.connect(dsn)
|
|
|
|
conn2 = psycopg2.connect(dsn)
|
2011-01-02 02:34:13 +03:00
|
|
|
try:
|
|
|
|
t = psycopg2.extras.register_composite("type_ii", conn1, globally=True)
|
|
|
|
try:
|
|
|
|
curs1 = conn1.cursor()
|
|
|
|
curs2 = conn2.cursor()
|
|
|
|
curs1.execute("select (1,2)::type_ii")
|
|
|
|
self.assertEqual(curs1.fetchone()[0], (1,2))
|
|
|
|
curs2.execute("select (1,2)::type_ii")
|
|
|
|
self.assertEqual(curs2.fetchone()[0], (1,2))
|
|
|
|
finally:
|
|
|
|
del psycopg2.extensions.string_types[t.oid]
|
|
|
|
|
|
|
|
finally:
|
|
|
|
conn1.close()
|
|
|
|
conn2.close()
|
|
|
|
|
2011-02-09 04:20:25 +03:00
|
|
|
@skip_if_no_composite
|
|
|
|
def test_composite_namespace(self):
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
curs.execute("""
|
|
|
|
select nspname from pg_namespace
|
|
|
|
where nspname = 'typens';
|
|
|
|
""")
|
|
|
|
if not curs.fetchone():
|
|
|
|
curs.execute("create schema typens;")
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
self._create_type("typens.typens_ii",
|
|
|
|
[("a", "integer"), ("b", "integer")])
|
|
|
|
t = psycopg2.extras.register_composite(
|
|
|
|
"typens.typens_ii", self.conn)
|
|
|
|
curs.execute("select (4,8)::typens.typens_ii")
|
|
|
|
self.assertEqual(curs.fetchone()[0], (4,8))
|
|
|
|
|
2011-09-22 21:55:33 +04:00
|
|
|
@skip_if_no_composite
|
|
|
|
@skip_before_postgres(8, 3)
|
|
|
|
def test_composite_array(self):
|
|
|
|
oid = self._create_type("type_isd",
|
|
|
|
[('anint', 'integer'), ('astring', 'text'), ('adate', 'date')])
|
|
|
|
|
|
|
|
t = psycopg2.extras.register_composite("type_isd", self.conn)
|
|
|
|
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
r1 = (10, 'hello', date(2011,1,2))
|
|
|
|
r2 = (20, 'world', date(2011,1,3))
|
|
|
|
curs.execute("select %s::type_isd[];", ([r1, r2],))
|
|
|
|
v = curs.fetchone()[0]
|
|
|
|
self.assertEqual(len(v), 2)
|
|
|
|
self.assert_(isinstance(v[0], t.type))
|
|
|
|
self.assertEqual(v[0][0], 10)
|
|
|
|
self.assertEqual(v[0][1], "hello")
|
|
|
|
self.assertEqual(v[0][2], date(2011,1,2))
|
|
|
|
self.assert_(isinstance(v[1], t.type))
|
|
|
|
self.assertEqual(v[1][0], 20)
|
|
|
|
self.assertEqual(v[1][1], "world")
|
|
|
|
self.assertEqual(v[1][2], date(2011,1,3))
|
|
|
|
|
2011-01-02 02:34:13 +03:00
|
|
|
def _create_type(self, name, fields):
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
try:
|
|
|
|
curs.execute("drop type %s cascade;" % name)
|
|
|
|
except psycopg2.ProgrammingError:
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
curs.execute("create type %s as (%s);" % (name,
|
|
|
|
", ".join(["%s %s" % p for p in fields])))
|
2011-02-09 04:20:25 +03:00
|
|
|
if '.' in name:
|
|
|
|
schema, name = name.split('.')
|
|
|
|
else:
|
|
|
|
schema = 'public'
|
|
|
|
|
2011-01-02 02:34:13 +03:00
|
|
|
curs.execute("""\
|
|
|
|
SELECT t.oid
|
|
|
|
FROM pg_type t JOIN pg_namespace ns ON typnamespace = ns.oid
|
2011-02-09 04:20:25 +03:00
|
|
|
WHERE typname = %s and nspname = %s;
|
|
|
|
""", (name, schema))
|
2011-01-02 02:34:13 +03:00
|
|
|
oid = curs.fetchone()[0]
|
|
|
|
self.conn.commit()
|
|
|
|
return oid
|
|
|
|
|
|
|
|
|
2008-09-19 23:25:16 +04:00
|
|
|
def test_suite():
|
|
|
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|
|
|
|
|