~james-w/ubuntu/lucid/psycopg2/precise-backport

« back to all changes in this revision

Viewing changes to tests/types_basic.py

  • Committer: Mikhail Turov
  • Date: 2010-07-28 20:30:01 UTC
  • mfrom: (5.1.9 sid)
  • Revision ID: groldster@gmail.com-20100728203001-u1dv46tnd3s02ejg
Tags: 2.2.1-1ubuntu1
releasing version 2.2.1-1ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
 
2
#
2
3
# types_basic.py - tests for basic types conversions
3
4
#
4
 
# Copyright (C) 2004 Federico Di Gregorio  <fog@debian.org>
5
 
#
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
9
 
# version.
10
 
#
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
14
 
# for more details.
 
5
# Copyright (C) 2004-2010 Federico Di Gregorio  <fog@debian.org>
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
17
# You must obey the GNU Lesser General Public License in all respects for
 
18
# all of the code used other than OpenSSL.
 
19
#
 
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.
15
24
 
16
25
try:
17
26
    import decimal
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))
 
66
 
 
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))
58
 
        else:
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))
62
81
 
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")
81
100
 
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))
96
115
 
 
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))
 
120
 
 
121
        # Test with an empty buffer
 
122
        o1 = buffer("")
 
123
        o2 = self.execute("select %s;", (o1,))
 
124
        self.assertEqual(type(o1), type(o2))
 
125
 
 
126
    def testTypeRoundtripBinaryArray(self):
 
127
        o1 = buffer("".join(map(chr, range(256))))
 
128
        o1 = [o1]
 
129
        o2 = self.execute("select %s;", (o1,))
 
130
        self.assertEqual(type(o1[0]), type(o2[0]))
97
131
 
98
132
def test_suite():
99
133
    return unittest.TestLoader().loadTestsFromName(__name__)