diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-05-30 12:08:26 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-05-30 12:08:26 -0400 |
commit | f000161f242f6814d594b5ccf91f96d99490ae61 (patch) | |
tree | 2348753e33be7b708e1108269e1d9b68b69d0326 | |
parent | 49c667e27dc6d09204c2bc18d77a5dc86bb20f53 (diff) | |
parent | f8f29d0a105a4a84f09a05b519142002cee1f5f6 (diff) | |
download | sqlalchemy-f000161f242f6814d594b5ccf91f96d99490ae61.tar.gz |
Merge branch 'master' of https://github.com/tlocke/sqlalchemy into tlocke-master
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/pg8000.py | 61 | ||||
-rw-r--r-- | test/dialect/postgresql/test_dialect.py | 111 | ||||
-rw-r--r-- | test/dialect/postgresql/test_types.py | 6 | ||||
-rw-r--r-- | test/requirements.py | 9 |
4 files changed, 98 insertions, 89 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/pg8000.py b/lib/sqlalchemy/dialects/postgresql/pg8000.py index bc73f9757..edb1afc39 100644 --- a/lib/sqlalchemy/dialects/postgresql/pg8000.py +++ b/lib/sqlalchemy/dialects/postgresql/pg8000.py @@ -1,5 +1,6 @@ # postgresql/pg8000.py -# Copyright (C) 2005-2014 the SQLAlchemy authors and contributors <see AUTHORS file> +# Copyright (C) 2005-2014 the SQLAlchemy authors and contributors <see AUTHORS +# file> # # This module is part of SQLAlchemy and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php @@ -8,31 +9,30 @@ .. dialect:: postgresql+pg8000 :name: pg8000 :dbapi: pg8000 - :connectstring: postgresql+pg8000://user:password@host:port/dbname[?key=value&key=value...] - :url: http://pybrary.net/pg8000/ + :connectstring: \ +postgresql+pg8000://user:password@host:port/dbname[?key=value&key=value...] + :url: https://pythonhosted.org/pg8000/ Unicode ------- -pg8000 requires that the postgresql client encoding be -configured in the postgresql.conf file in order to use encodings -other than ascii. Set this value to the same value as the -"encoding" parameter on create_engine(), usually "utf-8". +When communicating with the server, pg8000 uses the character set that the +server asks it to use (the client encoding). By default the client encoding is +the database's character set (chosen when the database is created), but the +client encoding can be changed in a number of ways (eg. setting CLIENT_ENCODING +in postgresql.conf). -Interval --------- - -Passing data from/to the Interval type is not supported as of -yet. +Set the "encoding" parameter on create_engine(), to the same as the client +encoding, usually "utf-8". """ from ... import util, exc import decimal from ... import processors from ... import types as sqltypes -from .base import PGDialect, \ - PGCompiler, PGIdentifierPreparer, PGExecutionContext,\ - _DECIMAL_TYPES, _FLOAT_TYPES, _INT_TYPES +from .base import ( + PGDialect, PGCompiler, PGIdentifierPreparer, PGExecutionContext, + _DECIMAL_TYPES, _FLOAT_TYPES, _INT_TYPES) class _PGNumeric(sqltypes.Numeric): @@ -40,14 +40,13 @@ class _PGNumeric(sqltypes.Numeric): if self.asdecimal: if coltype in _FLOAT_TYPES: return processors.to_decimal_processor_factory( - decimal.Decimal, - self._effective_decimal_return_scale) + decimal.Decimal, self._effective_decimal_return_scale) elif coltype in _DECIMAL_TYPES or coltype in _INT_TYPES: # pg8000 returns Decimal natively for 1700 return None else: raise exc.InvalidRequestError( - "Unknown PG numeric type: %d" % coltype) + "Unknown PG numeric type: %d" % coltype) else: if coltype in _FLOAT_TYPES: # pg8000 returns float natively for 701 @@ -56,7 +55,7 @@ class _PGNumeric(sqltypes.Numeric): return processors.to_float else: raise exc.InvalidRequestError( - "Unknown PG numeric type: %d" % coltype) + "Unknown PG numeric type: %d" % coltype) class _PGNumericNoBind(_PGNumeric): @@ -71,7 +70,7 @@ class PGExecutionContext_pg8000(PGExecutionContext): class PGCompiler_pg8000(PGCompiler): def visit_mod_binary(self, binary, operator, **kw): return self.process(binary.left, **kw) + " %% " + \ - self.process(binary.right, **kw) + self.process(binary.right, **kw) def post_process_text(self, text): if '%%' in text: @@ -111,7 +110,7 @@ class PGDialect_pg8000(PGDialect): @classmethod def dbapi(cls): - return __import__('pg8000').dbapi + return __import__('pg8000') def create_connect_args(self, url): opts = url.translate_connect_args(username='user') @@ -123,4 +122,24 @@ class PGDialect_pg8000(PGDialect): def is_disconnect(self, e, connection, cursor): return "connection is closed" in str(e) + def set_isolation_level(self, connection, level): + level = level.replace('_', ' ') + + if level == 'AUTOCOMMIT': + connection.connection.autocommit = True + elif level in self._isolation_lookup: + connection.connection.autocommit = False + cursor = connection.cursor() + cursor.execute( + "SET SESSION CHARACTERISTICS AS TRANSACTION " + "ISOLATION LEVEL %s" % level) + cursor.execute("COMMIT") + cursor.close() + else: + raise exc.ArgumentError( + "Invalid value '%s' for isolation_level. " + "Valid isolation levels for %s are %s or AUTOCOMMIT" % + (level, self.name, ", ".join(self._isolation_lookup)) + ) + dialect = PGDialect_pg8000 diff --git a/test/dialect/postgresql/test_dialect.py b/test/dialect/postgresql/test_dialect.py index e4b247713..c96e79c13 100644 --- a/test/dialect/postgresql/test_dialect.py +++ b/test/dialect/postgresql/test_dialect.py @@ -1,23 +1,20 @@ # coding: utf-8 -from sqlalchemy.testing.assertions import eq_, assert_raises, \ - assert_raises_message, AssertsExecutionResults, \ - AssertsCompiledSQL +from sqlalchemy.testing.assertions import ( + eq_, assert_raises, assert_raises_message, AssertsExecutionResults, + AssertsCompiledSQL) from sqlalchemy.testing import engines, fixtures from sqlalchemy import testing import datetime -from sqlalchemy import Table, Column, select, MetaData, text, Integer, \ - String, Sequence, ForeignKey, join, Numeric, \ - PrimaryKeyConstraint, DateTime, tuple_, Float, BigInteger, \ - func, literal_column, literal, bindparam, cast, extract, \ - SmallInteger, Enum, REAL, update, insert, Index, delete, \ - and_, Date, TypeDecorator, Time, Unicode, Interval, or_, Text +from sqlalchemy import ( + Table, Column, select, MetaData, text, Integer, String, Sequence, Numeric, + DateTime, BigInteger, func, extract, SmallInteger) from sqlalchemy import exc, schema from sqlalchemy.dialects.postgresql import base as postgresql import logging import logging.handlers from sqlalchemy.testing.mock import Mock -from sqlalchemy.engine.reflection import Inspector + class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL): @@ -26,9 +23,9 @@ class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL): @testing.provide_metadata def test_date_reflection(self): metadata = self.metadata - t1 = Table('pgdate', metadata, Column('date1', - DateTime(timezone=True)), Column('date2', - DateTime(timezone=False))) + Table( + 'pgdate', metadata, Column('date1', DateTime(timezone=True)), + Column('date2', DateTime(timezone=False))) metadata.create_all() m2 = MetaData(testing.db) t2 = Table('pgdate', m2, autoload=True) @@ -41,24 +38,23 @@ class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL): def mock_conn(res): return Mock( - execute=Mock( - return_value=Mock(scalar=Mock(return_value=res)) - ) - ) - - for string, version in \ - [('PostgreSQL 8.3.8 on i686-redhat-linux-gnu, compiled by ' - 'GCC gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)', (8, 3, - 8)), - ('PostgreSQL 8.5devel on x86_64-unknown-linux-gnu, ' - 'compiled by GCC gcc (GCC) 4.4.2, 64-bit', (8, 5)), - ('EnterpriseDB 9.1.2.2 on x86_64-unknown-linux-gnu, ' - 'compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-50), ' - '64-bit', (9, 1, 2)), - ('[PostgreSQL 9.2.4 ] VMware vFabric Postgres 9.2.4.0 ' - 'release build 1080137', (9, 2, 4)) - - ]: + execute=Mock(return_value=Mock(scalar=Mock(return_value=res)))) + + for string, version in [ + ( + 'PostgreSQL 8.3.8 on i686-redhat-linux-gnu, compiled by ' + 'GCC gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)', + (8, 3, 8)), + ( + 'PostgreSQL 8.5devel on x86_64-unknown-linux-gnu, ' + 'compiled by GCC gcc (GCC) 4.4.2, 64-bit', (8, 5)), + ( + 'EnterpriseDB 9.1.2.2 on x86_64-unknown-linux-gnu, ' + 'compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-50), ' + '64-bit', (9, 1, 2)), + ( + '[PostgreSQL 9.2.4 ] VMware vFabric Postgres 9.2.4.0 ' + 'release build 1080137', (9, 2, 4))]: eq_(testing.db.dialect._get_server_version_info(mock_conn(string)), version) @@ -66,7 +62,7 @@ class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL): def test_psycopg2_version(self): v = testing.db.dialect.psycopg2_version assert testing.db.dialect.dbapi.__version__.\ - startswith(".".join(str(x) for x in v)) + startswith(".".join(str(x) for x in v)) # currently not passing with pg 9.3 that does not seem to generate # any notices here, would rather find a way to mock this @@ -105,31 +101,34 @@ class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL): else: test_encoding = 'UTF8' - e = engines.testing_engine( - options={'client_encoding':test_encoding} - ) + e = engines.testing_engine(options={'client_encoding': test_encoding}) c = e.connect() eq_(c.connection.connection.encoding, test_encoding) - @testing.only_on('postgresql+psycopg2', 'psycopg2-specific feature') + @testing.only_on( + ['postgresql+psycopg2', 'postgresql+pg8000'], + 'psycopg2 / pg8000 - specific feature') @engines.close_open_connections def test_autocommit_isolation_level(self): - extensions = __import__('psycopg2.extensions').extensions - - c = testing.db.connect() - c = c.execution_options(isolation_level='AUTOCOMMIT') - eq_(c.connection.connection.isolation_level, - extensions.ISOLATION_LEVEL_AUTOCOMMIT) + c = testing.db.connect().execution_options( + isolation_level='AUTOCOMMIT') + # If we're really in autocommit mode then we'll get an error saying + # that the prepared transaction doesn't exist. Otherwise, we'd + # get an error saying that the command can't be run within a + # transaction. + assert_raises_message( + exc.ProgrammingError, + 'prepared transaction with identifier "gilberte" does not exist', + c.execute, "commit prepared 'gilberte'") @testing.fails_on('+zxjdbc', "Can't infer the SQL type to use for an instance " "of org.python.core.PyObjectDerived.") - @testing.fails_on('+pg8000', "Can't determine correct type.") def test_extract(self): fivedaysago = datetime.datetime.now() \ - datetime.timedelta(days=5) - for field, exp in ('year', fivedaysago.year), ('month', - fivedaysago.month), ('day', fivedaysago.day): + for field, exp in ('year', fivedaysago.year), \ + ('month', fivedaysago.month), ('day', fivedaysago.day): r = testing.db.execute(select([extract(field, func.now() + datetime.timedelta(days=-5))])).scalar() eq_(r, exp) @@ -157,13 +156,13 @@ class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL): users.insert().execute(id=2, name='name2') users.insert().execute(id=3, name='name3') users.insert().execute(id=4, name='name4') - eq_(users.select().where(users.c.name == 'name2' - ).execute().fetchall(), [(2, 'name2')]) + eq_(users.select().where(users.c.name == 'name2') + .execute().fetchall(), [(2, 'name2')]) eq_(users.select(use_labels=True).where(users.c.name == 'name2').execute().fetchall(), [(2, 'name2')]) users.delete().where(users.c.id == 3).execute() - eq_(users.select().where(users.c.name == 'name3' - ).execute().fetchall(), []) + eq_(users.select().where(users.c.name == 'name3') + .execute().fetchall(), []) users.update().where(users.c.name == 'name4' ).execute(name='newname') eq_(users.select(use_labels=True).where(users.c.id @@ -196,16 +195,16 @@ class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL): finally: testing.db.execute('drop table speedy_users') - @testing.fails_on('+zxjdbc', 'psycopg2/pg8000 specific assertion') @testing.fails_on('pypostgresql', 'psycopg2/pg8000 specific assertion') def test_numeric_raise(self): - stmt = text("select cast('hi' as char) 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) - @testing.only_if("postgresql >= 8.2", "requires standard_conforming_strings") + @testing.only_if( + "postgresql >= 8.2", "requires standard_conforming_strings") def test_serial_integer(self): for version, type_, expected in [ @@ -227,12 +226,8 @@ class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL): else: dialect = testing.db.dialect - ddl_compiler = dialect.ddl_compiler( - dialect, - schema.CreateTable(t) - ) + ddl_compiler = dialect.ddl_compiler(dialect, schema.CreateTable(t)) eq_( ddl_compiler.get_column_specification(t.c.c), "c %s NOT NULL" % expected ) - diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py index b94cc040a..1bfa8c4b7 100644 --- a/test/dialect/postgresql/test_types.py +++ b/test/dialect/postgresql/test_types.py @@ -108,9 +108,6 @@ class EnumTest(fixtures.TestBase, AssertsExecutionResults): @testing.fails_on('postgresql+zxjdbc', 'zxjdbc fails on ENUM: column "XXX" is of type ' 'XXX but expression is of type character varying') - @testing.fails_on('postgresql+pg8000', - 'zxjdbc fails on ENUM: column "XXX" is of type ' - 'XXX but expression is of type text') def test_create_table(self): metadata = MetaData(testing.db) t1 = Table('table', metadata, Column('id', Integer, @@ -138,9 +135,6 @@ class EnumTest(fixtures.TestBase, AssertsExecutionResults): @testing.fails_on('postgresql+zxjdbc', 'zxjdbc fails on ENUM: column "XXX" is of type ' 'XXX but expression is of type character varying') - @testing.fails_on('postgresql+pg8000', - 'zxjdbc fails on ENUM: column "XXX" is of type ' - 'XXX but expression is of type text') @testing.provide_metadata def test_unicode_labels(self): metadata = self.metadata diff --git a/test/requirements.py b/test/requirements.py index d6b1d3969..09f973543 100644 --- a/test/requirements.py +++ b/test/requirements.py @@ -578,8 +578,6 @@ class DefaultRequirements(SuiteRequirements): ('oracle', None, None, "this may be a bug due to the difficulty in handling " "oracle precision numerics"), - ('postgresql+pg8000', None, None, - "pg-8000 does native decimal but truncates the decimals."), ("firebird", None, None, "database and/or driver truncates decimal places.") ] @@ -610,8 +608,11 @@ class DefaultRequirements(SuiteRequirements): ), ('mssql+pymssql', None, None, 'mssql+pymssql has FP inaccuracy even with ' - 'only four decimal places ' - ) + 'only four decimal places '), + ( + 'postgresql+pg8000', None, None, + 'postgresql+pg8000 has FP inaccuracy even with ' + 'only four decimal places '), ]) @property |