1
1
#!/usr/bin/env python
2
3
# types_basic.py - tests for basic types conversions
4
# Copyright (C) 2004 Federico Di Gregorio <fog@debian.org>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by the
8
# Free Software Foundation; either version 2, or (at your option) any later
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
13
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5
# Copyright (C) 2004-2010 Federico Di Gregorio <fog@debian.org>
7
# psycopg2 is free software: you can redistribute it and/or modify it
8
# under the terms of the GNU Lesser General Public License as published
9
# by the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
12
# In addition, as a special exception, the copyright holders give
13
# permission to link this program with the OpenSSL library (or with
14
# modified versions of OpenSSL that use the same license as OpenSSL),
15
# and distribute linked combinations including the two.
17
# You must obey the GNU Lesser General Public License in all respects for
18
# all of the code used other than OpenSSL.
20
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
21
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
23
# License for more details.
50
59
self.failUnless(s == 1971, "wrong integer quoting: " + str(s))
51
60
s = self.execute("SELECT %s AS foo", (1971L,))
52
61
self.failUnless(s == 1971L, "wrong integer quoting: " + str(s))
53
# Python 2.4 defaults to Decimal?
62
if sys.version_info[0] < 2 or sys.version_info[1] < 4:
63
s = self.execute("SELECT %s AS foo", (19.10,))
64
self.failUnless(abs(s - 19.10) < 0.001,
65
"wrong float quoting: " + str(s))
67
def testDecimal(self):
54
68
if sys.version_info[0] >= 2 and sys.version_info[1] >= 4:
55
s = self.execute("SELECT %s AS foo", (19.10,))
69
s = self.execute("SELECT %s AS foo", (decimal.Decimal("19.10"),))
56
70
self.failUnless(s - decimal.Decimal("19.10") == 0,
57
71
"wrong decimal quoting: " + str(s))
59
s = self.execute("SELECT %s AS foo", (19.10,))
60
self.failUnless(abs(s - 19.10) < 0.001,
61
"wrong float quoting: " + str(s))
72
s = self.execute("SELECT %s AS foo", (decimal.Decimal("NaN"),))
73
self.failUnless(str(s) == "NaN", "wrong decimal quoting: " + str(s))
74
self.failUnless(type(s) == decimal.Decimal, "wrong decimal conversion: " + repr(s))
75
s = self.execute("SELECT %s AS foo", (decimal.Decimal("infinity"),))
76
self.failUnless(str(s) == "NaN", "wrong decimal quoting: " + str(s))
77
self.failUnless(type(s) == decimal.Decimal, "wrong decimal conversion: " + repr(s))
78
s = self.execute("SELECT %s AS foo", (decimal.Decimal("-infinity"),))
79
self.failUnless(str(s) == "NaN", "wrong decimal quoting: " + str(s))
80
self.failUnless(type(s) == decimal.Decimal, "wrong decimal conversion: " + repr(s))
63
82
def testFloat(self):
64
83
s = self.execute("SELECT %s AS foo", (float("nan"),))
77
96
def testBinaryEmptyString(self):
78
97
# test to make sure an empty Binary is converted to an empty string
79
98
b = psycopg2.Binary('')
80
self.assertEqual(str(b), "''")
99
self.assertEqual(str(b), "''::bytea")
82
101
def testBinaryRoundTrip(self):
83
102
# test to make sure buffers returned by psycopg2 are
94
113
self.failUnless(s == ['one', 'two', 'three'],
95
114
"wrong array quoting " + str(s))
116
def testTypeRoundtripBinary(self):
117
o1 = buffer("".join(map(chr, range(256))))
118
o2 = self.execute("select %s;", (o1,))
119
self.assertEqual(type(o1), type(o2))
121
# Test with an empty buffer
123
o2 = self.execute("select %s;", (o1,))
124
self.assertEqual(type(o1), type(o2))
126
def testTypeRoundtripBinaryArray(self):
127
o1 = buffer("".join(map(chr, range(256))))
129
o2 = self.execute("select %s;", (o1,))
130
self.assertEqual(type(o1[0]), type(o2[0]))
99
133
return unittest.TestLoader().loadTestsFromName(__name__)