2007-11-11 13:40:12 +03:00
|
|
|
#!/usr/bin/env python
|
2010-02-13 01:34:53 +03:00
|
|
|
#
|
2004-12-20 05:33:12 +03:00
|
|
|
# types_basic.py - tests for basic types conversions
|
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# Copyright (C) 2004-2010 Federico Di Gregorio <fog@debian.org>
|
|
|
|
#
|
|
|
|
# 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.
|
2004-12-20 05:33:12 +03:00
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# You must obey the GNU Lesser General Public License in all respects for
|
|
|
|
# all of the code used other than OpenSSL.
|
2004-12-20 05:33:12 +03: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.
|
2004-12-20 05:33:12 +03:00
|
|
|
|
2005-07-17 08:26:27 +04:00
|
|
|
try:
|
|
|
|
import decimal
|
|
|
|
except:
|
|
|
|
pass
|
2008-01-16 21:08:12 +03:00
|
|
|
import sys
|
2010-12-24 17:44:29 +03:00
|
|
|
import testutils
|
2011-03-26 17:27:58 +03:00
|
|
|
from testutils import unittest, decorate_all_tests
|
2010-12-29 05:47:29 +03:00
|
|
|
from testconfig import dsn
|
2004-12-20 05:33:12 +03:00
|
|
|
|
2008-01-16 21:08:12 +03:00
|
|
|
import psycopg2
|
2010-12-29 05:47:29 +03:00
|
|
|
from psycopg2.extensions import b
|
2004-12-20 05:33:12 +03:00
|
|
|
|
2007-11-11 13:40:12 +03:00
|
|
|
|
|
|
|
class TypesBasicTests(unittest.TestCase):
|
2008-09-19 23:25:16 +04:00
|
|
|
"""Test that all type conversions are working."""
|
2004-12-20 05:33:12 +03:00
|
|
|
|
|
|
|
def setUp(self):
|
2010-12-21 07:58:38 +03:00
|
|
|
self.conn = psycopg2.connect(dsn)
|
2004-12-20 05:33:12 +03:00
|
|
|
|
2010-11-28 19:00:32 +03:00
|
|
|
def tearDown(self):
|
|
|
|
self.conn.close()
|
|
|
|
|
2004-12-20 05:33:12 +03:00
|
|
|
def execute(self, *args):
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
curs.execute(*args)
|
|
|
|
return curs.fetchone()[0]
|
2007-11-11 06:58:45 +03:00
|
|
|
|
2004-12-20 05:33:12 +03:00
|
|
|
def testQuoting(self):
|
|
|
|
s = "Quote'this\\! ''ok?''"
|
|
|
|
self.failUnless(self.execute("SELECT %s AS foo", (s,)) == s,
|
|
|
|
"wrong quoting: " + s)
|
|
|
|
|
|
|
|
def testUnicode(self):
|
|
|
|
s = u"Quote'this\\! ''ok?''"
|
|
|
|
self.failUnless(self.execute("SELECT %s AS foo", (s,)) == s,
|
|
|
|
"wrong unicode quoting: " + s)
|
|
|
|
|
|
|
|
def testNumber(self):
|
|
|
|
s = self.execute("SELECT %s AS foo", (1971,))
|
|
|
|
self.failUnless(s == 1971, "wrong integer quoting: " + str(s))
|
2005-03-12 09:39:47 +03:00
|
|
|
s = self.execute("SELECT %s AS foo", (1971L,))
|
|
|
|
self.failUnless(s == 1971L, "wrong integer quoting: " + str(s))
|
2010-12-24 17:44:29 +03:00
|
|
|
if sys.version_info[0:2] < (2, 4):
|
2010-02-21 03:04:00 +03:00
|
|
|
s = self.execute("SELECT %s AS foo", (19.10,))
|
|
|
|
self.failUnless(abs(s - 19.10) < 0.001,
|
|
|
|
"wrong float quoting: " + str(s))
|
|
|
|
|
2010-12-24 17:54:01 +03:00
|
|
|
def testBoolean(self):
|
|
|
|
x = self.execute("SELECT %s as foo", (False,))
|
|
|
|
self.assert_(x is False)
|
|
|
|
x = self.execute("SELECT %s as foo", (True,))
|
|
|
|
self.assert_(x is True)
|
|
|
|
|
2010-02-21 03:04:00 +03:00
|
|
|
def testDecimal(self):
|
2010-12-24 17:44:29 +03:00
|
|
|
if sys.version_info[0:2] >= (2, 4):
|
2009-11-25 12:38:31 +03:00
|
|
|
s = self.execute("SELECT %s AS foo", (decimal.Decimal("19.10"),))
|
2005-07-17 08:26:27 +04:00
|
|
|
self.failUnless(s - decimal.Decimal("19.10") == 0,
|
|
|
|
"wrong decimal quoting: " + str(s))
|
2010-02-21 03:04:00 +03:00
|
|
|
s = self.execute("SELECT %s AS foo", (decimal.Decimal("NaN"),))
|
|
|
|
self.failUnless(str(s) == "NaN", "wrong decimal quoting: " + str(s))
|
|
|
|
self.failUnless(type(s) == decimal.Decimal, "wrong decimal conversion: " + repr(s))
|
|
|
|
s = self.execute("SELECT %s AS foo", (decimal.Decimal("infinity"),))
|
|
|
|
self.failUnless(str(s) == "NaN", "wrong decimal quoting: " + str(s))
|
|
|
|
self.failUnless(type(s) == decimal.Decimal, "wrong decimal conversion: " + repr(s))
|
|
|
|
s = self.execute("SELECT %s AS foo", (decimal.Decimal("-infinity"),))
|
|
|
|
self.failUnless(str(s) == "NaN", "wrong decimal quoting: " + str(s))
|
|
|
|
self.failUnless(type(s) == decimal.Decimal, "wrong decimal conversion: " + repr(s))
|
2010-11-19 06:55:37 +03:00
|
|
|
else:
|
|
|
|
return self.skipTest("decimal not available")
|
2004-12-20 05:33:12 +03:00
|
|
|
|
2010-11-19 13:44:39 +03:00
|
|
|
def testFloatNan(self):
|
2010-11-11 04:39:46 +03:00
|
|
|
try:
|
|
|
|
float("nan")
|
|
|
|
except ValueError:
|
2010-11-19 06:55:37 +03:00
|
|
|
return self.skipTest("nan not available on this platform")
|
2010-11-11 04:39:46 +03:00
|
|
|
|
2009-01-23 02:09:20 +03:00
|
|
|
s = self.execute("SELECT %s AS foo", (float("nan"),))
|
|
|
|
self.failUnless(str(s) == "nan", "wrong float quoting: " + str(s))
|
|
|
|
self.failUnless(type(s) == float, "wrong float conversion: " + repr(s))
|
2010-11-19 13:44:39 +03:00
|
|
|
|
|
|
|
def testFloatInf(self):
|
|
|
|
try:
|
|
|
|
self.execute("select 'inf'::float")
|
|
|
|
except psycopg2.DataError:
|
|
|
|
return self.skipTest("inf::float not available on the server")
|
2010-11-24 13:50:28 +03:00
|
|
|
except ValueError:
|
|
|
|
return self.skipTest("inf not available on this platform")
|
2009-01-23 02:09:20 +03:00
|
|
|
s = self.execute("SELECT %s AS foo", (float("inf"),))
|
|
|
|
self.failUnless(str(s) == "inf", "wrong float quoting: " + str(s))
|
|
|
|
self.failUnless(type(s) == float, "wrong float conversion: " + repr(s))
|
|
|
|
|
2011-02-12 22:56:49 +03:00
|
|
|
s = self.execute("SELECT %s AS foo", (float("-inf"),))
|
|
|
|
self.failUnless(str(s) == "-inf", "wrong float quoting: " + str(s))
|
|
|
|
|
2004-12-20 05:33:12 +03:00
|
|
|
def testBinary(self):
|
2010-12-24 17:44:29 +03:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
s = ''.join([chr(x) for x in range(256)])
|
|
|
|
b = psycopg2.Binary(s)
|
|
|
|
buf = self.execute("SELECT %s::bytea AS foo", (b,))
|
|
|
|
self.assertEqual(s, str(buf))
|
|
|
|
else:
|
|
|
|
s = bytes(range(256))
|
|
|
|
b = psycopg2.Binary(s)
|
|
|
|
buf = self.execute("SELECT %s::bytea AS foo", (b,))
|
|
|
|
self.assertEqual(s, buf)
|
2008-01-16 21:08:12 +03:00
|
|
|
|
2011-02-18 17:19:57 +03:00
|
|
|
def testBinaryNone(self):
|
|
|
|
b = psycopg2.Binary(None)
|
|
|
|
buf = self.execute("SELECT %s::bytea AS foo", (b,))
|
|
|
|
self.assertEqual(buf, None)
|
|
|
|
|
2008-01-16 21:08:12 +03:00
|
|
|
def testBinaryEmptyString(self):
|
2006-09-01 16:36:38 +04:00
|
|
|
# test to make sure an empty Binary is converted to an empty string
|
2010-12-24 17:44:29 +03:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
b = psycopg2.Binary('')
|
|
|
|
self.assertEqual(str(b), "''::bytea")
|
|
|
|
else:
|
|
|
|
b = psycopg2.Binary(bytes([]))
|
|
|
|
self.assertEqual(str(b), "''::bytea")
|
2008-01-16 21:08:12 +03:00
|
|
|
|
|
|
|
def testBinaryRoundTrip(self):
|
2008-01-11 01:19:23 +03:00
|
|
|
# test to make sure buffers returned by psycopg2 are
|
|
|
|
# understood by execute:
|
2010-12-24 17:44:29 +03:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
s = ''.join([chr(x) for x in range(256)])
|
|
|
|
buf = self.execute("SELECT %s::bytea AS foo", (psycopg2.Binary(s),))
|
|
|
|
buf2 = self.execute("SELECT %s::bytea AS foo", (buf,))
|
|
|
|
self.assertEqual(s, str(buf2))
|
|
|
|
else:
|
|
|
|
s = bytes(range(256))
|
|
|
|
buf = self.execute("SELECT %s::bytea AS foo", (psycopg2.Binary(s),))
|
|
|
|
buf2 = self.execute("SELECT %s::bytea AS foo", (buf,))
|
|
|
|
self.assertEqual(s, buf2)
|
2006-09-01 12:23:51 +04:00
|
|
|
|
2005-06-02 08:30:31 +04:00
|
|
|
def testArray(self):
|
2007-11-11 06:58:45 +03:00
|
|
|
s = self.execute("SELECT %s AS foo", ([[1,2],[3,4]],))
|
2010-12-23 05:38:37 +03:00
|
|
|
self.failUnlessEqual(s, [[1,2],[3,4]])
|
2007-11-11 06:58:45 +03:00
|
|
|
s = self.execute("SELECT %s AS foo", (['one', 'two', 'three'],))
|
2010-12-23 05:38:37 +03:00
|
|
|
self.failUnlessEqual(s, ['one', 'two', 'three'])
|
2007-11-11 06:58:45 +03:00
|
|
|
|
2011-02-15 13:27:47 +03:00
|
|
|
def testEmptyArrayRegression(self):
|
|
|
|
# ticket #42
|
|
|
|
import datetime
|
|
|
|
curs = self.conn.cursor()
|
|
|
|
curs.execute("create table array_test (id integer, col timestamp without time zone[])")
|
|
|
|
|
|
|
|
curs.execute("insert into array_test values (%s, %s)", (1, [datetime.date(2011,2,14)]))
|
|
|
|
curs.execute("select col from array_test where id = 1")
|
|
|
|
self.assertEqual(curs.fetchone()[0], [datetime.datetime(2011, 2, 14, 0, 0)])
|
|
|
|
|
|
|
|
curs.execute("insert into array_test values (%s, %s)", (2, []))
|
|
|
|
curs.execute("select col from array_test where id = 2")
|
|
|
|
self.assertEqual(curs.fetchone()[0], [])
|
|
|
|
|
2011-02-20 04:34:54 +03:00
|
|
|
def testEmptyArray(self):
|
|
|
|
s = self.execute("SELECT '{}' AS foo")
|
|
|
|
self.failUnlessEqual(s, [])
|
|
|
|
s = self.execute("SELECT '{}'::text[] AS foo")
|
|
|
|
self.failUnlessEqual(s, [])
|
|
|
|
s = self.execute("SELECT %s AS foo", ([],))
|
|
|
|
self.failUnlessEqual(s, [])
|
|
|
|
s = self.execute("SELECT 1 != ALL(%s)", ([],))
|
|
|
|
self.failUnlessEqual(s, True)
|
2011-02-20 15:23:48 +03:00
|
|
|
# but don't break the strings :)
|
|
|
|
s = self.execute("SELECT '{}'::text AS foo")
|
|
|
|
self.failUnlessEqual(s, "{}")
|
2011-02-20 04:34:54 +03:00
|
|
|
|
2011-02-15 20:11:07 +03:00
|
|
|
@testutils.skip_from_python(3)
|
2010-12-24 17:44:29 +03:00
|
|
|
def testTypeRoundtripBuffer(self):
|
2010-05-04 02:07:48 +04:00
|
|
|
o1 = buffer("".join(map(chr, range(256))))
|
|
|
|
o2 = self.execute("select %s;", (o1,))
|
|
|
|
self.assertEqual(type(o1), type(o2))
|
|
|
|
|
|
|
|
# Test with an empty buffer
|
|
|
|
o1 = buffer("")
|
|
|
|
o2 = self.execute("select %s;", (o1,))
|
|
|
|
self.assertEqual(type(o1), type(o2))
|
2011-02-23 17:04:27 +03:00
|
|
|
self.assertEqual(str(o1), str(o2))
|
2010-05-04 02:07:48 +04:00
|
|
|
|
2011-02-15 20:11:07 +03:00
|
|
|
@testutils.skip_from_python(3)
|
2010-12-24 17:44:29 +03:00
|
|
|
def testTypeRoundtripBufferArray(self):
|
2010-05-04 02:07:48 +04:00
|
|
|
o1 = buffer("".join(map(chr, range(256))))
|
|
|
|
o1 = [o1]
|
|
|
|
o2 = self.execute("select %s;", (o1,))
|
|
|
|
self.assertEqual(type(o1[0]), type(o2[0]))
|
2011-02-23 17:04:27 +03:00
|
|
|
self.assertEqual(str(o1[0]), str(o2[0]))
|
2007-11-11 06:58:45 +03:00
|
|
|
|
2011-02-15 20:11:07 +03:00
|
|
|
@testutils.skip_before_python(3)
|
2010-12-24 17:44:29 +03:00
|
|
|
def testTypeRoundtripBytes(self):
|
|
|
|
o1 = bytes(range(256))
|
|
|
|
o2 = self.execute("select %s;", (o1,))
|
|
|
|
self.assertEqual(memoryview, type(o2))
|
|
|
|
|
|
|
|
# Test with an empty buffer
|
|
|
|
o1 = bytes([])
|
|
|
|
o2 = self.execute("select %s;", (o1,))
|
|
|
|
self.assertEqual(memoryview, type(o2))
|
|
|
|
|
2011-02-15 20:11:07 +03:00
|
|
|
@testutils.skip_before_python(3)
|
2010-12-24 17:44:29 +03:00
|
|
|
def testTypeRoundtripBytesArray(self):
|
|
|
|
o1 = bytes(range(256))
|
|
|
|
o1 = [o1]
|
|
|
|
o2 = self.execute("select %s;", (o1,))
|
|
|
|
self.assertEqual(memoryview, type(o2[0]))
|
|
|
|
|
2011-02-15 20:30:43 +03:00
|
|
|
@testutils.skip_before_python(2, 6)
|
2010-12-24 17:44:29 +03:00
|
|
|
def testAdaptBytearray(self):
|
|
|
|
o1 = bytearray(range(256))
|
|
|
|
o2 = self.execute("select %s;", (o1,))
|
2011-02-23 17:04:27 +03:00
|
|
|
|
2011-02-15 20:30:43 +03:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
self.assertEqual(buffer, type(o2))
|
|
|
|
else:
|
|
|
|
self.assertEqual(memoryview, type(o2))
|
2010-12-24 17:44:29 +03:00
|
|
|
|
2011-02-23 17:04:27 +03:00
|
|
|
self.assertEqual(len(o1), len(o2))
|
|
|
|
for c1, c2 in zip(o1, o2):
|
|
|
|
self.assertEqual(c1, ord(c2))
|
|
|
|
|
2010-12-24 17:44:29 +03:00
|
|
|
# Test with an empty buffer
|
|
|
|
o1 = bytearray([])
|
|
|
|
o2 = self.execute("select %s;", (o1,))
|
2011-02-23 17:04:27 +03:00
|
|
|
|
|
|
|
self.assertEqual(len(o2), 0)
|
2011-02-15 20:30:43 +03:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
self.assertEqual(buffer, type(o2))
|
|
|
|
else:
|
|
|
|
self.assertEqual(memoryview, type(o2))
|
2010-12-24 17:44:29 +03:00
|
|
|
|
2011-02-15 20:30:43 +03:00
|
|
|
@testutils.skip_before_python(2, 7)
|
2010-12-24 17:44:29 +03:00
|
|
|
def testAdaptMemoryview(self):
|
2011-02-15 20:30:43 +03:00
|
|
|
o1 = memoryview(bytearray(range(256)))
|
2010-12-24 17:44:29 +03:00
|
|
|
o2 = self.execute("select %s;", (o1,))
|
2011-02-15 20:30:43 +03:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
self.assertEqual(buffer, type(o2))
|
|
|
|
else:
|
|
|
|
self.assertEqual(memoryview, type(o2))
|
2010-12-24 17:44:29 +03:00
|
|
|
|
|
|
|
# Test with an empty buffer
|
2011-02-15 20:30:43 +03:00
|
|
|
o1 = memoryview(bytearray([]))
|
2010-12-24 17:44:29 +03:00
|
|
|
o2 = self.execute("select %s;", (o1,))
|
2011-02-15 20:30:43 +03:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
self.assertEqual(buffer, type(o2))
|
|
|
|
else:
|
|
|
|
self.assertEqual(memoryview, type(o2))
|
2010-12-24 17:44:29 +03:00
|
|
|
|
2011-02-23 17:04:27 +03:00
|
|
|
def testByteaHexCheckFalsePositive(self):
|
|
|
|
# the check \x -> x to detect bad bytea decode
|
|
|
|
# may be fooled if the first char is really an 'x'
|
|
|
|
o1 = psycopg2.Binary(b('x'))
|
|
|
|
o2 = self.execute("SELECT %s::bytea AS foo", (o1,))
|
|
|
|
self.assertEqual(b('x'), o2[0])
|
|
|
|
|
2011-05-31 01:00:20 +04:00
|
|
|
def testNegNumber(self):
|
|
|
|
d1 = self.execute("select -%s;", (decimal.Decimal('-1.0'),))
|
|
|
|
self.assertEqual(1, d1)
|
|
|
|
f1 = self.execute("select -%s;", (-1.0,))
|
|
|
|
self.assertEqual(1, f1)
|
|
|
|
i1 = self.execute("select -%s;", (-1,))
|
|
|
|
self.assertEqual(1, i1)
|
|
|
|
l1 = self.execute("select -%s;", (-1L,))
|
|
|
|
self.assertEqual(1, l1)
|
|
|
|
|
2010-11-08 04:35:06 +03:00
|
|
|
|
|
|
|
class AdaptSubclassTest(unittest.TestCase):
|
|
|
|
def test_adapt_subtype(self):
|
|
|
|
from psycopg2.extensions import adapt
|
|
|
|
class Sub(str): pass
|
|
|
|
s1 = "hel'lo"
|
|
|
|
s2 = Sub(s1)
|
|
|
|
self.assertEqual(adapt(s1).getquoted(), adapt(s2).getquoted())
|
|
|
|
|
|
|
|
def test_adapt_most_specific(self):
|
|
|
|
from psycopg2.extensions import adapt, register_adapter, AsIs
|
|
|
|
|
|
|
|
class A(object): pass
|
|
|
|
class B(A): pass
|
|
|
|
class C(B): pass
|
|
|
|
|
|
|
|
register_adapter(A, lambda a: AsIs("a"))
|
|
|
|
register_adapter(B, lambda b: AsIs("b"))
|
2011-01-02 19:09:30 +03:00
|
|
|
try:
|
2011-01-03 23:43:02 +03:00
|
|
|
self.assertEqual(b('b'), adapt(C()).getquoted())
|
2011-01-02 19:09:30 +03:00
|
|
|
finally:
|
|
|
|
del psycopg2.extensions.adapters[A, psycopg2.extensions.ISQLQuote]
|
|
|
|
del psycopg2.extensions.adapters[B, psycopg2.extensions.ISQLQuote]
|
2010-11-08 04:35:06 +03:00
|
|
|
|
2011-02-15 20:11:07 +03:00
|
|
|
@testutils.skip_from_python(3)
|
2010-11-08 04:35:06 +03:00
|
|
|
def test_no_mro_no_joy(self):
|
|
|
|
from psycopg2.extensions import adapt, register_adapter, AsIs
|
|
|
|
|
|
|
|
class A: pass
|
|
|
|
class B(A): pass
|
|
|
|
|
|
|
|
register_adapter(A, lambda a: AsIs("a"))
|
2011-01-02 19:09:30 +03:00
|
|
|
try:
|
|
|
|
self.assertRaises(psycopg2.ProgrammingError, adapt, B())
|
|
|
|
finally:
|
|
|
|
del psycopg2.extensions.adapters[A, psycopg2.extensions.ISQLQuote]
|
|
|
|
|
2010-11-08 04:35:06 +03:00
|
|
|
|
2011-02-15 20:11:07 +03:00
|
|
|
@testutils.skip_before_python(3)
|
2010-12-24 17:44:29 +03:00
|
|
|
def test_adapt_subtype_3(self):
|
|
|
|
from psycopg2.extensions import adapt, register_adapter, AsIs
|
|
|
|
|
|
|
|
class A: pass
|
|
|
|
class B(A): pass
|
|
|
|
|
|
|
|
register_adapter(A, lambda a: AsIs("a"))
|
2011-01-03 23:43:02 +03:00
|
|
|
try:
|
|
|
|
self.assertEqual(b("a"), adapt(B()).getquoted())
|
|
|
|
finally:
|
|
|
|
del psycopg2.extensions.adapters[A, psycopg2.extensions.ISQLQuote]
|
2010-12-24 17:44:29 +03:00
|
|
|
|
|
|
|
|
2011-03-26 15:57:14 +03:00
|
|
|
class ByteaParserTest(unittest.TestCase):
|
|
|
|
"""Unit test for our bytea format parser."""
|
|
|
|
def setUp(self):
|
|
|
|
try:
|
|
|
|
self._cast = self._import_cast()
|
|
|
|
except Exception, e:
|
2011-03-26 17:27:58 +03:00
|
|
|
self._cast = None
|
|
|
|
self._exc = e
|
2011-03-26 15:57:14 +03:00
|
|
|
|
|
|
|
def _import_cast(self):
|
|
|
|
"""Use ctypes to access the C function.
|
|
|
|
|
|
|
|
Raise any sort of error: we just support this where ctypes works as
|
|
|
|
expected.
|
|
|
|
"""
|
|
|
|
import ctypes
|
|
|
|
lib = ctypes.cdll.LoadLibrary(psycopg2._psycopg.__file__)
|
|
|
|
cast = lib.typecast_BINARY_cast
|
|
|
|
cast.argtypes = [ctypes.c_char_p, ctypes.c_size_t, ctypes.py_object]
|
|
|
|
cast.restype = ctypes.py_object
|
|
|
|
return cast
|
|
|
|
|
|
|
|
def cast(self, buffer):
|
|
|
|
"""Cast a buffer from the output format"""
|
|
|
|
l = buffer and len(buffer) or 0
|
|
|
|
rv = self._cast(buffer, l, None)
|
|
|
|
|
|
|
|
if rv is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
return str(rv)
|
|
|
|
else:
|
|
|
|
return rv.tobytes()
|
|
|
|
|
|
|
|
def test_null(self):
|
|
|
|
rv = self.cast(None)
|
|
|
|
self.assertEqual(rv, None)
|
|
|
|
|
|
|
|
def test_blank(self):
|
|
|
|
rv = self.cast(b(''))
|
|
|
|
self.assertEqual(rv, b(''))
|
|
|
|
|
|
|
|
def test_blank_hex(self):
|
|
|
|
# Reported as problematic in ticket #48
|
|
|
|
rv = self.cast(b('\\x'))
|
|
|
|
self.assertEqual(rv, b(''))
|
|
|
|
|
|
|
|
def test_full_hex(self, upper=False):
|
|
|
|
buf = ''.join(("%02x" % i) for i in range(256))
|
|
|
|
if upper: buf = buf.upper()
|
|
|
|
buf = '\\x' + buf
|
|
|
|
rv = self.cast(b(buf))
|
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
self.assertEqual(rv, ''.join(map(chr, range(256))))
|
|
|
|
else:
|
|
|
|
self.assertEqual(rv, bytes(range(256)))
|
|
|
|
|
|
|
|
def test_full_hex_upper(self):
|
|
|
|
return self.test_full_hex(upper=True)
|
|
|
|
|
|
|
|
def test_full_escaped_octal(self):
|
|
|
|
buf = ''.join(("\\%03o" % i) for i in range(256))
|
|
|
|
rv = self.cast(b(buf))
|
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
self.assertEqual(rv, ''.join(map(chr, range(256))))
|
|
|
|
else:
|
|
|
|
self.assertEqual(rv, bytes(range(256)))
|
|
|
|
|
|
|
|
def test_escaped_mixed(self):
|
|
|
|
import string
|
|
|
|
buf = ''.join(("\\%03o" % i) for i in range(32))
|
|
|
|
buf += string.ascii_letters
|
|
|
|
buf += ''.join('\\' + c for c in string.ascii_letters)
|
|
|
|
buf += '\\\\'
|
|
|
|
rv = self.cast(b(buf))
|
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
tgt = ''.join(map(chr, range(32))) \
|
|
|
|
+ string.ascii_letters * 2 + '\\'
|
|
|
|
else:
|
|
|
|
tgt = bytes(range(32)) + \
|
|
|
|
(string.ascii_letters * 2 + '\\').encode('ascii')
|
|
|
|
|
|
|
|
self.assertEqual(rv, tgt)
|
|
|
|
|
2011-03-26 17:27:58 +03:00
|
|
|
def skip_if_cant_cast(f):
|
|
|
|
def skip_if_cant_cast_(self, *args, **kwargs):
|
|
|
|
if self._cast is None:
|
|
|
|
return self.skipTest("can't test bytea parser: %s - %s"
|
|
|
|
% (self._exc.__class__.__name__, self._exc))
|
|
|
|
|
|
|
|
return f(self, *args, **kwargs)
|
|
|
|
|
|
|
|
return skip_if_cant_cast_
|
|
|
|
|
|
|
|
decorate_all_tests(ByteaParserTest, skip_if_cant_cast)
|
|
|
|
|
2011-03-26 15:57:14 +03:00
|
|
|
|
2007-11-11 13:40:12 +03:00
|
|
|
def test_suite():
|
|
|
|
return unittest.TestLoader().loadTestsFromName(__name__)
|
2004-12-20 05:33:12 +03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2007-11-11 13:40:12 +03:00
|
|
|
unittest.main()
|
|
|
|
|