1 # types_basic.py - tests for basic types conversions
3 # Copyright (C) 2004 Federico Di Gregorio <fog@debian.org>
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
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
21 from unittest import TestCase, TestSuite, main
24 class TypesBasicTests(TestCase):
25 """Test presence of mandatory attributes and methods."""
28 self.conn = psycopg2.connect("dbname=test")
30 def execute(self, *args):
31 curs = self.conn.cursor()
33 return curs.fetchone()[0]
35 def testQuoting(self):
36 s = "Quote'this\\! ''ok?''"
37 self.failUnless(self.execute("SELECT %s AS foo", (s,)) == s,
38 "wrong quoting: " + s)
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)
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))
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))
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), "''")
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))
77 class TypesBasicSuite(TestSuite):
78 """Build a suite of all tests."""
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))
86 if __name__ == "__main__":