root/pg8000/pg8000-v1.00/pg8000-test.py

Revision 816, 2.8 kB (checked in by mfenniak, 2 years ago)

Add support for NULL DB parameters and values

  • Property svn:executable set to
Line 
1 #!/usr/bin/env python
2
3 import datetime
4 import decimal
5
6 import pg8000
7
8 #db = pg8000.Connection(host='joy', user='Mathieu Fenniak', database="software", password="hello", socket_timeout=5)
9 db = pg8000.Connection(host='localhost', user='mfenniak')
10
11 db.begin()
12
13 db.execute("DROP TABLE t1")
14 db.execute("CREATE TABLE t1 (f1 int primary key, f2 int not null, f3 varchar(50) null)")
15
16 s1 = pg8000.PreparedStatement(db, "INSERT INTO t1 (f1, f2, f3) VALUES ($1, $2, $3)", int, int, str)
17 s1.execute(1, 1, "hello")
18 s1.execute(2, 10, "he\u0173llo")
19 s1.execute(3, 100, "hello")
20 s1.execute(4, 1000, None)
21 s1.execute(5, 10000, "hello")
22 s1.execute(6, 100000, "hello")
23
24 print "begin query..."
25 db.execute("SELECT * FROM t1")
26 for row in db.iterate_dict():
27     print repr(row)
28 print "end query..."
29
30 print "begin query..."
31 cur1 = pg8000.Cursor(db)
32 cur1.execute("SELECT * FROM t1")
33 s1 = pg8000.PreparedStatement(db, "SELECT * FROM t1 WHERE f1 > $1", int)
34 i = 0
35 for row1 in cur1.iterate_dict():
36     i = i + 1
37     print i, repr(row1)
38     s1.execute(row1['f1'])
39     for row2 in s1.iterate_dict():
40         print "\t", repr(row2)
41 print "end query..."
42
43 print "Beginning type checks..."
44
45 cur1.execute("SELECT $1", 5)
46 assert tuple(cur1.iterate_dict()) == ({"?column?": 5},)
47
48 cur1.execute("SELECT 5000::smallint")
49 assert tuple(cur1.iterate_dict()) == ({"int2": 5000},)
50
51 cur1.execute("SELECT 5000::integer")
52 assert tuple(cur1.iterate_dict()) == ({"int4": 5000},)
53
54 cur1.execute("SELECT 50000000000000::bigint")
55 assert tuple(cur1.iterate_dict()) == ({"int8": 50000000000000},)
56
57 cur1.execute("SELECT 5000.023232::decimal")
58 assert tuple(cur1.iterate_dict()) == ({"numeric": decimal.Decimal("5000.023232")},)
59
60 cur1.execute("SELECT 1.1::real")
61 assert tuple(cur1.iterate_dict()) == ({"float4": 1.1000000238418579},)
62
63 cur1.execute("SELECT 1.1::double precision")
64 assert tuple(cur1.iterate_dict()) == ({"float8": 1.1000000000000001},)
65
66 cur1.execute("SELECT 'hello'::varchar(50)")
67 assert tuple(cur1.iterate_dict()) == ({"varchar": u"hello"},)
68
69 cur1.execute("SELECT 'hello'::char(20)")
70 assert tuple(cur1.iterate_dict()) == ({"bpchar": u"hello               "},)
71
72 cur1.execute("SELECT 'hello'::text")
73 assert tuple(cur1.iterate_dict()) == ({"text": u"hello"},)
74
75 #cur1.execute("SELECT 'hell\007o'::bytea")
76 #assert tuple(cur1.iterate_dict()) == ({"bytea": "hello"},)
77
78 cur1.execute("SELECT '2001-02-03 04:05:06.17'::timestamp")
79 assert tuple(cur1.iterate_dict()) == ({'timestamp': datetime.datetime(2001, 2, 3, 4, 5, 6, 170000)},)
80
81 #cur1.execute("SELECT '2001-02-03 04:05:06.17'::timestamp with time zone")
82 #assert tuple(cur1.iterate_dict()) == ({'timestamp': datetime.datetime(2001, 2, 3, 4, 5, 6, 170000, pg8000.Types.FixedOffsetTz("-07"))},)
83
84 cur1.execute("SELECT '1 month'::interval")
85 assert tuple(cur1.iterate_dict()) == ({'interval': '1 mon'},)
86 #print repr(tuple(cur1.iterate_dict()))
87
88 print "Type checks complete."
89
90 db.commit()
91
Note: See TracBrowser for help on using the browser.