Skip to content

Commit 2b43110

Browse files
authored
Fixes to start time for change feed query (#35500)
1 parent 3727b87 commit 2b43110

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

sdk/cosmos/azure-cosmos/azure/cosmos/aio/_container.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,8 @@ def query_items_change_feed(
487487
488488
:keyword bool is_start_from_beginning: Get whether change feed should start from
489489
beginning (true) or from current (false). By default, it's start from current (false).
490-
:keyword datetime start_time: Specifies a point of time to start change feed. Start time in
491-
'%a, %d %b %Y %H:%M:%S GMT' format. Converts datetime to UTC regardless of timezone.
490+
:keyword ~datetime.datetime start_time: Specifies a point of time to start change feed. Provided value will be
491+
converted to UTC. This value will be ignored if `is_start_from_beginning` is set to true.
492492
:keyword str partition_key_range_id: ChangeFeed requests can be executed against specific partition key
493493
ranges. This is used to process the change feed in parallel across multiple consumers.
494494
:keyword str continuation: e_tag value to be used as continuation for reading change feed.
@@ -508,7 +508,7 @@ def query_items_change_feed(
508508
kwargs['priority'] = priority
509509
feed_options = _build_options(kwargs)
510510
feed_options["isStartFromBeginning"] = is_start_from_beginning
511-
if start_time is not None and is_start_from_beginning is False and isinstance(start_time, datetime):
511+
if start_time is not None and is_start_from_beginning is False:
512512
feed_options["startTime"] = start_time.astimezone(timezone.utc).strftime('%a, %d %b %Y %H:%M:%S GMT')
513513
if partition_key_range_id is not None:
514514
feed_options["partitionKeyRangeId"] = partition_key_range_id

sdk/cosmos/azure-cosmos/azure/cosmos/container.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ def query_items_change_feed(
307307
self,
308308
partition_key_range_id: Optional[str] = None,
309309
is_start_from_beginning: bool = False,
310-
start_time: Optional[datetime] = None,
311310
continuation: Optional[str] = None,
312311
max_item_count: Optional[int] = None,
313312
*,
313+
start_time: Optional[datetime] = None,
314314
partition_key: Optional[PartitionKeyType] = None,
315315
priority: Optional[Literal["High", "Low"]] = None,
316316
**kwargs: Any
@@ -321,11 +321,11 @@ def query_items_change_feed(
321321
This is used to process the change feed in parallel across multiple consumers.
322322
:param bool is_start_from_beginning: Get whether change feed should start from
323323
beginning (true) or from current (false). By default, it's start from current (false).
324-
:param datetime start_time: Specifies a point of time to start change feed. Start time in
325-
'%a, %d %b %Y %H:%M:%S GMT' format. Converts datetime to UTC regardless of timezone.
326324
:param max_item_count: Max number of items to be returned in the enumeration operation.
327325
:param str continuation: e_tag value to be used as continuation for reading change feed.
328326
:param int max_item_count: Max number of items to be returned in the enumeration operation.
327+
:keyword ~datetime.datetime start_time: Specifies a point of time to start change feed. Provided value will be
328+
converted to UTC. This value will be ignored if `is_start_from_beginning` is set to true.
329329
:keyword partition_key: partition key at which ChangeFeed requests are targeted.
330330
:paramtype partition_key: Union[str, int, float, bool, List[Union[str, int, float, bool]]]
331331
:keyword Callable response_hook: A callable invoked with the response metadata.
@@ -345,7 +345,7 @@ def query_items_change_feed(
345345
feed_options["partitionKey"] = self._set_partition_key(partition_key)
346346
if is_start_from_beginning is not None:
347347
feed_options["isStartFromBeginning"] = is_start_from_beginning
348-
if start_time is not None and is_start_from_beginning is False and isinstance(start_time, datetime):
348+
if start_time is not None and is_start_from_beginning is False:
349349
feed_options["startTime"] = start_time.astimezone(timezone.utc).strftime('%a, %d %b %Y %H:%M:%S GMT')
350350
if max_item_count is not None:
351351
feed_options["maxItemCount"] = max_item_count

sdk/cosmos/azure-cosmos/test/test_query.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,13 @@ def create_random_items(container, batch_size):
337337
# Should equal batch size
338338
self.assertEqual(totalCount, batchSize)
339339

340-
# test an invalid value, will ignore start time option
340+
# test an invalid value, Attribute error will be raised for passing non datetime object
341341
invalid_time = "Invalid value"
342-
change_feed_iter = list(created_collection.query_items_change_feed(start_time=invalid_time))
343-
totalCount = len(change_feed_iter)
344-
# Should not equal batch size
345-
self.assertNotEqual(totalCount, batchSize)
342+
try:
343+
change_feed_iter = list(created_collection.query_items_change_feed(start_time=invalid_time))
344+
self.fail("Cannot format date on a non datetime object.")
345+
except AttributeError as e:
346+
self.assertTrue("'str' object has no attribute 'astimezone'" == e.args[0])
346347

347348
def test_populate_query_metrics(self):
348349
created_collection = self.created_db.create_container("query_metrics_test",

sdk/cosmos/azure-cosmos/test/test_query_async.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,13 @@ async def create_random_items(container, batch_size):
379379
# Should equal batch size
380380
assert totalCount == batchSize
381381

382-
# test an invalid value, will ignore start time option
382+
# test an invalid value, Attribute error will be raised for passing non datetime object
383383
invalid_time = "Invalid value"
384-
change_feed_iter = [i async for i in created_collection.query_items_change_feed(start_time=invalid_time)]
385-
totalCount = len(change_feed_iter)
386-
# Should not equal batch size
387-
assert totalCount != batchSize
384+
try:
385+
change_feed_iter = [i async for i in created_collection.query_items_change_feed(start_time=invalid_time)]
386+
self.fail("Cannot format date on a non datetime object.")
387+
except AttributeError as e:
388+
assert ("'str' object has no attribute 'astimezone'" == e.args[0])
388389

389390
await self.created_db.delete_container(created_collection.id)
390391

0 commit comments

Comments
 (0)