4
4
5
5
import re
6
6
from base64 import b64decode
7
+ from datetime import timedelta
8
+ from functools import partial
7
9
from unittest import mock
8
10
9
11
import pytest
@@ -301,7 +303,7 @@ def __call__(self, request):
301
303
def test_display_report_stream_slices_full_refresh (config ):
302
304
profiles = make_profiles ()
303
305
stream = SponsoredDisplayReportStream (config , profiles , authenticator = mock .MagicMock ())
304
- slices = stream .stream_slices (SyncMode .full_refresh , cursor_field = stream .cursor_field )
306
+ slices = list ( stream .stream_slices (SyncMode .full_refresh , cursor_field = stream .cursor_field ) )
305
307
assert slices == [{"profile" : profiles [0 ], "reportDate" : "20210729" }]
306
308
307
309
@@ -311,7 +313,7 @@ def test_display_report_stream_slices_incremental(config):
311
313
profiles = make_profiles ()
312
314
stream = SponsoredDisplayReportStream (config , profiles , authenticator = mock .MagicMock ())
313
315
stream_state = {str (profiles [0 ].profileId ): {"reportDate" : "20210725" }}
314
- slices = stream .stream_slices (SyncMode .incremental , cursor_field = stream .cursor_field , stream_state = stream_state )
316
+ slices = list ( stream .stream_slices (SyncMode .incremental , cursor_field = stream .cursor_field , stream_state = stream_state ) )
315
317
assert slices == [
316
318
{"profile" : profiles [0 ], "reportDate" : "20210725" },
317
319
{"profile" : profiles [0 ], "reportDate" : "20210726" },
@@ -321,13 +323,13 @@ def test_display_report_stream_slices_incremental(config):
321
323
]
322
324
323
325
stream_state = {str (profiles [0 ].profileId ): {"reportDate" : "20210730" }}
324
- slices = stream .stream_slices (SyncMode .incremental , cursor_field = stream .cursor_field , stream_state = stream_state )
326
+ slices = list ( stream .stream_slices (SyncMode .incremental , cursor_field = stream .cursor_field , stream_state = stream_state ) )
325
327
assert slices == [None ]
326
328
327
- slices = stream .stream_slices (SyncMode .incremental , cursor_field = stream .cursor_field , stream_state = {})
329
+ slices = list ( stream .stream_slices (SyncMode .incremental , cursor_field = stream .cursor_field , stream_state = {}) )
328
330
assert slices == [{"profile" : profiles [0 ], "reportDate" : "20210729" }]
329
331
330
- slices = stream .stream_slices (SyncMode .incremental , cursor_field = None , stream_state = {})
332
+ slices = list ( stream .stream_slices (SyncMode .incremental , cursor_field = None , stream_state = {}) )
331
333
assert slices == [{"profile" : profiles [0 ], "reportDate" : "20210729" }]
332
334
333
335
@@ -358,5 +360,56 @@ def test_stream_slices_different_timezones(config):
358
360
profile1 = Profile (profileId = 1 , timezone = "America/Los_Angeles" , accountInfo = AccountInfo (marketplaceStringId = "" , id = "" , type = "seller" ))
359
361
profile2 = Profile (profileId = 2 , timezone = "UTC" , accountInfo = AccountInfo (marketplaceStringId = "" , id = "" , type = "seller" ))
360
362
stream = SponsoredProductsReportStream (config , [profile1 , profile2 ], authenticator = mock .MagicMock ())
361
- slices = stream .stream_slices (SyncMode .incremental , cursor_field = stream .cursor_field , stream_state = {})
363
+ slices = list ( stream .stream_slices (SyncMode .incremental , cursor_field = stream .cursor_field , stream_state = {}) )
362
364
assert slices == [{"profile" : profile1 , "reportDate" : "20210731" }, {"profile" : profile2 , "reportDate" : "20210801" }]
365
+
366
+
367
+ def test_stream_slices_lazy_evaluation (config ):
368
+ with freeze_time ("2022-06-01T23:50:00+00:00" ) as frozen_datetime :
369
+ config ["start_date" ] = "2021-05-10"
370
+ profile1 = Profile (profileId = 1 , timezone = "UTC" , accountInfo = AccountInfo (marketplaceStringId = "" , id = "" , type = "seller" ))
371
+ profile2 = Profile (profileId = 2 , timezone = "UTC" , accountInfo = AccountInfo (marketplaceStringId = "" , id = "" , type = "seller" ))
372
+
373
+ stream = SponsoredProductsReportStream (config , [profile1 , profile2 ], authenticator = mock .MagicMock ())
374
+ stream .REPORTING_PERIOD = 5
375
+
376
+ slices = []
377
+ for _slice in stream .stream_slices (SyncMode .incremental , cursor_field = stream .cursor_field ):
378
+ slices .append (_slice )
379
+ frozen_datetime .tick (delta = timedelta (minutes = 10 ))
380
+
381
+ assert slices == [
382
+ {"profile" : profile1 , "reportDate" : "20220527" },
383
+ {"profile" : profile2 , "reportDate" : "20220528" },
384
+ {"profile" : profile1 , "reportDate" : "20220528" },
385
+ {"profile" : profile2 , "reportDate" : "20220529" },
386
+ {"profile" : profile1 , "reportDate" : "20220529" },
387
+ {"profile" : profile2 , "reportDate" : "20220530" },
388
+ {"profile" : profile1 , "reportDate" : "20220530" },
389
+ {"profile" : profile2 , "reportDate" : "20220531" },
390
+ {"profile" : profile1 , "reportDate" : "20220531" },
391
+ {"profile" : profile2 , "reportDate" : "20220601" },
392
+ {"profile" : profile1 , "reportDate" : "20220601" },
393
+ {"profile" : profile2 , "reportDate" : "20220602" },
394
+ {"profile" : profile1 , "reportDate" : "20220602" },
395
+ ]
396
+
397
+
398
+ def test_get_date_range_lazy_evaluation ():
399
+ get_date_range = partial (SponsoredProductsReportStream .get_date_range , SponsoredProductsReportStream )
400
+
401
+ with freeze_time ("2022-06-01T12:00:00+00:00" ) as frozen_datetime :
402
+ date_range = list (get_date_range (start_date = Date (2022 , 5 , 29 ), timezone = "UTC" ))
403
+ assert date_range == ["20220529" , "20220530" , "20220531" , "20220601" ]
404
+
405
+ date_range = list (get_date_range (start_date = Date (2022 , 6 , 1 ), timezone = "UTC" ))
406
+ assert date_range == ["20220601" ]
407
+
408
+ date_range = list (get_date_range (start_date = Date (2022 , 6 , 2 ), timezone = "UTC" ))
409
+ assert date_range == []
410
+
411
+ date_range = []
412
+ for date in get_date_range (start_date = Date (2022 , 5 , 29 ), timezone = "UTC" ):
413
+ date_range .append (date )
414
+ frozen_datetime .tick (delta = timedelta (hours = 3 ))
415
+ assert date_range == ["20220529" , "20220530" , "20220531" , "20220601" , "20220602" ]
0 commit comments