From: Mike Bayer Date: Fri, 9 Apr 2010 17:01:17 +0000 (-0400) Subject: - fixed numeric test for pg8000, factored out decimal/float codes X-Git-Tag: rel_0_6_0~27^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d61a48ccfd1024b744c003d354cd95b136d41a43;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - fixed numeric test for pg8000, factored out decimal/float codes --- diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index dcdaa3c7b1..312ae9aa8a 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -80,6 +80,10 @@ from sqlalchemy.types import INTEGER, BIGINT, SMALLINT, VARCHAR, \ CHAR, TEXT, FLOAT, NUMERIC, \ DATE, BOOLEAN +_DECIMAL_TYPES = (1700, 1231) +_FLOAT_TYPES = (700, 701, 1021, 1022) + + class REAL(sqltypes.Float): __visit_name__ = "REAL" diff --git a/lib/sqlalchemy/dialects/postgresql/pg8000.py b/lib/sqlalchemy/dialects/postgresql/pg8000.py index c822e37a57..862c915aa2 100644 --- a/lib/sqlalchemy/dialects/postgresql/pg8000.py +++ b/lib/sqlalchemy/dialects/postgresql/pg8000.py @@ -26,23 +26,24 @@ from sqlalchemy import util, exc from sqlalchemy import processors from sqlalchemy import types as sqltypes from sqlalchemy.dialects.postgresql.base import PGDialect, \ - PGCompiler, PGIdentifierPreparer, PGExecutionContext + PGCompiler, PGIdentifierPreparer, PGExecutionContext,\ + _DECIMAL_TYPES, _FLOAT_TYPES class _PGNumeric(sqltypes.Numeric): def result_processor(self, dialect, coltype): if self.asdecimal: - if coltype in (700, 701, 1021, 1022): + if coltype in _FLOAT_TYPES: return processors.to_decimal_processor_factory(decimal.Decimal) - elif coltype in (1700, 1231): + elif coltype in _DECIMAL_TYPES: # pg8000 returns Decimal natively for 1700 return None else: raise exc.InvalidRequestError("Unknown PG numeric type: %d" % coltype) else: - if coltype in (700, 701, 1021, 1022): + if coltype in _FLOAT_TYPES: # pg8000 returns float natively for 701 return None - elif coltype in (1700, 1231): + elif coltype in _DECIMAL_TYPES: return processors.to_float else: raise exc.InvalidRequestError("Unknown PG numeric type: %d" % coltype) diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py index 45d1bf29e5..2a51a72397 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py @@ -68,7 +68,7 @@ from sqlalchemy.sql import operators as sql_operators from sqlalchemy import types as sqltypes from sqlalchemy.dialects.postgresql.base import PGDialect, PGCompiler, \ PGIdentifierPreparer, PGExecutionContext, \ - ENUM, ARRAY + ENUM, ARRAY, _DECIMAL_TYPES, _FLOAT_TYPES logger = logging.getLogger('sqlalchemy.dialects.postgresql') @@ -80,18 +80,18 @@ class _PGNumeric(sqltypes.Numeric): def result_processor(self, dialect, coltype): if self.asdecimal: - if coltype in (700, 701, 1021, 1022): + if coltype in _FLOAT_TYPES: return processors.to_decimal_processor_factory(decimal.Decimal) - elif coltype in (1700, 1231): + elif coltype in _DECIMAL_TYPES: # pg8000 returns Decimal natively for 1700 return None else: raise exc.InvalidRequestError("Unknown PG numeric type: %d" % coltype) else: - if coltype in (700, 701, 1021, 1022): + if coltype in _FLOAT_TYPES: # pg8000 returns float natively for 701 return None - elif coltype in (1700, 1231): + elif coltype in _DECIMAL_TYPES: return processors.to_float else: raise exc.InvalidRequestError("Unknown PG numeric type: %d" % coltype) diff --git a/test/dialect/test_postgresql.py b/test/dialect/test_postgresql.py index bcf8ac9562..14814bc20f 100644 --- a/test/dialect/test_postgresql.py +++ b/test/dialect/test_postgresql.py @@ -1326,11 +1326,12 @@ class MiscTest(TestBase, AssertsExecutionResults, AssertsCompiledSQL): exception_cls = eng.dialect.dbapi.ProgrammingError assert_raises(exception_cls, eng.execute, "show transaction isolation level") - @testing.only_on('postgresql+psycopg2', - "this assertion isn't used on others, " - "except pg8000 which circumvents it") + @testing.fails_on('+zxjdbc', + "psycopg2/pg8000 specific assertion") + @testing.fails_on('pypostgresql', + "psycopg2/pg8000 specific assertion") def test_numeric_raise(self): - stmt = text("select 'hi' as hi", typemap={'hi':Numeric}) + stmt = text("select cast('hi' as char) as hi", typemap={'hi':Numeric}) assert_raises( exc.InvalidRequestError, testing.db.execute, stmt