File: test_context_query.py

package info (click to toggle)
python-cassandra-driver 3.29.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,144 kB
  • sloc: python: 51,532; ansic: 768; makefile: 136; sh: 13
file content (175 lines) | stat: -rw-r--r-- 5,573 bytes parent folder | download | duplicates (3)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# 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.

from cassandra.cqlengine import columns
from cassandra.cqlengine.management import drop_keyspace, sync_table, create_keyspace_simple
from cassandra.cqlengine.models import Model
from cassandra.cqlengine.query import ContextQuery
from tests.integration.cqlengine.base import BaseCassEngTestCase


class TestModel(Model):

    __keyspace__ = 'ks1'

    partition = columns.Integer(primary_key=True)
    cluster = columns.Integer(primary_key=True)
    count = columns.Integer()
    text = columns.Text()


class ContextQueryTests(BaseCassEngTestCase):

    KEYSPACES = ('ks1', 'ks2', 'ks3', 'ks4')

    @classmethod
    def setUpClass(cls):
        super(ContextQueryTests, cls).setUpClass()
        for ks in cls.KEYSPACES:
            create_keyspace_simple(ks, 1)
        sync_table(TestModel, keyspaces=cls.KEYSPACES)

    @classmethod
    def tearDownClass(cls):
        super(ContextQueryTests, cls).tearDownClass()
        for ks in cls.KEYSPACES:
            drop_keyspace(ks)


    def setUp(self):
        super(ContextQueryTests, self).setUp()
        for ks in self.KEYSPACES:
            with ContextQuery(TestModel, keyspace=ks) as tm:
                for obj in tm.all():
                    obj.delete()

    def test_context_manager(self):
        """
        Validates that when a context query is constructed that the
        keyspace of the returned model is toggled appropriately

        @since 3.6
        @jira_ticket PYTHON-598
        @expected_result default keyspace should be used

        @test_category query
        """
        # model keyspace write/read
        for ks in self.KEYSPACES:
            with ContextQuery(TestModel, keyspace=ks) as tm:
                self.assertEqual(tm.__keyspace__, ks)

        self.assertEqual(TestModel._get_keyspace(), 'ks1')

    def test_default_keyspace(self):
        """
        Tests the use of context queries with the default model keyspsace

        @since 3.6
        @jira_ticket PYTHON-598
        @expected_result default keyspace should be used

        @test_category query
        """
        # model keyspace write/read
        for i in range(5):
            TestModel.objects.create(partition=i, cluster=i)

        with ContextQuery(TestModel) as tm:
            self.assertEqual(5, len(tm.objects.all()))

        with ContextQuery(TestModel, keyspace='ks1') as tm:
            self.assertEqual(5, len(tm.objects.all()))

        for ks in self.KEYSPACES[1:]:
            with ContextQuery(TestModel, keyspace=ks) as tm:
                self.assertEqual(0, len(tm.objects.all()))

    def test_context_keyspace(self):
        """
        Tests the use of context queries with non default keyspaces

        @since 3.6
        @jira_ticket PYTHON-598
        @expected_result queries should be routed to appropriate keyspaces

        @test_category query
        """
        for i in range(5):
            with ContextQuery(TestModel, keyspace='ks4') as tm:
                tm.objects.create(partition=i, cluster=i)

        with ContextQuery(TestModel, keyspace='ks4') as tm:
            self.assertEqual(5, len(tm.objects.all()))

        self.assertEqual(0, len(TestModel.objects.all()))

        for ks in self.KEYSPACES[:2]:
            with ContextQuery(TestModel, keyspace=ks) as tm:
                self.assertEqual(0, len(tm.objects.all()))

        # simple data update
        with ContextQuery(TestModel, keyspace='ks4') as tm:
            obj = tm.objects.get(partition=1)
            obj.update(count=42)

            self.assertEqual(42, tm.objects.get(partition=1).count)

    def test_context_multiple_models(self):
        """
        Tests the use of multiple models with the context manager

        @since 3.7
        @jira_ticket PYTHON-613
        @expected_result all models are properly updated with the context

        @test_category query
        """

        with ContextQuery(TestModel, TestModel, keyspace='ks4') as (tm1, tm2):

            self.assertNotEqual(tm1, tm2)
            self.assertEqual(tm1.__keyspace__, 'ks4')
            self.assertEqual(tm2.__keyspace__, 'ks4')

    def test_context_invalid_parameters(self):
        """
        Tests that invalid parameters are raised by the context manager

        @since 3.7
        @jira_ticket PYTHON-613
        @expected_result a ValueError is raised when passing invalid parameters

        @test_category query
        """

        with self.assertRaises(ValueError):
            with ContextQuery(keyspace='ks2'):
                pass

        with self.assertRaises(ValueError):
            with ContextQuery(42) as tm:
                pass

        with self.assertRaises(ValueError):
            with ContextQuery(TestModel, 42):
                pass

        with self.assertRaises(ValueError):
            with ContextQuery(TestModel, unknown_param=42):
                pass

        with self.assertRaises(ValueError):
            with ContextQuery(TestModel, keyspace='ks2', unknown_param=42):
                pass