Changeset 772 for trunk/pgdb.py


Ignore:
Timestamp:
Jan 20, 2016, 5:44:20 PM (4 years ago)
Author:
cito
Message:

Improve test coverage for the pgdb module

Includes a simple patch that allows storing Python lists or tuple values
in PostgreSQL array fields (they are not yet converted when read, though).

Also re-activated the shortcut methods on the connection again
since they can be sometimes useful.

Test coverage is now around 95%, the remaining lines are due to support for
old Python versions or obscure database errors that can't easily be aroused.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pgdb.py

    r740 r772  
    113113paramstyle = 'pyformat'
    114114
    115 # shortcut methods are not supported by default
    116 # since they have been excluded from DB API 2
    117 # and are not recommended by the DB SIG.
    118 
    119 shortcutmethods = 0
     115# shortcut methods have been excluded from DB API 2 and
     116# are not recommended by the DB SIG, but they can be handy
     117shortcutmethods = 1
    120118
    121119
     
    145143
    146144def _cast_float(value):
    147     try:
    148         return float(value)
    149     except ValueError:
    150         if value == 'NaN':
    151             return nan
    152         elif value == 'Infinity':
    153             return inf
    154         elif value == '-Infinity':
    155             return -inf
    156         raise
     145    return float(value)  # this also works with NaN and Infinity
    157146
    158147
     
    281270            val = 'NULL'
    282271        elif isinstance(val, (list, tuple)):
    283             val = '(%s)' % ','.join(map(lambda v: str(self._quote(v)), val))
     272            q = self._quote
     273            val = 'ARRAY[%s]' % ','.join(str(q(v)) for v in val)
    284274        elif Decimal is not float and isinstance(val, Decimal):
    285275            pass
     
    340330                    self._cnx.source().execute(sql)
    341331                except DatabaseError:
    342                     raise
    343                 except Exception:
     332                    raise  # database provides error message
     333                except Exception as err:
    344334                    raise _op_error("can't start transaction")
    345335                self._dbcnx._tnx = True
     
    355345                    self.rowcount = -1
    356346        except DatabaseError:
    357             raise
     347            raise  # database provides error message
    358348        except Error as err:
    359             raise _db_error("error in '%s': '%s' " % (sql, err))
     349            raise _db_error(
     350                "error in '%s': '%s' " % (sql, err), InterfaceError)
    360351        except Exception as err:
    361352            raise _op_error("internal error in '%s': %s" % (sql, err))
     
    494485            if size is None:
    495486                size = 8192
     487            elif not isinstance(size, int):
     488                raise TypeError("The size option must be an integer")
    496489            if size > 0:
    497                 if not isinstance(size, int):
    498                     raise TypeError("The size option must be an integer")
    499490
    500491                def chunks():
Note: See TracChangeset for help on using the changeset viewer.