12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from unittest import TestCase
15
+ try :
16
+ import unittest2 as unittest
17
+ except ImportError :
18
+ import unittest # noqa
19
+
16
20
from cassandra .cqlengine .operators import *
17
21
22
+ from uuid import uuid4
23
+
24
+ from cassandra .cqlengine .management import sync_table , drop_table
25
+ from cassandra .cqlengine .operators import IsNotNullOperator
26
+ from cassandra .cqlengine .statements import IsNotNull
27
+ from cassandra import InvalidRequest
28
+
29
+ from tests .integration .cqlengine .base import TestQueryUpdateModel , BaseCassEngTestCase
30
+ from tests .integration .cqlengine .operators import check_lookup
31
+ from tests .integration import greaterthanorequalcass30
32
+
18
33
import six
19
34
20
35
21
- class TestWhereOperators (TestCase ):
36
+ class TestWhereOperators (unittest . TestCase ):
22
37
23
38
def test_symbol_lookup (self ):
24
39
""" tests where symbols are looked up properly """
25
40
26
- def check_lookup (symbol , expected ):
27
- op = BaseWhereOperator .get_operator (symbol )
28
- self .assertEqual (op , expected )
29
-
30
- check_lookup ('EQ' , EqualsOperator )
31
- check_lookup ('NE' , NotEqualsOperator )
32
- check_lookup ('IN' , InOperator )
33
- check_lookup ('GT' , GreaterThanOperator )
34
- check_lookup ('GTE' , GreaterThanOrEqualOperator )
35
- check_lookup ('LT' , LessThanOperator )
36
- check_lookup ('LTE' , LessThanOrEqualOperator )
37
- check_lookup ('CONTAINS' , ContainsOperator )
38
- check_lookup ('LIKE' , LikeOperator )
41
+ check_lookup (self , 'EQ' , EqualsOperator )
42
+ check_lookup (self , 'NE' , NotEqualsOperator )
43
+ check_lookup (self , 'IN' , InOperator )
44
+ check_lookup (self , 'GT' , GreaterThanOperator )
45
+ check_lookup (self , 'GTE' , GreaterThanOrEqualOperator )
46
+ check_lookup (self , 'LT' , LessThanOperator )
47
+ check_lookup (self , 'LTE' , LessThanOrEqualOperator )
48
+ check_lookup (self , 'CONTAINS' , ContainsOperator )
49
+ check_lookup (self , 'LIKE' , LikeOperator )
39
50
40
51
def test_operator_rendering (self ):
41
52
""" tests symbols are rendered properly """
@@ -48,3 +59,56 @@ def test_operator_rendering(self):
48
59
self .assertEqual ("<=" , six .text_type (LessThanOrEqualOperator ()))
49
60
self .assertEqual ("CONTAINS" , six .text_type (ContainsOperator ()))
50
61
self .assertEqual ("LIKE" , six .text_type (LikeOperator ()))
62
+
63
+
64
+ class TestIsNotNull (BaseCassEngTestCase ):
65
+ def test_is_not_null_to_cql (self ):
66
+ """
67
+ Verify that IsNotNull is converted correctly to CQL
68
+
69
+ @since 2.5
70
+ @jira_ticket PYTHON-968
71
+ @expected_result the strings match
72
+
73
+ @test_category cqlengine
74
+ """
75
+
76
+ check_lookup (self , 'IS NOT NULL' , IsNotNullOperator )
77
+
78
+ # The * is not expanded because there are no referred fields
79
+ self .assertEqual (
80
+ str (TestQueryUpdateModel .filter (IsNotNull ("text" )).limit (2 )),
81
+ 'SELECT * FROM cqlengine_test.test_query_update_model WHERE "text" IS NOT NULL LIMIT 2'
82
+ )
83
+
84
+ # We already know partition so cqlengine doesn't query for it
85
+ self .assertEqual (
86
+ str (TestQueryUpdateModel .filter (IsNotNull ("text" ), partition = uuid4 ())),
87
+ ('SELECT "cluster", "count", "text", "text_set", '
88
+ '"text_list", "text_map" FROM cqlengine_test.test_query_update_model '
89
+ 'WHERE "text" IS NOT NULL AND "partition" = %(0)s LIMIT 10000' )
90
+ )
91
+
92
+ @greaterthanorequalcass30
93
+ def test_is_not_null_execution (self ):
94
+ """
95
+ Verify that CQL statements have correct syntax when executed
96
+ If we wanted them to return something meaningful and not a InvalidRequest
97
+ we'd have to create an index in search for the column we are using
98
+ IsNotNull
99
+
100
+ @since 2.5
101
+ @jira_ticket PYTHON-968
102
+ @expected_result InvalidRequest is arisen
103
+
104
+ @test_category cqlengine
105
+ """
106
+ sync_table (TestQueryUpdateModel )
107
+ self .addCleanup (drop_table , TestQueryUpdateModel )
108
+
109
+ # Raises InvalidRequest instead of dse.protocol.SyntaxException
110
+ with self .assertRaises (InvalidRequest ):
111
+ list (TestQueryUpdateModel .filter (IsNotNull ("text" )))
112
+
113
+ with self .assertRaises (InvalidRequest ):
114
+ list (TestQueryUpdateModel .filter (IsNotNull ("text" ), partition = uuid4 ()))
0 commit comments