forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_query_async.py
More file actions
839 lines (723 loc) · 41.5 KB
/
test_query_async.py
File metadata and controls
839 lines (723 loc) · 41.5 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
# The MIT License (MIT)
# Copyright (c) 2022 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.
# IMPORTANT NOTES:
# Most test cases in this file create collections in your Azure Cosmos account.
# Collections are billing entities. By running these test cases, you may incur monetary costs on your account.
# To Run the test, replace the two member fields (masterKey and host) with values
# associated with your Azure Cosmos account.
import unittest
import uuid
import pytest
import azure.cosmos.aio._retry_utility_async as retry_utility
import azure.cosmos.exceptions as exceptions
import test_config
from azure.cosmos import http_constants
from azure.cosmos._execution_context.query_execution_info import _PartitionedQueryExecutionInfo
from azure.cosmos.aio import CosmosClient, DatabaseProxy, ContainerProxy
from azure.cosmos.documents import _DistinctType
from azure.cosmos.partition_key import PartitionKey
@pytest.mark.cosmosEmulator
class TestQueryAsync(unittest.IsolatedAsyncioTestCase):
"""Test to ensure escaping of non-ascii characters from partition key"""
created_db: DatabaseProxy = None
created_container: ContainerProxy = None
client: CosmosClient = None
config = test_config.TestConfig
TEST_CONTAINER_ID = config.TEST_MULTI_PARTITION_CONTAINER_ID
TEST_DATABASE_ID = config.TEST_DATABASE_ID
host = config.host
masterKey = config.masterKey
connectionPolicy = config.connectionPolicy
@classmethod
def setUpClass(cls):
if (cls.masterKey == '[YOUR_KEY_HERE]' or
cls.host == '[YOUR_ENDPOINT_HERE]'):
raise Exception(
"You must specify your Azure Cosmos account values for "
"'masterKey' and 'host' at the top of this class to run the "
"tests.")
async def asyncSetUp(self):
self.client = CosmosClient(self.host, self.masterKey)
self.created_db = self.client.get_database_client(self.TEST_DATABASE_ID)
async def asyncTearDown(self):
await self.client.close()
async def test_first_and_last_slashes_trimmed_for_query_string_async(self):
created_collection = await self.created_db.create_container(
str(uuid.uuid4()), PartitionKey(path="/pk"))
doc_id = 'myId' + str(uuid.uuid4())
document_definition = {'pk': 'pk', 'id': doc_id}
await created_collection.create_item(body=document_definition)
query = 'SELECT * from c'
query_iterable = created_collection.query_items(
query=query,
partition_key='pk'
)
iter_list = [item async for item in query_iterable]
assert iter_list[0]['id'] == doc_id
await self.created_db.delete_container(created_collection.id)
async def test_query_change_feed_with_pk_async(self):
created_collection = await self.created_db.create_container(
"change_feed_test_" + str(uuid.uuid4()),
PartitionKey(path="/pk"))
# The test targets partition #3
partition_key = "pk"
# Read change feed without passing any options
query_iterable = created_collection.query_items_change_feed()
iter_list = [item async for item in query_iterable]
assert len(iter_list) == 0
# Read change feed from current should return an empty list
query_iterable = created_collection.query_items_change_feed(partition_key=partition_key)
iter_list = [item async for item in query_iterable]
assert len(iter_list) == 0
if 'Etag' in created_collection.client_connection.last_response_headers:
assert created_collection.client_connection.last_response_headers['Etag'] != ''
elif 'etag' in created_collection.client_connection.last_response_headers:
assert created_collection.client_connection.last_response_headers['etag'] != ''
else:
self.fail("No Etag or etag found in last response headers")
# Read change feed from beginning should return an empty list
query_iterable = created_collection.query_items_change_feed(
is_start_from_beginning=True,
partition_key=partition_key
)
iter_list = [item async for item in query_iterable]
assert len(iter_list) == 0
if 'Etag' in created_collection.client_connection.last_response_headers:
continuation1 = created_collection.client_connection.last_response_headers['Etag']
elif 'etag' in created_collection.client_connection.last_response_headers:
continuation1 = created_collection.client_connection.last_response_headers['etag']
else:
self.fail("No Etag or etag found in last response headers")
assert continuation1 != ''
# Create a document. Read change feed should return be able to read that document
document_definition = {'pk': 'pk', 'id': 'doc1'}
await created_collection.create_item(body=document_definition)
query_iterable = created_collection.query_items_change_feed(
is_start_from_beginning=True,
partition_key=partition_key
)
iter_list = [item async for item in query_iterable]
assert len(iter_list) == 1
assert iter_list[0]['id'] == 'doc1'
if 'Etag' in created_collection.client_connection.last_response_headers:
continuation2 = created_collection.client_connection.last_response_headers['Etag']
elif 'etag' in created_collection.client_connection.last_response_headers:
continuation2 = created_collection.client_connection.last_response_headers['etag']
else:
self.fail("No Etag or etag found in last response headers")
assert continuation2 != ''
assert continuation2 != continuation1
# Create two new documents. Verify that change feed contains the 2 new documents
# with page size 1 and page size 100
document_definition = {'pk': 'pk', 'id': 'doc2'}
await created_collection.create_item(body=document_definition)
document_definition = {'pk': 'pk', 'id': 'doc3'}
await created_collection.create_item(body=document_definition)
for pageSize in [2, 100]:
# verify iterator
query_iterable = created_collection.query_items_change_feed(
continuation=continuation2,
max_item_count=pageSize,
partition_key=partition_key)
it = query_iterable.__aiter__()
expected_ids = 'doc2.doc3.'
actual_ids = ''
async for item in it:
actual_ids += item['id'] + '.'
assert actual_ids == expected_ids
# verify by_page
# the options is not copied, therefore it need to be restored
query_iterable = created_collection.query_items_change_feed(
continuation=continuation2,
max_item_count=pageSize,
partition_key=partition_key
)
count = 0
expected_count = 2
all_fetched_res = []
pages = query_iterable.by_page()
async for items in await pages.__anext__():
count += 1
all_fetched_res.append(items)
assert count == expected_count
actual_ids = ''
for item in all_fetched_res:
actual_ids += item['id'] + '.'
assert actual_ids == expected_ids
# verify reading change feed from the beginning
query_iterable = created_collection.query_items_change_feed(
is_start_from_beginning=True,
partition_key=partition_key
)
expected_ids = ['doc1', 'doc2', 'doc3']
it = query_iterable.__aiter__()
for i in range(0, len(expected_ids)):
doc = await it.__anext__()
assert doc['id'] == expected_ids[i]
if 'Etag' in created_collection.client_connection.last_response_headers:
continuation3 = created_collection.client_connection.last_response_headers['Etag']
elif 'etag' in created_collection.client_connection.last_response_headers:
continuation3 = created_collection.client_connection.last_response_headers['etag']
else:
self.fail("No Etag or etag found in last response headers")
# verify reading empty change feed
query_iterable = created_collection.query_items_change_feed(
continuation=continuation3,
is_start_from_beginning=True,
partition_key=partition_key
)
iter_list = [item async for item in query_iterable]
assert len(iter_list) == 0
await self.created_db.delete_container(created_collection.id)
# TODO: partition key range id 0 is relative to the way collection is created
@pytest.mark.skip
async def test_query_change_feed_with_pk_range_id_async(self):
created_collection = await self.created_db.create_container("cf_test_" + str(uuid.uuid4()),
PartitionKey(path="/pk"))
# The test targets partition #3
partition_key_range_id = 0
partition_param = {"partition_key_range_id": partition_key_range_id}
# Read change feed without passing any options
query_iterable = created_collection.query_items_change_feed()
iter_list = [item async for item in query_iterable]
assert len(iter_list) == 0
# Read change feed from current should return an empty list
query_iterable = created_collection.query_items_change_feed(**partition_param)
iter_list = [item async for item in query_iterable]
assert len(iter_list) == 0
if 'Etag' in created_collection.client_connection.last_response_headers:
assert created_collection.client_connection.last_response_headers['Etag']
elif 'etag' in created_collection.client_connection.last_response_headers:
assert created_collection.client_connection.last_response_headers['etag']
else:
self.fail("No Etag or etag found in last response headers")
# Read change feed from beginning should return an empty list
query_iterable = created_collection.query_items_change_feed(
is_start_from_beginning=True,
**partition_param
)
iter_list = [item async for item in query_iterable]
assert len(iter_list) == 0
if 'Etag' in created_collection.client_connection.last_response_headers:
continuation1 = created_collection.client_connection.last_response_headers['Etag']
elif 'etag' in created_collection.client_connection.last_response_headers:
continuation1 = created_collection.client_connection.last_response_headers['etag']
else:
self.fail("No Etag or etag found in last response headers")
assert continuation1 != ''
# Create a document. Read change feed should return be able to read that document
document_definition = {'pk': 'pk', 'id': 'doc1'}
await created_collection.create_item(body=document_definition)
query_iterable = created_collection.query_items_change_feed(
is_start_from_beginning=True,
**partition_param
)
iter_list = [item async for item in query_iterable]
assert len(iter_list) == 1
assert iter_list[0]['id'] == 'doc1'
if 'Etag' in created_collection.client_connection.last_response_headers:
continuation2 = created_collection.client_connection.last_response_headers['Etag']
elif 'etag' in created_collection.client_connection.last_response_headers:
continuation2 = created_collection.client_connection.last_response_headers['etag']
else:
self.fail("No Etag or etag found in last response headers")
assert continuation2 != ''
assert continuation2 != continuation1
# Create two new documents. Verify that change feed contains the 2 new documents
# with page size 1 and page size 100
document_definition = {'pk': 'pk', 'id': 'doc2'}
await created_collection.create_item(body=document_definition)
document_definition = {'pk': 'pk', 'id': 'doc3'}
await created_collection.create_item(body=document_definition)
for pageSize in [2, 100]:
# verify iterator
query_iterable = created_collection.query_items_change_feed(
continuation=continuation2,
max_item_count=pageSize,
**partition_param
)
it = query_iterable.__aiter__()
expected_ids = 'doc2.doc3.'
actual_ids = ''
async for item in it:
actual_ids += item['id'] + '.'
assert actual_ids == expected_ids
# verify by_page
# the options is not copied, therefore it need to be restored
query_iterable = created_collection.query_items_change_feed(
continuation=continuation2,
max_item_count=pageSize,
**partition_param
)
count = 0
expected_count = 2
all_fetched_res = []
pages = query_iterable.by_page()
async for items in await pages.__anext__():
count += 1
all_fetched_res.append(items)
assert count == expected_count
actual_ids = ''
for item in all_fetched_res:
actual_ids += item['id'] + '.'
assert actual_ids == expected_ids
# verify reading change feed from the beginning
query_iterable = created_collection.query_items_change_feed(
is_start_from_beginning=True,
**partition_param
)
expected_ids = ['doc1', 'doc2', 'doc3']
it = query_iterable.__aiter__()
for i in range(0, len(expected_ids)):
doc = await it.__anext__()
assert doc['id'] == expected_ids[i]
if 'Etag' in created_collection.client_connection.last_response_headers:
continuation3 = created_collection.client_connection.last_response_headers['Etag']
elif 'etag' in created_collection.client_connection.last_response_headers:
continuation3 = created_collection.client_connection.last_response_headers['etag']
else:
self.fail("No Etag or etag found in last response headers")
# verify reading empty change feed
query_iterable = created_collection.query_items_change_feed(
continuation=continuation3,
is_start_from_beginning=True,
**partition_param
)
iter_list = [item async for item in query_iterable]
assert len(iter_list) == 0
await self.created_db.delete_container(created_collection.id)
async def test_populate_query_metrics_async(self):
created_collection = await self.created_db.create_container(
"query_metrics_test" + str(uuid.uuid4()),
PartitionKey(path="/pk"))
doc_id = 'MyId' + str(uuid.uuid4())
document_definition = {'pk': 'pk', 'id': doc_id}
await created_collection.create_item(body=document_definition)
query = 'SELECT * from c'
query_iterable = created_collection.query_items(
query=query,
partition_key='pk',
populate_query_metrics=True
)
iter_list = [item async for item in query_iterable]
assert iter_list[0]['id'] == doc_id
metrics_header_name = 'x-ms-documentdb-query-metrics'
assert metrics_header_name in created_collection.client_connection.last_response_headers
metrics_header = created_collection.client_connection.last_response_headers[metrics_header_name]
# Validate header is well-formed: "key1=value1;key2=value2;etc"
metrics = metrics_header.split(';')
assert len(metrics) > 1
assert all(['=' in x for x in metrics])
await self.created_db.delete_container(created_collection.id)
async def test_populate_index_metrics(self):
created_collection = await self.created_db.create_container(
"index_metrics_test" + str(uuid.uuid4()),
PartitionKey(path="/pk"))
doc_id = 'MyId' + str(uuid.uuid4())
document_definition = {'pk': 'pk', 'id': doc_id}
await created_collection.create_item(body=document_definition)
query = 'SELECT * from c'
query_iterable = created_collection.query_items(
query=query,
partition_key='pk',
populate_index_metrics=True
)
iter_list = [item async for item in query_iterable]
assert iter_list[0]['id'] == doc_id
index_header_name = http_constants.HttpHeaders.IndexUtilization
assert index_header_name in created_collection.client_connection.last_response_headers
index_metrics = created_collection.client_connection.last_response_headers[index_header_name]
assert index_metrics != {}
expected_index_metrics = {'UtilizedSingleIndexes': [{'FilterExpression': '', 'IndexSpec': '/pk/?',
'FilterPreciseSet': True, 'IndexPreciseSet': True,
'IndexImpactScore': 'High'}],
'PotentialSingleIndexes': [], 'UtilizedCompositeIndexes': [],
'PotentialCompositeIndexes': []}
assert expected_index_metrics == index_metrics
await self.created_db.delete_container(created_collection.id)
# TODO: Need to validate the query request count logic
@pytest.mark.skip
async def test_max_item_count_honored_in_order_by_query_async(self):
created_collection = await self.created_db.create_container(str(uuid.uuid4()),
PartitionKey(path="/pk"))
docs = []
for i in range(10):
document_definition = {'pk': 'pk', 'id': 'myId' + str(uuid.uuid4())}
docs.append(await created_collection.create_item(body=document_definition))
query = 'SELECT * from c ORDER BY c._ts'
query_iterable = created_collection.query_items(
query=query,
max_item_count=1
)
await self.validate_query_requests_count(query_iterable, 25)
query_iterable = created_collection.query_items(
query=query,
max_item_count=100
)
await self.validate_query_requests_count(query_iterable, 5)
await self.created_db.delete_container(created_collection.id)
async def validate_query_requests_count(self, query_iterable, expected_count):
self.count = 0
self.OriginalExecuteFunction = retry_utility.ExecuteFunctionAsync
retry_utility.ExecuteFunctionAsync = self._mock_execute_function
item_pages = query_iterable.by_page()
while True:
try:
page = await item_pages.__anext__()
assert len([item async for item in page]) > 0
except StopAsyncIteration:
break
retry_utility.ExecuteFunctionAsync = self.OriginalExecuteFunction
assert self.count == expected_count
self.count = 0
async def _mock_execute_function(self, function, *args, **kwargs):
self.count += 1
return await self.OriginalExecuteFunction(function, *args, **kwargs)
async def test_get_query_plan_through_gateway_async(self):
created_collection = self.created_db.get_container_client(self.config.TEST_MULTI_PARTITION_CONTAINER_ID)
await self._validate_query_plan(query="Select top 10 value count(c.id) from c",
container_link=created_collection.container_link,
top=10,
order_by=[],
aggregate=['Count'],
select_value=True,
offset=None,
limit=None,
distinct=_DistinctType.NoneType)
await self._validate_query_plan(query="Select * from c order by c._ts offset 5 limit 10",
container_link=created_collection.container_link,
top=None,
order_by=['Ascending'],
aggregate=[],
select_value=False,
offset=5,
limit=10,
distinct=_DistinctType.NoneType)
await self._validate_query_plan(query="Select distinct value c.id from c order by c.id",
container_link=created_collection.container_link,
top=None,
order_by=['Ascending'],
aggregate=[],
select_value=True,
offset=None,
limit=None,
distinct=_DistinctType.Ordered)
async def _validate_query_plan(self, query, container_link, top, order_by, aggregate, select_value, offset, limit,
distinct):
query_plan_dict = await self.client.client_connection._GetQueryPlanThroughGateway(query, container_link)
query_execution_info = _PartitionedQueryExecutionInfo(query_plan_dict)
assert query_execution_info.has_rewritten_query()
assert query_execution_info.has_distinct_type() == (distinct != "None")
assert query_execution_info.get_distinct_type() == distinct
assert query_execution_info.has_top() == (top is not None)
assert query_execution_info.get_top() == top
assert query_execution_info.has_order_by() == (len(order_by) > 0)
assert query_execution_info.get_order_by() == order_by
assert query_execution_info.has_aggregates() == (len(aggregate) > 0)
assert query_execution_info.get_aggregates() == aggregate
assert query_execution_info.has_select_value() == select_value
assert query_execution_info.has_offset() == (offset is not None)
assert query_execution_info.get_offset() == offset
assert query_execution_info.has_limit() == (limit is not None)
assert query_execution_info.get_limit() == limit
async def test_unsupported_queries_async(self):
created_collection = self.created_db.get_container_client(self.config.TEST_MULTI_PARTITION_CONTAINER_ID)
queries = ['SELECT COUNT(1) FROM c', 'SELECT COUNT(1) + 5 FROM c', 'SELECT COUNT(1) + SUM(c) FROM c']
for query in queries:
query_iterable = created_collection.query_items(query=query)
try:
results = [item async for item in query_iterable]
self.fail("query '{}' should have failed".format(query))
except exceptions.CosmosHttpResponseError as e:
assert e.status_code == 400
async def test_query_with_non_overlapping_pk_ranges_async(self):
created_collection = self.created_db.get_container_client(self.config.TEST_MULTI_PARTITION_CONTAINER_ID)
query_iterable = created_collection.query_items("select * from c where c.pk='1' or c.pk='2'")
assert [item async for item in query_iterable] == []
async def test_offset_limit_async(self):
created_collection = await self.created_db.create_container("offset_limit_" + str(uuid.uuid4()),
PartitionKey(path="/pk"))
values = []
for i in range(10):
document_definition = {'pk': i, 'id': 'myId' + str(uuid.uuid4()), 'value': i // 3}
current_document = await created_collection.create_item(body=document_definition)
values.append(current_document['pk'])
await self.config._validate_distinct_offset_limit(
created_collection=created_collection,
query='SELECT DISTINCT c["value"] from c ORDER BY c.pk OFFSET 0 LIMIT 2',
results=[0, 1])
await self.config._validate_distinct_offset_limit(
created_collection=created_collection,
query='SELECT DISTINCT c["value"] from c ORDER BY c.pk OFFSET 2 LIMIT 2',
results=[2, 3])
await self.config._validate_distinct_offset_limit(
created_collection=created_collection,
query='SELECT DISTINCT c["value"] from c ORDER BY c.pk OFFSET 4 LIMIT 3',
results=[])
await self.config._validate_offset_limit(created_collection=created_collection,
query='SELECT * from c ORDER BY c.pk OFFSET 0 LIMIT 5',
results=values[:5])
await self.config._validate_offset_limit(created_collection=created_collection,
query='SELECT * from c ORDER BY c.pk OFFSET 5 LIMIT 10',
results=values[5:])
await self.config._validate_offset_limit(created_collection=created_collection,
query='SELECT * from c ORDER BY c.pk OFFSET 10 LIMIT 5',
results=[])
await self.config._validate_offset_limit(created_collection=created_collection,
query='SELECT * from c ORDER BY c.pk OFFSET 100 LIMIT 1',
results=[])
await self.created_db.delete_container(created_collection.id)
async def test_distinct_async(self):
created_database = self.created_db
distinct_field = 'distinct_field'
pk_field = "pk"
different_field = "different_field"
created_collection = await created_database.create_container(
id='collection with composite index ' + str(uuid.uuid4()),
partition_key=PartitionKey(path="/pk", kind="Hash"),
indexing_policy={
"compositeIndexes": [
[{"path": "/" + pk_field, "order": "ascending"},
{"path": "/" + distinct_field, "order": "ascending"}],
[{"path": "/" + distinct_field, "order": "ascending"},
{"path": "/" + pk_field, "order": "ascending"}]
]
}
)
documents = []
for i in range(5):
j = i
while j > i - 5:
document_definition = {pk_field: i, 'id': str(uuid.uuid4()), distinct_field: j}
documents.append(await created_collection.create_item(body=document_definition))
document_definition = {pk_field: i, 'id': str(uuid.uuid4()), distinct_field: j}
documents.append(await created_collection.create_item(body=document_definition))
document_definition = {pk_field: i, 'id': str(uuid.uuid4())}
documents.append(await created_collection.create_item(body=document_definition))
j -= 1
padded_docs = self.config._pad_with_none(documents, distinct_field)
await self.config._validate_distinct(created_collection=created_collection, # returns {} and is right number
query='SELECT distinct c.%s from c' % distinct_field, # nosec
results=self.config._get_distinct_docs(padded_docs, distinct_field, None,
False),
is_select=True,
fields=[distinct_field])
await self.config._validate_distinct(created_collection=created_collection,
query='SELECT distinct c.%s, c.%s from c' % (distinct_field, pk_field),
# nosec
results=self.config._get_distinct_docs(padded_docs, distinct_field,
pk_field, False),
is_select=True,
fields=[distinct_field, pk_field])
await self.config._validate_distinct(created_collection=created_collection,
query='SELECT distinct value c.%s from c' % distinct_field, # nosec
results=self.config._get_distinct_docs(padded_docs, distinct_field, None,
True),
is_select=True,
fields=[distinct_field])
await self.config._validate_distinct(created_collection=created_collection,
query='SELECT distinct c.%s from c' % different_field, # nosec
results=['None'],
is_select=True,
fields=[different_field])
await created_database.delete_container(created_collection.id)
async def test_distinct_on_different_types_and_field_orders_async(self):
created_collection = await self.created_db.create_container(
id="test-distinct-container-" + str(uuid.uuid4()),
partition_key=PartitionKey("/pk"),
offer_throughput=self.config.THROUGHPUT_FOR_5_PARTITIONS)
payloads = [
{'id': str(uuid.uuid4()), 'f1': 1, 'f2': 'value', 'f3': 100000000000000000, 'f4': [1, 2, '3'],
'f5': {'f6': {'f7': 2}}},
{'id': str(uuid.uuid4()), 'f2': '\'value', 'f4': [1.0, 2, '3'], 'f5': {'f6': {'f7': 2.0}}, 'f1': 1.0,
'f3': 100000000000000000.00},
{'id': str(uuid.uuid4()), 'f3': 100000000000000000.0, 'f5': {'f6': {'f7': 2}}, 'f2': '\'value', 'f1': 1,
'f4': [1, 2.0, '3']}
]
for pay in payloads:
await created_collection.create_item(pay)
await self.config._validate_distinct_on_different_types_and_field_orders(
collection=created_collection,
query="Select distinct value c.f1 from c",
expected_results=[1]
)
await self.config._validate_distinct_on_different_types_and_field_orders(
collection=created_collection,
query="Select distinct value c.f2 from c",
expected_results=['value', '\'value']
)
await self.config._validate_distinct_on_different_types_and_field_orders(
collection=created_collection,
query="Select distinct value c.f2 from c order by c.f2",
expected_results=['value', '\'value']
)
await self.config._validate_distinct_on_different_types_and_field_orders(
collection=created_collection,
query="Select distinct value c.f3 from c",
expected_results=[100000000000000000]
)
await self.config._validate_distinct_on_different_types_and_field_orders(
collection=created_collection,
query="Select distinct value c.f4 from c",
expected_results=[[1, 2, '3']]
)
await self.config._validate_distinct_on_different_types_and_field_orders(
collection=created_collection,
query="Select distinct value c.f5.f6 from c",
expected_results=[{'f7': 2}]
)
await self.config._validate_distinct_on_different_types_and_field_orders(
collection=created_collection,
query="Select distinct c.f1, c.f2, c.f3 from c",
expected_results=[{'f1': 1, 'f2': 'value', 'f3': 100000000000000000},
{'f1': 1.0, 'f2': '\'value', 'f3': 100000000000000000.00}]
)
await self.config._validate_distinct_on_different_types_and_field_orders(
collection=created_collection,
query="Select distinct c.f1, c.f2, c.f3 from c order by c.f1",
expected_results=[{'f1': 1, 'f2': 'value', 'f3': 100000000000000000},
{'f1': 1.0, 'f2': '\'value', 'f3': 100000000000000000.00}]
)
await self.created_db.delete_container(created_collection.id)
async def test_paging_with_continuation_token_async(self):
created_collection = self.created_db.get_container_client(self.config.TEST_MULTI_PARTITION_CONTAINER_ID)
document_definition = {'pk': 'pk', 'id': '1'}
await created_collection.upsert_item(body=document_definition)
document_definition = {'pk': 'pk', 'id': '2'}
await created_collection.upsert_item(body=document_definition)
query = 'SELECT * from c'
query_iterable = created_collection.query_items(
query=query,
partition_key='pk',
max_item_count=1
)
pager = query_iterable.by_page()
await pager.__anext__()
token = pager.continuation_token
second_page = [item async for item in await pager.__anext__()][0]
pager = query_iterable.by_page(token)
second_page_fetched_with_continuation_token = [item async for item in await pager.__anext__()][0]
assert second_page['id'] == second_page_fetched_with_continuation_token['id']
async def test_cross_partition_query_with_continuation_token_async(self):
created_collection = self.created_db.get_container_client(self.config.TEST_MULTI_PARTITION_CONTAINER_ID)
document_definition = {'pk': 'pk1', 'id': str(uuid.uuid4())}
await created_collection.create_item(body=document_definition)
document_definition = {'pk': 'pk2', 'id': str(uuid.uuid4())}
await created_collection.create_item(body=document_definition)
query = 'SELECT * from c'
query_iterable = created_collection.query_items(
query=query,
max_item_count=1)
pager = query_iterable.by_page()
await pager.__anext__()
token = pager.continuation_token
second_page = [item async for item in await pager.__anext__()][0]
pager = query_iterable.by_page(token)
second_page_fetched_with_continuation_token = [item async for item in await pager.__anext__()][0]
assert second_page['id'] == second_page_fetched_with_continuation_token['id']
async def test_value_max_query_async(self):
container = self.created_db.get_container_client(self.config.TEST_MULTI_PARTITION_CONTAINER_ID)
await container.create_item(
{"id": str(uuid.uuid4()), "isComplete": True, "version": 3, "lookupVersion": "console_version"})
await container.create_item(
{"id": str(uuid.uuid4()), "isComplete": True, "version": 2, "lookupVersion": "console_version"})
query = "Select value max(c.version) FROM c where c.isComplete = true and c.lookupVersion = @lookupVersion"
query_results = container.query_items(query, parameters=[
{"name": "@lookupVersion", "value": "console_version"}
])
item_list = [item async for item in query_results]
assert len(item_list) == 1
assert item_list[0] == 3
async def test_continuation_token_size_limit_query_async(self):
container = self.created_db.get_container_client(self.config.TEST_MULTI_PARTITION_CONTAINER_ID)
for i in range(1, 1000):
await container.create_item(body=dict(pk='123', id=str(uuid.uuid4()), some_value=str(i % 3)))
query = "Select * from c where c.some_value='2'"
response_query = container.query_items(query, partition_key='123', max_item_count=100,
continuation_token_limit=1)
pager = response_query.by_page()
await pager.__anext__()
token = pager.continuation_token
# Continuation token size should be below 1kb
assert len(token.encode('utf-8')) <= 1024
await pager.__anext__()
token = pager.continuation_token
# verify a second time
assert len(token.encode('utf-8')) <= 1024
@pytest.mark.cosmosLiveTest
@pytest.mark.skip
async def test_computed_properties_query(self):
computed_properties = [{'name': "cp_lower", 'query': "SELECT VALUE LOWER(c.db_group) FROM c"},
{'name': "cp_power",
'query': "SELECT VALUE POWER(c.val, 2) FROM c"},
{'name': "cp_str_len", 'query': "SELECT VALUE LENGTH(c.stringProperty) FROM c"}]
items = [
{'id': str(uuid.uuid4()), 'pk': 'test', 'val': 5, 'stringProperty': 'prefixOne', 'db_group': 'GroUp1'},
{'id': str(uuid.uuid4()), 'pk': 'test', 'val': 5, 'stringProperty': 'prefixTwo', 'db_group': 'GrOUp1'},
{'id': str(uuid.uuid4()), 'pk': 'test', 'val': 5, 'stringProperty': 'randomWord1', 'db_group': 'GroUp2'},
{'id': str(uuid.uuid4()), 'pk': 'test', 'val': 5, 'stringProperty': 'randomWord2', 'db_group': 'groUp1'},
{'id': str(uuid.uuid4()), 'pk': 'test', 'val': 5, 'stringProperty': 'randomWord3', 'db_group': 'GroUp3'},
{'id': str(uuid.uuid4()), 'pk': 'test', 'val': 5, 'stringProperty': 'randomWord4', 'db_group': 'GrOUP1'},
{'id': str(uuid.uuid4()), 'pk': 'test', 'val': 5, 'stringProperty': 'randomWord5', 'db_group': 'GroUp2'},
{'id': str(uuid.uuid4()), 'pk': 'test', 'val': 0, 'stringProperty': 'randomWord6', 'db_group': 'group1'},
{'id': str(uuid.uuid4()), 'pk': 'test', 'val': 3, 'stringProperty': 'randomWord7', 'db_group': 'group2'},
{'id': str(uuid.uuid4()), 'pk': 'test', 'val': 2, 'stringProperty': 'randomWord8', 'db_group': 'GroUp3'}
]
created_collection = await self.created_db.create_container(
"computed_properties_query_test_" + str(uuid.uuid4()),
PartitionKey(path="/pk"),
computed_properties=computed_properties)
# Create Items
for item in items:
await created_collection.create_item(body=item)
# Check if computed properties were set
container_properties = await created_collection._get_properties()
assert computed_properties == container_properties["computedProperties"]
# Test 0: Negative test, test if using non-existent computed property
queried_items = [q async for q in
created_collection.query_items(query='Select * from c Where c.cp_upper = "GROUP2"',
partition_key="test")]
assert len(queried_items) == 0
# Test 1: Test first computed property
queried_items = [q async for q in
created_collection.query_items(query='Select * from c Where c.cp_lower = "group1"',
partition_key="test")]
assert len(queried_items) == 5
# Test 1 Negative: Test if using non-existent string in group property returns nothing
queried_items = [q async for q in
created_collection.query_items(query='Select * from c Where c.cp_lower = "group4"',
partition_key="test")]
assert len(queried_items) == 0
# Test 2: Test second computed property
queried_items = [q async for q in created_collection.query_items(query='Select * from c Where c.cp_power = 25',
partition_key="test")]
assert len(queried_items) == 7
# Test 2 Negative: Test Non-Existent POWER
queried_items = [q async for q in created_collection.query_items(query='Select * from c Where c.cp_power = 16',
partition_key="test")]
assert len(queried_items) == 0
# Test 3: Test Third Computed Property
queried_items = [q async for q in created_collection.query_items(query='Select * from c Where c.cp_str_len = 9',
partition_key="test")]
assert len(queried_items) == 2
# Test 3 Negative: Test Str length that isn't there
queried_items = [q async for q in created_collection.query_items(query='Select * from c Where c.cp_str_len = 3',
partition_key="test")]
assert len(queried_items) == 0
if __name__ == '__main__':
unittest.main()