22import uuid
33import pytest
44import azure .cosmos .cosmos_client as cosmos_client
5- import azure .cosmos .documents as documents
5+ import azure .cosmos .retry_utility as retry_utility
66import test .test_config as test_config
77
88@pytest .mark .usefixtures ("teardown" )
@@ -155,5 +155,40 @@ def test_populate_query_metrics (self):
155155 self .assertTrue (len (metrics ) > 1 )
156156 self .assertTrue (all (['=' in x for x in metrics ]))
157157
158+ def test_max_item_count_honored_in_order_by_query (self ):
159+ created_collection = self .config .create_multi_partition_collection_with_custom_pk_if_not_exist (self .client )
160+ docs = []
161+ for i in range (10 ):
162+ document_definition = {'pk' : 'pk' , 'id' : 'myId' + str (uuid .uuid4 ())}
163+ docs .append (self .client .CreateItem (created_collection ['_self' ], document_definition ))
164+
165+ query = 'SELECT * from c ORDER BY c._ts'
166+ query_options = {'enableCrossPartitionQuery' : True ,
167+ 'maxItemCount' : 1 }
168+ query_iterable = self .client .QueryItems (created_collection ['_self' ], query , query_options )
169+ #1 call to get query plans, 1 call to get pkr, 10 calls to one partion with the documents, 1 call each to other 4 partitions
170+ self .validate_query_requests_count (query_iterable , 16 * 2 )
171+
172+ query_options ['maxItemCount' ] = 100
173+ query_iterable = self .client .QueryItems (created_collection ['_self' ], query , query_options )
174+ # 1 call to get query plan 1 calls to one partition with the documents, 1 call each to other 4 partitions
175+ self .validate_query_requests_count (query_iterable , 6 * 2 )
176+
177+ def validate_query_requests_count (self , query_iterable , expected_count ):
178+ self .count = 0
179+ self .OriginalExecuteFunction = retry_utility ._ExecuteFunction
180+ retry_utility ._ExecuteFunction = self ._MockExecuteFunction
181+ block = query_iterable .fetch_next_block ()
182+ while block :
183+ block = query_iterable .fetch_next_block ()
184+ retry_utility ._ExecuteFunction = self .OriginalExecuteFunction
185+ self .assertEquals (self .count , expected_count )
186+ self .count = 0
187+
188+ def _MockExecuteFunction (self , function , * args , ** kwargs ):
189+ self .count += 1
190+ return self .OriginalExecuteFunction (function , * args , ** kwargs )
191+
192+
158193if __name__ == "__main__" :
159194 unittest .main ()
0 commit comments