446ad15337754bc2663abd80103435456fd4b6f8
[plcapi.git] / psycopg2 / tests / types_basic.py
1 # types_basic.py - tests for basic types conversions
2 #
3 # Copyright (C) 2004 Federico Di Gregorio  <fog@debian.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2, or (at your option) any later
8 # version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 # for more details.
14
15 import sys
16 try:
17     import decimal
18 except:
19     pass
20 import psycopg2
21 from unittest import TestCase, TestSuite, main
22
23
24 class TypesBasicTests(TestCase):
25     """Test presence of mandatory attributes and methods."""
26
27     def setUp(self):
28         self.conn = psycopg2.connect("dbname=test")
29
30     def execute(self, *args):
31         curs = self.conn.cursor()
32         curs.execute(*args)
33         return curs.fetchone()[0]
34     
35     def testQuoting(self):
36         s = "Quote'this\\! ''ok?''"
37         self.failUnless(self.execute("SELECT %s AS foo", (s,)) == s,
38                         "wrong quoting: " + s)
39
40     def testUnicode(self):
41         s = u"Quote'this\\! ''ok?''"
42         self.failUnless(self.execute("SELECT %s AS foo", (s,)) == s,
43                         "wrong unicode quoting: " + s)
44
45     def testNumber(self):
46         s = self.execute("SELECT %s AS foo", (1971,))
47         self.failUnless(s == 1971, "wrong integer quoting: " + str(s))
48         s = self.execute("SELECT %s AS foo", (1971L,))
49         self.failUnless(s == 1971L, "wrong integer quoting: " + str(s))
50         # Python 2.4 defaults to Decimal?
51         if sys.version_info[0] >= 2 and sys.version_info[1] >= 4:
52             s = self.execute("SELECT %s AS foo", (19.10,))
53             self.failUnless(s - decimal.Decimal("19.10") == 0,
54                             "wrong decimal quoting: " + str(s))
55         else:
56             s = self.execute("SELECT %s AS foo", (19.10,))
57             self.failUnless(abs(s - 19.10) < 0.001,
58                             "wrong float quoting: " + str(s))
59
60     def testBinary(self):
61         s = ''.join([chr(x) for x in range(256)])
62         b = psycopg2.Binary(s)
63         r = str(self.execute("SELECT %s::bytea AS foo", (b,)))
64         self.failUnless(r == s, "wrong binary quoting")
65         # test to make sure an empty Binary is converted to an empty string
66         b = psycopg2.Binary('')
67         self.assertEqual(str(b), "''")
68
69     def testArray(self):
70         s = self.execute("SELECT %s AS foo", ([[1,2],[3,4]],))
71         self.failUnless(s == [[1,2],[3,4]], "wrong array quoting " + str(s))
72         s = self.execute("SELECT %s AS foo", (['one', 'two', 'three'],))
73         self.failUnless(s == ['one', 'two', 'three'], 
74                         "wrong array quoting " + str(s))
75         
76         
77 class TypesBasicSuite(TestSuite):
78     """Build a suite of all tests."""
79
80     def __init__(self):
81         """Build a list of tests."""
82         self.tests = [x for x in dir(TypesBasicTests) if x.startswith('test')]
83         TestSuite.__init__(self, map(TestModule, self.tests))
84
85
86 if __name__ == "__main__":
87     main()