From: Mike Bayer Date: Sat, 22 Sep 2012 20:46:29 +0000 (-0400) Subject: - aaand actually get is/isnot to be usable with None/NULL X-Git-Tag: rel_0_7_9~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e5d143aae3ac928626a0352fcf23e315a4a5605a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - aaand actually get is/isnot to be usable with None/NULL --- diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 0dd95b3369..d5cb1ff628 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -1846,10 +1846,10 @@ class _CompareMixin(ColumnOperators): **kwargs ): if obj is None or isinstance(obj, _Null): - if op == operators.eq: + if op in (operators.eq, operators.is_): return _BinaryExpression(self, null(), operators.is_, negate=operators.isnot) - elif op == operators.ne: + elif op in (operators.ne, operators.isnot): return _BinaryExpression(self, null(), operators.isnot, negate=operators.is_) else: @@ -2959,6 +2959,10 @@ class _Null(ColumnElement): def __init__(self): self.type = sqltypes.NULLTYPE + def compare(self, other): + return isinstance(other, _Null) + + class _False(ColumnElement): """Represent the ``false`` keyword in a SQL statement. diff --git a/test/sql/test_operators.py b/test/sql/test_operators.py index cf20189340..7ea3ade819 100644 --- a/test/sql/test_operators.py +++ b/test/sql/test_operators.py @@ -21,9 +21,8 @@ class DefaultColumnComparatorTest(fixtures.TestBase): compare_to(left) ) - def _do_operate_test(self, operator): + def _do_operate_test(self, operator, right=column('right')): left = column('left') - right = column('right') assert operator(left, right).compare( BinaryExpression(left, right, operator) @@ -38,6 +37,12 @@ class DefaultColumnComparatorTest(fixtures.TestBase): def test_plus(self): self._do_operate_test(operators.add) + def test_is_null(self): + self._do_operate_test(operators.is_, None) + + def test_isnot_null(self): + self._do_operate_test(operators.isnot, None) + def test_is(self): self._do_operate_test(operators.is_)