diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-08-14 13:47:58 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-08-14 13:47:58 -0400 |
commit | 05864ab8a6883cd286f27a05f045ef41a7327fcd (patch) | |
tree | 5bda84a6028168f0d0c6edf5a1f12df214acf095 /test/sql/test_operators.py | |
parent | 7ce34c2b34d7122f6972eaf900ba988c19a0bb98 (diff) | |
download | sqlalchemy-05864ab8a6883cd286f27a05f045ef41a7327fcd.tar.gz |
- fix concat() operator, tests
- [feature] Custom unary operators can now be
used by combining operators.custom_op() with
UnaryExpression().
- clean up the operator dispatch system and make it more consistent.
This does change the compiler contract for custom ops.
Diffstat (limited to 'test/sql/test_operators.py')
-rw-r--r-- | test/sql/test_operators.py | 66 |
1 files changed, 64 insertions, 2 deletions
diff --git a/test/sql/test_operators.py b/test/sql/test_operators.py index 02acda0f1..b369dfc11 100644 --- a/test/sql/test_operators.py +++ b/test/sql/test_operators.py @@ -1,8 +1,11 @@ -from test.lib import fixtures +from test.lib import fixtures, testing +from test.lib.testing import assert_raises_message from sqlalchemy.sql import column, desc, asc, literal, collate from sqlalchemy.sql.expression import BinaryExpression, \ - ClauseList, Grouping, _DefaultColumnComparator + ClauseList, Grouping, _DefaultColumnComparator,\ + UnaryExpression from sqlalchemy.sql import operators +from sqlalchemy import exc from sqlalchemy.schema import Column, Table, MetaData from sqlalchemy.types import Integer, TypeEngine, TypeDecorator @@ -54,6 +57,65 @@ class DefaultColumnComparatorTest(fixtures.TestBase): collate(left, right) ) + def test_concat(self): + self._do_operate_test(operators.concat_op) + +class CustomUnaryOperatorTest(fixtures.TestBase, testing.AssertsCompiledSQL): + __dialect__ = 'default' + + def _factorial_fixture(self): + class MyInteger(Integer): + class comparator_factory(Integer.Comparator): + def factorial(self): + return UnaryExpression(self.expr, + modifier=operators.custom_op("!"), + type_=MyInteger) + + def factorial_prefix(self): + return UnaryExpression(self.expr, + operator=operators.custom_op("!!"), + type_=MyInteger) + + return MyInteger + + def test_factorial(self): + col = column('somecol', self._factorial_fixture()) + self.assert_compile( + col.factorial(), + "somecol !" + ) + + def test_double_factorial(self): + col = column('somecol', self._factorial_fixture()) + self.assert_compile( + col.factorial().factorial(), + "somecol ! !" + ) + + def test_factorial_prefix(self): + col = column('somecol', self._factorial_fixture()) + self.assert_compile( + col.factorial_prefix(), + "!! somecol" + ) + + def test_unary_no_ops(self): + assert_raises_message( + exc.CompileError, + "Unary expression has no operator or modifier", + UnaryExpression(literal("x")).compile + ) + + def test_unary_both_ops(self): + assert_raises_message( + exc.CompileError, + "Unary expression does not support operator and " + "modifier simultaneously", + UnaryExpression(literal("x"), + operator=operators.custom_op("x"), + modifier=operators.custom_op("y")).compile + ) + class _CustomComparatorTests(object): def test_override_builtin(self): c1 = Column('foo', self._add_override_factory()) |