forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_partition_split_query.py
More file actions
115 lines (96 loc) · 4.59 KB
/
test_partition_split_query.py
File metadata and controls
115 lines (96 loc) · 4.59 KB
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
# The MIT License (MIT)
# Copyright (c) 2021 Microsoft Corporation
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# This test class serves to test partition splits within the query context
import random
import time
import unittest
import uuid
import azure.cosmos.cosmos_client as cosmos_client
import test_config
from azure.cosmos import PartitionKey, DatabaseProxy
from azure.cosmos.exceptions import CosmosClientTimeoutError
def get_test_item():
test_item = {
'id': 'Item_' + str(uuid.uuid4()),
'test_object': True,
'lastName': 'Smith',
'attr1': random.randint(0, 10)
}
return test_item
def run_queries(container, iterations):
ret_list = list()
for i in range(iterations):
curr = str(random.randint(0, 10))
query = 'SELECT * FROM c WHERE c.attr1=' + curr + ' order by c.attr1'
qlist = list(container.query_items(query=query, enable_cross_partition_query=True))
ret_list.append((curr, qlist))
for ret in ret_list:
curr = ret[0]
if len(ret[1]) != 0:
for results in ret[1]:
attr_number = results['attr1']
assert str(attr_number) == curr # verify that all results match their randomly generated attributes
print("validation succeeded for all query results")
# @pytest.mark.cosmosEmulator
class TestPartitionSplitQuery(unittest.TestCase):
database: DatabaseProxy = None
client: cosmos_client.CosmosClient = None
configs = test_config._test_config
host = configs.host
masterKey = configs.masterKey
throughput = 400
TEST_DATABASE_ID = "Python SDK Test Database " + str(uuid.uuid4())
TEST_CONTAINER_ID = "Single Partition Test Collection " + str(uuid.uuid4())
@classmethod
def setUpClass(cls):
cls.client = cosmos_client.CosmosClient(cls.host, cls.masterKey)
cls.database = cls.client.create_database_if_not_exists(id=cls.TEST_DATABASE_ID,
offer_throughput=cls.throughput)
cls.container = cls.database.create_container_if_not_exists(
id=cls.TEST_CONTAINER_ID,
partition_key=PartitionKey(path="/id"))
@classmethod
def tearDownClass(cls):
cls.client.delete_database(cls.TEST_DATABASE_ID)
def test_partition_split_query(self):
for i in range(100):
body = get_test_item()
self.container.create_item(body=body)
start_time = time.time()
print("created items, changing offer to 22k and starting queries")
self.database.replace_throughput(11000)
offer_time = time.time()
print("changed offer to 11k")
print("--------------------------------")
print("now starting queries")
run_queries(self.container, 100) # initial check for queries before partition split
print("initial check succeeded, now reading offer until replacing is done")
offer = self.database.get_throughput()
while True:
if time.time() - start_time > 60 * 20: # timeout test at 20 minutes
raise CosmosClientTimeoutError()
if offer.properties['content'].get('isOfferReplacePending', False):
time.sleep(10)
offer = self.database.get_throughput()
else:
print("offer replaced successfully, took around {} seconds".format(time.time() - offer_time))
run_queries(self.container, 100) # check queries work post partition split
self.assertTrue(offer.offer_throughput > self.throughput)
return
if __name__ == "__main__":
unittest.main()