Changeset 823

Show
Ignore:
Timestamp:
03/09/07 22:06:11 (2 years ago)
Author:
mfenniak
Message:

add new error handling code, and associated tests. attempting to debug timestamp bin format bug

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pg8000/trunk/pg8000.py

    r822 r823  
    880880                        self._send(Protocol.Execute(portal, 0)) 
    881881                        self._send(Protocol.Sync()) 
     882                        exc = None 
    882883                        while 1: 
    883884                            msg = self._read_message() 
     
    886887                                pass 
    887888                            elif isinstance(msg, Protocol.ReadyForQuery): 
    888                                 # ready to move on with life... 
     889                                if exc != None: 
     890                                    raise exc 
    889891                                break 
    890892                            elif isinstance(msg, Protocol.ErrorResponse): 
    891                                 raise msg.createException() 
     893                                exc = msg.createException() 
    892894                            else: 
    893895                                raise InternalError("unexpected response") 
     
    10851087 
    10861088    def py_value(v, description, **kwargs): 
     1089        print repr(v), repr(description) 
    10871090        if v == None: 
    10881091            # special case - NULL value 
     
    11421145    def timestamp_recv(data, **kwargs): 
    11431146        val = struct.unpack("!d", data)[0] 
     1147        print "timestamp_recv", repr(val), repr(data) 
    11441148        return datetime.datetime(2000, 1, 1) + datetime.timedelta(seconds = val) 
    11451149 
  • pg8000/trunk/pg8000-test.py

    r821 r823  
    77import pg8000 
    88 
    9 #db = pg8000.Connection(host='joy', user='Mathieu Fenniak', database="software", password="hello", socket_timeout=5) 
     9db = pg8000.Connection(host='joy', user='pg8000-test', database='pg8000-test', password='pg8000-test', socket_timeout=5) 
    1010#db = pg8000.Connection(host='localhost', user='mfenniak') 
    11 db = pg8000.Connection(user="mfenniak", unix_sock="/tmp/.s.PGSQL.5432") 
     11#db = pg8000.Connection(user="mfenniak", unix_sock="/tmp/.s.PGSQL.5432") 
    1212 
     13print "testing db.execute and error recovery in NoData query" 
     14for i in range(1, 3): 
     15    db.begin() 
     16    try: 
     17        db.execute("DROP TABLE t1") 
     18        db.commit() 
     19    except pg8000.DatabaseError, e: 
     20        assert e.args[1] == '42P01' # table does not exist 
     21        db.rollback() 
     22        db.begin() 
     23 
     24print "creating test table" 
    1325db.begin() 
     26db.execute("CREATE TABLE t1 (f1 int primary key, f2 int not null, f3 varchar(50) null)") 
     27db.commit() 
    1428 
    15 db.execute("DROP TABLE t1") 
    16 db.execute("CREATE TABLE t1 (f1 int primary key, f2 int not null, f3 varchar(50) null)") 
     29print "testing db.execute on query, error recovery in on parsing" 
     30for i in range(1, 3): 
     31    db.begin() 
     32    try: 
     33        db.execute("SELECT * FROM table_that_does_not_exist") 
     34        print "error - shouldn't get here" 
     35        for row in db.iterate_dict(): 
     36            print "definately shouldn't be here... %r" % row 
     37    except pg8000.DatabaseError, e: 
     38        assert e.args[1] == '42P01' # table does not exist 
     39        db.rollback() 
     40        db.begin() 
    1741 
     42print "testing multithreaded prepared statement with arguments" 
    1843# Not the most efficient way to do this.  Multiple DB connections would 
    1944# multiplex this insert and make it faster -- we're just testing for thread 
     
    3459 
    3560 
    36 print "begin query..." 
     61print "testing basic query..." 
    3762db.execute("SELECT * FROM t1") 
    3863for row in db.iterate_dict(): 
    39     print repr(row) 
    40 print "end query..." 
     64    assert row.has_key("f1") and row.has_key("f2") and row.has_key("f3") 
    4165 
    42 #print "begin query..." 
     66print "testing two queries at once..." 
    4367cur1 = pg8000.Cursor(db) 
    44 #cur1.execute("SELECT * FROM t1") 
    45 #s1 = pg8000.PreparedStatement(db, "SELECT * FROM t1 WHERE f1 > $1", int) 
    46 #i = 0 
    47 #for row1 in cur1.iterate_dict(): 
    48 #    i = i + 1 
    49 #    print i, repr(row1) 
    50 #    s1.execute(row1['f1']) 
    51 #    for row2 in s1.iterate_dict(): 
    52 #        print "\t", repr(row2) 
    53 #print "end query..." 
     68cur1.execute("SELECT * FROM t1") 
     69s1 = pg8000.PreparedStatement(db, "SELECT f1, f2 FROM t1 WHERE f1 > $1", int) 
     70i = 0 
     71for row1 in cur1.iterate_dict(): 
     72    assert row1.has_key("f1") and row1.has_key("f2") and row1.has_key("f3") 
     73    i = i + 1 
     74    s1.execute(row1['f1']) 
     75    for row2 in s1.iterate_dict(): 
     76        assert row2.has_key("f1") and row2.has_key("f2") and not row2.has_key("f3") 
    5477 
    55 print "Beginning type checks..." 
     78print "beginning type checks..." 
    5679 
    5780cur1.execute("SELECT $1", 5) 
     
    89112 
    90113cur1.execute("SELECT '2001-02-03 04:05:06.17'::timestamp") 
    91 assert tuple(cur1.iterate_dict()) == ({'timestamp': datetime.datetime(2001, 2, 3, 4, 5, 6, 170000)},) 
     114retval = tuple(cur1.iterate_dict()) 
     115print repr(retval) 
     116assert retval == ({'timestamp': datetime.datetime(2001, 2, 3, 4, 5, 6, 170000)},) 
    92117 
    93118#cur1.execute("SELECT '2001-02-03 04:05:06.17'::timestamp with time zone") 
     
    98123#print repr(tuple(cur1.iterate_dict())) 
    99124 
    100 print "Type checks complete." 
    101125 
     126print "tests complete"