diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-01-11 15:22:46 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-01-11 15:22:46 -0500 |
| commit | 67e0f356b2093fdc03303d50be1f89e75e847c7f (patch) | |
| tree | e2209edb3a8aeb16702bc47573b9809a8e521db5 /test/sql/test_defaults.py | |
| parent | 0342a4886f00b34cf02e0d2d986a0896ba946788 (diff) | |
| download | sqlalchemy-67e0f356b2093fdc03303d50be1f89e75e847c7f.tar.gz | |
- A TypeDecorator of Integer can be used with a primary key
column, and the "autoincrement" feature of various dialects
as well as the "sqlite_autoincrement" flag will honor
the underlying database type as being Integer-based.
[ticket:2005]
- Result-row processors are applied to pre-executed SQL
defaults, as well as cursor.lastrowid, when determining
the contents of result.inserted_primary_key.
[ticket:2006]
- Bind parameters present in the "columns clause" of a select
are now auto-labeled like other "anonymous" clauses,
which among other things allows their "type" to be meaningful
when the row is fetched, as in result row processors.
- TypeDecorator is present in the "sqlalchemy" import space.
Diffstat (limited to 'test/sql/test_defaults.py')
| -rw-r--r-- | test/sql/test_defaults.py | 85 |
1 files changed, 82 insertions, 3 deletions
diff --git a/test/sql/test_defaults.py b/test/sql/test_defaults.py index 31759a709..49aa8d3b3 100644 --- a/test/sql/test_defaults.py +++ b/test/sql/test_defaults.py @@ -1,11 +1,12 @@ from test.lib.testing import eq_, assert_raises, assert_raises_message import datetime -from sqlalchemy import Sequence, Column, func from sqlalchemy.schema import CreateSequence, DropSequence -from sqlalchemy.sql import select, text +from sqlalchemy.sql import select, text, literal_column import sqlalchemy as sa from test.lib import testing, engines -from sqlalchemy import MetaData, Integer, String, ForeignKey, Boolean, exc +from sqlalchemy import MetaData, Integer, String, ForeignKey, Boolean, exc,\ + Sequence, Column, func, literal +from sqlalchemy.types import TypeDecorator from test.lib.schema import Table from test.lib.testing import eq_ from test.sql import _base @@ -690,3 +691,81 @@ class SequenceTest(testing.TestBase, testing.AssertsCompiledSQL): metadata.drop_all() +class SpecialTypePKTest(testing.TestBase): + """test process_result_value in conjunction with primary key columns. + + Also tests that "autoincrement" checks are against column.type._type_affinity, + rather than the class of "type" itself. + + """ + + @classmethod + def setup_class(cls): + class MyInteger(TypeDecorator): + impl = Integer + def process_bind_param(self, value, dialect): + return int(value[4:]) + + def process_result_value(self, value, dialect): + return "INT_%d" % value + + cls.MyInteger = MyInteger + + @testing.provide_metadata + def _run_test(self, *arg, **kw): + implicit_returning = kw.pop('implicit_returning', True) + kw['primary_key'] = True + t = Table('x', metadata, + Column('y', self.MyInteger, *arg, **kw), + Column('data', Integer), + implicit_returning=implicit_returning + ) + + t.create() + r = t.insert().values(data=5).execute() + eq_(r.inserted_primary_key, ['INT_1']) + r.close() + + eq_( + t.select().execute().first(), + ('INT_1', 5) + ) + + def test_plain(self): + # among other things, tests that autoincrement + # is enabled. + self._run_test() + + def test_literal_default_label(self): + self._run_test(default=literal("INT_1", type_=self.MyInteger).label('foo')) + + def test_literal_default_no_label(self): + self._run_test(default=literal("INT_1", type_=self.MyInteger)) + + def test_sequence(self): + self._run_test(Sequence('foo_seq')) + + @testing.fails_on('mysql', "Pending [ticket:2021]") + @testing.fails_on('sqlite', "Pending [ticket:2021]") + def test_server_default(self): + # note that the MySQL dialect has to not render AUTOINCREMENT on this one + self._run_test(server_default='1',) + + @testing.fails_on('mysql', "Pending [ticket:2021]") + @testing.fails_on('sqlite', "Pending [ticket:2021]") + def test_server_default_no_autoincrement(self): + self._run_test(server_default='1', autoincrement=False) + + def test_clause(self): + stmt = select([literal("INT_1", type_=self.MyInteger)]).as_scalar() + self._run_test(default=stmt) + + @testing.requires.returning + def test_no_implicit_returning(self): + self._run_test(implicit_returning=False) + + @testing.requires.returning + def test_server_default_no_implicit_returning(self): + self._run_test(server_default='1', autoincrement=False) + + |
