1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
# Copyright DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from cassandra.cqlengine.operators import *
from uuid import uuid4
from cassandra.cqlengine.management import sync_table, drop_table
from cassandra.cqlengine.operators import IsNotNullOperator
from cassandra.cqlengine.statements import IsNotNull
from cassandra import InvalidRequest
from tests.integration.cqlengine.base import TestQueryUpdateModel, BaseCassEngTestCase
from tests.integration.cqlengine.operators import check_lookup
from tests.integration import greaterthanorequalcass30
class TestWhereOperators(unittest.TestCase):
def test_symbol_lookup(self):
""" tests where symbols are looked up properly """
check_lookup(self, 'EQ', EqualsOperator)
check_lookup(self, 'NE', NotEqualsOperator)
check_lookup(self, 'IN', InOperator)
check_lookup(self, 'GT', GreaterThanOperator)
check_lookup(self, 'GTE', GreaterThanOrEqualOperator)
check_lookup(self, 'LT', LessThanOperator)
check_lookup(self, 'LTE', LessThanOrEqualOperator)
check_lookup(self, 'CONTAINS', ContainsOperator)
check_lookup(self, 'LIKE', LikeOperator)
def test_operator_rendering(self):
""" tests symbols are rendered properly """
self.assertEqual("=", str(EqualsOperator()))
self.assertEqual("!=", str(NotEqualsOperator()))
self.assertEqual("IN", str(InOperator()))
self.assertEqual(">", str(GreaterThanOperator()))
self.assertEqual(">=", str(GreaterThanOrEqualOperator()))
self.assertEqual("<", str(LessThanOperator()))
self.assertEqual("<=", str(LessThanOrEqualOperator()))
self.assertEqual("CONTAINS", str(ContainsOperator()))
self.assertEqual("LIKE", str(LikeOperator()))
class TestIsNotNull(BaseCassEngTestCase):
def test_is_not_null_to_cql(self):
"""
Verify that IsNotNull is converted correctly to CQL
@since 2.5
@jira_ticket PYTHON-968
@expected_result the strings match
@test_category cqlengine
"""
check_lookup(self, 'IS NOT NULL', IsNotNullOperator)
# The * is not expanded because there are no referred fields
self.assertEqual(
str(TestQueryUpdateModel.filter(IsNotNull("text")).limit(2)),
'SELECT * FROM cqlengine_test.test_query_update_model WHERE "text" IS NOT NULL LIMIT 2'
)
# We already know partition so cqlengine doesn't query for it
self.assertEqual(
str(TestQueryUpdateModel.filter(IsNotNull("text"), partition=uuid4())),
('SELECT "cluster", "count", "text", "text_set", '
'"text_list", "text_map" FROM cqlengine_test.test_query_update_model '
'WHERE "text" IS NOT NULL AND "partition" = %(0)s LIMIT 10000')
)
@greaterthanorequalcass30
def test_is_not_null_execution(self):
"""
Verify that CQL statements have correct syntax when executed
If we wanted them to return something meaningful and not a InvalidRequest
we'd have to create an index in search for the column we are using
IsNotNull
@since 2.5
@jira_ticket PYTHON-968
@expected_result InvalidRequest is arisen
@test_category cqlengine
"""
sync_table(TestQueryUpdateModel)
self.addCleanup(drop_table, TestQueryUpdateModel)
# Raises InvalidRequest instead of dse.protocol.SyntaxException
with self.assertRaises(InvalidRequest):
list(TestQueryUpdateModel.filter(IsNotNull("text")))
with self.assertRaises(InvalidRequest):
list(TestQueryUpdateModel.filter(IsNotNull("text"), partition=uuid4()))
|