diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-10-23 16:40:39 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-10-23 16:40:39 -0400 |
| commit | 79c25ede440e80e15c4c789234157d0e9497349c (patch) | |
| tree | e9517ef97e2b0a1ed8426b909135d21c23557772 /test/sql | |
| parent | c01c6baf5715a13d707a4d57717f9ad1bc12027a (diff) | |
| download | sqlalchemy-79c25ede440e80e15c4c789234157d0e9497349c.tar.gz | |
- Added type_coerce(expr, type_) expression element.
Treats the given expression as the given type when evaluating
expressions and processing result rows, but does not
affect the generation of SQL, other than an anonymous label.
Diffstat (limited to 'test/sql')
| -rw-r--r-- | test/sql/test_types.py | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/test/sql/test_types.py b/test/sql/test_types.py index 437d69fff..993843891 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -201,7 +201,63 @@ class UserDefinedTest(TestBase, AssertsCompiledSQL): t.dialect_impl(dialect=pg).impl.__class__, Float().dialect_impl(pg).__class__ ) - + + @testing.provide_metadata + def test_type_coerce(self): + """test ad-hoc usage of custom types with type_coerce().""" + + class MyType(types.TypeDecorator): + impl = String + + def process_bind_param(self, value, dialect): + return value[0:-8] + + def process_result_value(self, value, dialect): + return value + "BIND_OUT" + + t = Table('t', metadata, Column('data', String(50))) + metadata.create_all() + + t.insert().values(data=type_coerce('d1BIND_OUT',MyType)).execute() + + eq_( + select([type_coerce(t.c.data, MyType)]).execute().fetchall(), + [('d1BIND_OUT', )] + ) + + eq_( + select([t.c.data, type_coerce(t.c.data, MyType)]).execute().fetchall(), + [('d1', 'd1BIND_OUT')] + ) + + eq_( + select([t.c.data, type_coerce(t.c.data, MyType)]).\ + where(type_coerce(t.c.data, MyType) == 'd1BIND_OUT').\ + execute().fetchall(), + [('d1', 'd1BIND_OUT')] + ) + + eq_( + select([t.c.data, type_coerce(t.c.data, MyType)]).\ + where(t.c.data == type_coerce('d1BIND_OUT', MyType)).\ + execute().fetchall(), + [('d1', 'd1BIND_OUT')] + ) + + eq_( + select([t.c.data, type_coerce(t.c.data, MyType)]).\ + where(t.c.data == type_coerce(None, MyType)).\ + execute().fetchall(), + [] + ) + + eq_( + select([t.c.data, type_coerce(t.c.data, MyType)]).\ + where(type_coerce(t.c.data, MyType) == None).\ + execute().fetchall(), + [] + ) + @classmethod def setup_class(cls): global users, metadata |
