-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_concurrent_perpartitioncursor.py
2969 lines (2904 loc) · 126 KB
/
test_concurrent_perpartitioncursor.py
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2024 Airbyte, Inc., all rights reserved.
import copy
from copy import deepcopy
from datetime import datetime, timedelta
from typing import Any, List, Mapping, MutableMapping, Optional, Union
from urllib.parse import unquote
import pytest
from orjson import orjson
from airbyte_cdk.models import (
AirbyteStateBlob,
AirbyteStateMessage,
AirbyteStateType,
AirbyteStreamState,
StreamDescriptor,
)
from airbyte_cdk.sources.declarative.concurrent_declarative_source import (
ConcurrentDeclarativeSource,
)
from airbyte_cdk.test.catalog_builder import CatalogBuilder, ConfiguredAirbyteStreamBuilder
from airbyte_cdk.test.entrypoint_wrapper import EntrypointOutput, read
SUBSTREAM_MANIFEST: MutableMapping[str, Any] = {
"version": "0.51.42",
"type": "DeclarativeSource",
"check": {"type": "CheckStream", "stream_names": ["post_comment_votes"]},
"definitions": {
"basic_authenticator": {
"type": "BasicHttpAuthenticator",
"username": "{{ config['credentials']['email'] + '/token' }}",
"password": "{{ config['credentials']['api_token'] }}",
},
"retriever": {
"type": "SimpleRetriever",
"requester": {
"type": "HttpRequester",
"url_base": "https://api.example.com",
"http_method": "GET",
"authenticator": "#/definitions/basic_authenticator",
},
"record_selector": {
"type": "RecordSelector",
"extractor": {
"type": "DpathExtractor",
"field_path": ["{{ parameters.get('data_path') or parameters['name'] }}"],
},
"schema_normalization": "Default",
},
"paginator": {
"type": "DefaultPaginator",
"page_size_option": {
"type": "RequestOption",
"field_name": "per_page",
"inject_into": "request_parameter",
},
"pagination_strategy": {
"type": "CursorPagination",
"page_size": 100,
"cursor_value": "{{ response.get('next_page', {}) }}",
"stop_condition": "{{ not response.get('next_page', {}) }}",
},
"page_token_option": {"type": "RequestPath"},
},
},
"cursor_incremental_sync": {
"type": "DatetimeBasedCursor",
"cursor_datetime_formats": ["%Y-%m-%dT%H:%M:%SZ", "%Y-%m-%dT%H:%M:%S%z", "%ms"],
"datetime_format": "%Y-%m-%dT%H:%M:%SZ",
"cursor_field": "{{ parameters.get('cursor_field', 'updated_at') }}",
"start_datetime": {"datetime": "{{ config.get('start_date')}}"},
"start_time_option": {
"inject_into": "request_parameter",
"field_name": "start_time",
"type": "RequestOption",
},
},
"posts_stream": {
"type": "DeclarativeStream",
"name": "posts",
"primary_key": ["id"],
"schema_loader": {
"type": "InlineSchemaLoader",
"schema": {
"$schema": "http://json-schema.org/schema#",
"properties": {
"id": {"type": "integer"},
"updated_at": {"type": "string", "format": "date-time"},
"title": {"type": "string"},
"content": {"type": "string"},
},
"type": "object",
},
},
"retriever": {
"type": "SimpleRetriever",
"requester": {
"type": "HttpRequester",
"url_base": "https://api.example.com",
"path": "/community/posts",
"http_method": "GET",
"authenticator": "#/definitions/basic_authenticator",
},
"record_selector": "#/definitions/retriever/record_selector",
"paginator": "#/definitions/retriever/paginator",
},
"incremental_sync": "#/definitions/cursor_incremental_sync",
"$parameters": {
"name": "posts",
"path": "community/posts",
"data_path": "posts",
"cursor_field": "updated_at",
"primary_key": "id",
},
},
"post_comments_stream": {
"type": "DeclarativeStream",
"name": "post_comments",
"primary_key": ["id"],
"schema_loader": {
"type": "InlineSchemaLoader",
"schema": {
"$schema": "http://json-schema.org/schema#",
"properties": {
"id": {"type": "integer"},
"updated_at": {"type": "string", "format": "date-time"},
"post_id": {"type": "integer"},
"comment": {"type": "string"},
},
"type": "object",
},
},
"retriever": {
"type": "SimpleRetriever",
"requester": {
"type": "HttpRequester",
"url_base": "https://api.example.com",
"path": "/community/posts/{{ stream_slice.id }}/comments",
"http_method": "GET",
"authenticator": "#/definitions/basic_authenticator",
},
"record_selector": {
"type": "RecordSelector",
"extractor": {"type": "DpathExtractor", "field_path": ["comments"]},
"record_filter": {
"condition": "{{ record['updated_at'] >= stream_state.get('updated_at', config.get('start_date')) }}"
},
},
"paginator": "#/definitions/retriever/paginator",
"partition_router": {
"type": "SubstreamPartitionRouter",
"parent_stream_configs": [
{
"stream": "#/definitions/posts_stream",
"parent_key": "id",
"partition_field": "id",
"incremental_dependency": True,
}
],
},
},
"incremental_sync": {
"type": "DatetimeBasedCursor",
"cursor_datetime_formats": ["%Y-%m-%dT%H:%M:%SZ", "%Y-%m-%dT%H:%M:%S%z"],
"datetime_format": "%Y-%m-%dT%H:%M:%SZ",
"cursor_field": "{{ parameters.get('cursor_field', 'updated_at') }}",
"start_datetime": {"datetime": "{{ config.get('start_date') }}"},
},
"$parameters": {
"name": "post_comments",
"path": "community/posts/{{ stream_slice.id }}/comments",
"data_path": "comments",
"cursor_field": "updated_at",
"primary_key": "id",
},
},
"post_comment_votes_stream": {
"type": "DeclarativeStream",
"name": "post_comment_votes",
"primary_key": ["id"],
"schema_loader": {
"type": "InlineSchemaLoader",
"schema": {
"$schema": "http://json-schema.org/schema#",
"properties": {
"id": {"type": "integer"},
"created_at": {"type": "string", "format": "date-time"},
"comment_id": {"type": "integer"},
"vote": {"type": "number"},
},
"type": "object",
},
},
"retriever": {
"type": "SimpleRetriever",
"requester": {
"type": "HttpRequester",
"url_base": "https://api.example.com",
"path": "/community/posts/{{ stream_slice.parent_slice.id }}/comments/{{ stream_slice.id }}/votes",
"http_method": "GET",
"authenticator": "#/definitions/basic_authenticator",
},
"record_selector": "#/definitions/retriever/record_selector",
"paginator": "#/definitions/retriever/paginator",
"partition_router": {
"type": "SubstreamPartitionRouter",
"parent_stream_configs": [
{
"stream": "#/definitions/post_comments_stream",
"parent_key": "id",
"partition_field": "id",
"incremental_dependency": True,
"extra_fields": [["updated_at"]],
}
],
},
},
"transformations": [
{
"type": "AddFields",
"fields": [
{
"path": ["comment_updated_at"],
"value_type": "string",
"value": "{{ stream_slice.extra_fields['updated_at'] }}",
},
],
},
],
"incremental_sync": "#/definitions/cursor_incremental_sync",
"$parameters": {
"name": "post_comment_votes",
"path": "community/posts/{{ stream_slice.parent_slice.id }}/comments/{{ stream_slice.id }}/votes",
"data_path": "votes",
"cursor_field": "created_at",
"primary_key": "id",
},
},
},
"streams": [
{"$ref": "#/definitions/posts_stream"},
{"$ref": "#/definitions/post_comments_stream"},
{"$ref": "#/definitions/post_comment_votes_stream"},
],
"concurrency_level": {
"type": "ConcurrencyLevel",
"default_concurrency": "{{ config['num_workers'] or 10 }}",
"max_concurrency": 25,
},
"spec": {
"type": "Spec",
"documentation_url": "https://airbyte.com/#yaml-from-manifest",
"connection_specification": {
"title": "Test Spec",
"type": "object",
"required": ["credentials", "start_date"],
"additionalProperties": False,
"properties": {
"credentials": {
"type": "object",
"required": ["email", "api_token"],
"properties": {
"email": {
"type": "string",
"title": "Email",
"description": "The email for authentication.",
},
"api_token": {
"type": "string",
"airbyte_secret": True,
"title": "API Token",
"description": "The API token for authentication.",
},
},
},
"start_date": {
"type": "string",
"format": "date-time",
"title": "Start Date",
"description": "The date from which to start syncing data.",
},
},
},
},
}
STREAM_NAME = "post_comment_votes"
CONFIG = {
"start_date": "2024-01-01T00:00:01Z",
"credentials": {"email": "email", "api_token": "api_token"},
}
SUBSTREAM_MANIFEST_NO_DEPENDENCY = deepcopy(SUBSTREAM_MANIFEST)
# Disable incremental_dependency
SUBSTREAM_MANIFEST_NO_DEPENDENCY["definitions"]["post_comments_stream"]["retriever"][
"partition_router"
]["parent_stream_configs"][0]["incremental_dependency"] = False
SUBSTREAM_MANIFEST_NO_DEPENDENCY["definitions"]["post_comment_votes_stream"]["retriever"][
"partition_router"
]["parent_stream_configs"][0]["incremental_dependency"] = False
import orjson
import requests_mock
def run_mocked_test(
mock_requests, manifest, config, stream_name, initial_state, expected_records, expected_state
):
"""
Helper function to mock requests, run the test, and verify the results.
Args:
mock_requests (list): List of tuples containing the URL and response data to mock.
manifest (dict): Manifest configuration for the source.
config (dict): Source configuration.
stream_name (str): Name of the stream being tested.
initial_state (dict): Initial state for the stream.
expected_records (list): Expected records to be returned by the stream.
expected_state (dict): Expected state after processing the records.
Raises:
AssertionError: If the test output does not match the expected records or state.
"""
with requests_mock.Mocker() as m:
for url, response in mock_requests:
if response is None:
m.get(url, status_code=404)
else:
m.get(url, json=response)
initial_state = [
AirbyteStateMessage(
type=AirbyteStateType.STREAM,
stream=AirbyteStreamState(
stream_descriptor=StreamDescriptor(name=stream_name, namespace=None),
stream_state=AirbyteStateBlob(initial_state),
),
)
]
output = _run_read(manifest, config, stream_name, initial_state)
# Verify records
assert sorted([r.record.data for r in output.records], key=lambda x: x["id"]) == sorted(
expected_records, key=lambda x: x["id"]
)
# Verify state
final_state = output.state_messages[-1].state.stream.stream_state
assert final_state.__dict__ == expected_state
# Verify that each request was made exactly once
for url, _ in mock_requests:
request_count = len(
[req for req in m.request_history if unquote(req.url) == unquote(url)]
)
assert (
request_count == 1
), f"URL {url} was called {request_count} times, expected exactly once."
def _run_read(
manifest: Mapping[str, Any],
config: Mapping[str, Any],
stream_name: str,
state: Optional[Union[List[AirbyteStateMessage], MutableMapping[str, Any]]] = None,
) -> EntrypointOutput:
source = ConcurrentDeclarativeSource(
source_config=manifest, config=config, catalog=None, state=state
)
output = read(
source,
config,
CatalogBuilder()
.with_stream(ConfiguredAirbyteStreamBuilder().with_name(stream_name))
.build(),
)
return output
# Existing Constants for Dates
START_DATE = "2024-01-01T00:00:01Z" # Start of the sync
POST_1_UPDATED_AT = "2024-01-30T00:00:00Z" # Latest update date for post 1
POST_2_UPDATED_AT = "2024-01-29T00:00:00Z" # Latest update date for post 2
POST_3_UPDATED_AT = "2024-01-28T00:00:00Z" # Latest update date for post 3
COMMENT_9_OLDEST = "2023-01-01T00:00:00Z" # Comment in partition 1 - filtered out due to date
COMMENT_10_UPDATED_AT = "2024-01-25T00:00:00Z" # Latest comment in partition 1
COMMENT_11_UPDATED_AT = "2024-01-24T00:00:00Z" # Comment in partition 1
COMMENT_12_UPDATED_AT = "2024-01-23T00:00:00Z" # Comment in partition 1
COMMENT_20_UPDATED_AT = "2024-01-22T00:00:00Z" # Latest comment in partition 2
COMMENT_21_UPDATED_AT = "2024-01-21T00:00:00Z" # Comment in partition 2
COMMENT_30_UPDATED_AT = "2024-01-09T00:00:00Z" # Latest comment in partition 3
LOOKBACK_WINDOW_DAYS = 1 # Lookback window duration in days
# Votes Date Constants
VOTE_100_CREATED_AT = "2024-01-15T00:00:00Z" # Latest vote in partition 10
VOTE_101_CREATED_AT = "2024-01-14T00:00:00Z" # Second-latest vote in partition 10
VOTE_111_CREATED_AT = "2024-01-13T00:00:00Z" # Latest vote in partition 11
VOTE_200_CREATED_AT = "2024-01-12T00:00:00Z" # Latest vote in partition 20
VOTE_210_CREATED_AT = "2024-01-12T00:00:15Z" # Latest vote in partition 21
VOTE_300_CREATED_AT = "2024-01-10T00:00:00Z" # Latest vote in partition 30
VOTE_300_CREATED_AT_TIMESTAMP = 1704844800000 # Latest vote in partition 30
# Initial State Constants
PARENT_COMMENT_CURSOR_PARTITION_1 = "2023-01-04T00:00:00Z" # Parent comment cursor (partition)
PARENT_POSTS_CURSOR = "2024-01-05T00:00:00Z" # Parent posts cursor (expected in state)
INITIAL_STATE_PARTITION_10_CURSOR = "2024-01-02T00:00:01Z"
INITIAL_STATE_PARTITION_10_CURSOR_TIMESTAMP = 1704153601000
INITIAL_STATE_PARTITION_11_CURSOR = "2024-01-03T00:00:02Z"
INITIAL_STATE_PARTITION_11_CURSOR_TIMESTAMP = 1704240002000
INITIAL_GLOBAL_CURSOR = INITIAL_STATE_PARTITION_11_CURSOR
INITIAL_GLOBAL_CURSOR_DATE = datetime.fromisoformat(
INITIAL_STATE_PARTITION_11_CURSOR.replace("Z", "")
)
LOOKBACK_DATE = (
INITIAL_GLOBAL_CURSOR_DATE - timedelta(days=LOOKBACK_WINDOW_DAYS)
).isoformat() + "Z"
PARTITION_SYNC_START_TIME = "2024-01-02T00:00:00Z"
@pytest.mark.parametrize(
"test_name, manifest, mock_requests, expected_records, initial_state, expected_state",
[
(
"test_incremental_parent_state",
SUBSTREAM_MANIFEST_NO_DEPENDENCY,
[
# Fetch the first page of posts
(
f"https://api.example.com/community/posts?per_page=100&start_time={START_DATE}",
{
"posts": [
{"id": 1, "updated_at": POST_1_UPDATED_AT},
{"id": 2, "updated_at": POST_2_UPDATED_AT},
],
"next_page": f"https://api.example.com/community/posts?per_page=100&start_time={START_DATE}&page=2",
},
),
# Fetch the second page of posts
(
f"https://api.example.com/community/posts?per_page=100&start_time={START_DATE}&page=2",
{"posts": [{"id": 3, "updated_at": POST_3_UPDATED_AT}]},
),
# Fetch the first page of comments for post 1
(
"https://api.example.com/community/posts/1/comments?per_page=100",
{
"comments": [
{
"id": 9,
"post_id": 1,
"updated_at": COMMENT_9_OLDEST, # No requests for comment 9, filtered out due to the date
},
{
"id": 10,
"post_id": 1,
"updated_at": COMMENT_10_UPDATED_AT,
},
{
"id": 11,
"post_id": 1,
"updated_at": COMMENT_11_UPDATED_AT,
},
],
"next_page": "https://api.example.com/community/posts/1/comments?per_page=100&page=2",
},
),
# Fetch the second page of comments for post 1
(
"https://api.example.com/community/posts/1/comments?per_page=100&page=2",
{
"comments": [
{
"id": 12,
"post_id": 1,
"updated_at": COMMENT_12_UPDATED_AT,
}
]
},
),
# Fetch the first page of votes for comment 10 of post 1
(
f"https://api.example.com/community/posts/1/comments/10/votes?per_page=100&start_time={INITIAL_STATE_PARTITION_10_CURSOR}",
{
"votes": [
{
"id": 100,
"comment_id": 10,
"created_at": VOTE_100_CREATED_AT,
}
],
"next_page": f"https://api.example.com/community/posts/1/comments/10/votes?per_page=100&page=2&start_time={INITIAL_STATE_PARTITION_10_CURSOR}",
},
),
# Fetch the second page of votes for comment 10 of post 1
(
f"https://api.example.com/community/posts/1/comments/10/votes?per_page=100&page=2&start_time={INITIAL_STATE_PARTITION_10_CURSOR}",
{
"votes": [
{
"id": 101,
"comment_id": 10,
"created_at": VOTE_101_CREATED_AT,
}
]
},
),
# Fetch the first page of votes for comment 11 of post 1
(
f"https://api.example.com/community/posts/1/comments/11/votes?per_page=100&start_time={INITIAL_STATE_PARTITION_11_CURSOR}",
{
"votes": [
{
"id": 111,
"comment_id": 11,
"created_at": VOTE_111_CREATED_AT,
}
]
},
),
# Fetch the first page of votes for comment 12 of post 1
(
f"https://api.example.com/community/posts/1/comments/12/votes?per_page=100&start_time={LOOKBACK_DATE}",
{"votes": []},
),
# Fetch the first page of comments for post 2
(
"https://api.example.com/community/posts/2/comments?per_page=100",
{
"comments": [
{
"id": 20,
"post_id": 2,
"updated_at": COMMENT_20_UPDATED_AT,
}
],
"next_page": "https://api.example.com/community/posts/2/comments?per_page=100&page=2",
},
),
# Fetch the second page of comments for post 2
(
"https://api.example.com/community/posts/2/comments?per_page=100&page=2",
{
"comments": [
{
"id": 21,
"post_id": 2,
"updated_at": COMMENT_21_UPDATED_AT,
}
]
},
),
# Fetch the first page of votes for comment 20 of post 2
(
f"https://api.example.com/community/posts/2/comments/20/votes?per_page=100&start_time={LOOKBACK_DATE}",
{
"votes": [
{
"id": 200,
"comment_id": 20,
"created_at": VOTE_200_CREATED_AT,
}
]
},
),
# Fetch the first page of votes for comment 21 of post 2
(
f"https://api.example.com/community/posts/2/comments/21/votes?per_page=100&start_time={LOOKBACK_DATE}",
{
"votes": [
{
"id": 210,
"comment_id": 21,
"created_at": VOTE_210_CREATED_AT,
}
]
},
),
# Fetch the first page of comments for post 3
(
"https://api.example.com/community/posts/3/comments?per_page=100",
{
"comments": [
{
"id": 30,
"post_id": 3,
"updated_at": COMMENT_30_UPDATED_AT,
}
]
},
),
# Fetch the first page of votes for comment 30 of post 3
(
f"https://api.example.com/community/posts/3/comments/30/votes?per_page=100&start_time={LOOKBACK_DATE}",
{
"votes": [
{
"id": 300,
"comment_id": 30,
"created_at": VOTE_300_CREATED_AT_TIMESTAMP,
}
]
},
),
],
# Expected records
[
{
"comment_id": 10,
"comment_updated_at": COMMENT_10_UPDATED_AT,
"created_at": VOTE_100_CREATED_AT,
"id": 100,
},
{
"comment_id": 10,
"comment_updated_at": COMMENT_10_UPDATED_AT,
"created_at": VOTE_101_CREATED_AT,
"id": 101,
},
{
"comment_id": 11,
"comment_updated_at": COMMENT_11_UPDATED_AT,
"created_at": VOTE_111_CREATED_AT,
"id": 111,
},
{
"comment_id": 20,
"comment_updated_at": COMMENT_20_UPDATED_AT,
"created_at": VOTE_200_CREATED_AT,
"id": 200,
},
{
"comment_id": 21,
"comment_updated_at": COMMENT_21_UPDATED_AT,
"created_at": VOTE_210_CREATED_AT,
"id": 210,
},
{
"comment_id": 30,
"comment_updated_at": COMMENT_30_UPDATED_AT,
"created_at": str(VOTE_300_CREATED_AT_TIMESTAMP),
"id": 300,
},
],
# Initial state
{
# This should not happen since parent state is disabled, but I've added this to validate that and
# incoming parent_state is ignored when the parent stream's incremental_dependency is disabled
"parent_state": {
"post_comments": {
"states": [
{
"partition": {"id": 1, "parent_slice": {}},
"cursor": {"updated_at": PARENT_COMMENT_CURSOR_PARTITION_1},
}
],
"parent_state": {"posts": {"updated_at": PARENT_POSTS_CURSOR}},
}
},
"states": [
{
"partition": {
"id": 10,
"parent_slice": {"id": 1, "parent_slice": {}},
},
"cursor": {"created_at": INITIAL_STATE_PARTITION_10_CURSOR_TIMESTAMP},
},
{
"partition": {
"id": 11,
"parent_slice": {"id": 1, "parent_slice": {}},
},
"cursor": {"created_at": INITIAL_STATE_PARTITION_11_CURSOR},
},
],
"state": {"created_at": INITIAL_STATE_PARTITION_11_CURSOR_TIMESTAMP},
"lookback_window": 86400,
},
# Expected state
{
"states": [
{
"partition": {
"id": 10,
"parent_slice": {"id": 1, "parent_slice": {}},
},
"cursor": {"created_at": VOTE_100_CREATED_AT},
},
{
"partition": {
"id": 11,
"parent_slice": {"id": 1, "parent_slice": {}},
},
"cursor": {"created_at": VOTE_111_CREATED_AT},
},
{
"partition": {
"id": 12,
"parent_slice": {"id": 1, "parent_slice": {}},
},
"cursor": {"created_at": LOOKBACK_DATE},
},
{
"partition": {
"id": 20,
"parent_slice": {"id": 2, "parent_slice": {}},
},
"cursor": {"created_at": VOTE_200_CREATED_AT},
},
{
"partition": {
"id": 21,
"parent_slice": {"id": 2, "parent_slice": {}},
},
"cursor": {"created_at": VOTE_210_CREATED_AT},
},
{
"partition": {
"id": 30,
"parent_slice": {"id": 3, "parent_slice": {}},
},
"cursor": {"created_at": VOTE_300_CREATED_AT},
},
],
"use_global_cursor": False,
"lookback_window": 1,
"parent_state": {},
"state": {"created_at": VOTE_100_CREATED_AT},
},
),
],
)
def test_incremental_parent_state_no_incremental_dependency(
test_name, manifest, mock_requests, expected_records, initial_state, expected_state
):
"""
This is a pretty complicated test that syncs a low-code connector stream with three levels of substreams
- posts: (ids: 1, 2, 3)
- post comments: (parent post 1 with ids: 9, 10, 11, 12; parent post 2 with ids: 20, 21; parent post 3 with id: 30)
- post comment votes: (parent comment 10 with ids: 100, 101; parent comment 11 with id: 111;
parent comment 20 with id: 200; parent comment 21 with id: 210, parent comment 30 with id: 300)
By setting incremental_dependency to false, parent streams will not use the incoming state and will not update state.
The post_comment_votes substream is incremental and will emit state messages We verify this by ensuring that mocked
parent stream requests use the incoming config as query parameters and the substream state messages does not
contain parent stream state.
"""
run_mocked_test(
mock_requests,
manifest,
CONFIG,
STREAM_NAME,
initial_state,
expected_records,
expected_state,
)
def run_incremental_parent_state_test(
manifest,
mock_requests,
expected_records,
num_intermediate_states,
initial_state,
expected_states,
):
"""
Run an incremental parent state test for the specified stream.
This function performs the following steps:
1. Mocks the API requests as defined in mock_requests.
2. Executes the read operation using the provided manifest and config.
3. Asserts that the output records match the expected records.
4. Collects intermediate states and records, performing additional reads as necessary.
5. Compares the cumulative records from each state against the expected records.
6. Asserts that the final state matches one of the expected states for each run.
Args:
manifest (dict): The manifest configuration for the stream.
mock_requests (list): A list of tuples containing URL and response data for mocking API requests.
expected_records (list): The expected records to compare against the output.
num_intermediate_states (int): The number of intermediate states to expect.
initial_state (list): The initial state to start the read operation.
expected_states (list): A list of expected final states after the read operation.
"""
initial_state = [
AirbyteStateMessage(
type=AirbyteStateType.STREAM,
stream=AirbyteStreamState(
stream_descriptor=StreamDescriptor(name=STREAM_NAME, namespace=None),
stream_state=AirbyteStateBlob(initial_state),
),
)
]
with requests_mock.Mocker() as m:
for url, response in mock_requests:
m.get(url, json=response)
# Run the initial read
output = _run_read(manifest, CONFIG, STREAM_NAME, initial_state)
# Assert that output_data equals expected_records
assert sorted([r.record.data for r in output.records], key=lambda x: x["id"]) == sorted(
expected_records, key=lambda x: x["id"]
)
# Collect the intermediate states and records produced before each state
cumulative_records = []
intermediate_states = []
final_states = [] # To store the final state after each read
# Store the final state after the initial read
final_states.append(output.state_messages[-1].state.stream.stream_state.__dict__)
for message in output.records_and_state_messages:
if message.type.value == "RECORD":
record_data = message.record.data
cumulative_records.append(record_data)
elif message.type.value == "STATE":
# Record the state and the records produced before this state
state = message.state
records_before_state = cumulative_records.copy()
intermediate_states.append((state, records_before_state))
# Assert that the number of intermediate states is as expected
assert len(intermediate_states) - 1 == num_intermediate_states
# For each intermediate state, perform another read starting from that state
for state, records_before_state in intermediate_states[:-1]:
output_intermediate = _run_read(manifest, CONFIG, STREAM_NAME, [state])
records_from_state = [r.record.data for r in output_intermediate.records]
# Combine records produced before the state with records from the new read
cumulative_records_state = records_before_state + records_from_state
# Duplicates may occur because the state matches the cursor of the last record, causing it to be re-emitted in the next sync.
cumulative_records_state_deduped = list(
{orjson.dumps(record): record for record in cumulative_records_state}.values()
)
# Compare the cumulative records with the expected records
expected_records_set = list(
{orjson.dumps(record): record for record in expected_records}.values()
)
assert (
sorted(cumulative_records_state_deduped, key=lambda x: x["id"])
== sorted(expected_records_set, key=lambda x: x["id"])
), f"Records mismatch with intermediate state {state}. Expected {expected_records}, got {cumulative_records_state_deduped}"
# Store the final state after each intermediate read
final_state_intermediate = [
message.state.stream.stream_state.__dict__
for message in output_intermediate.state_messages
]
final_states.append(final_state_intermediate[-1])
# Assert that the final state matches the expected state for all runs
for i, final_state in enumerate(final_states):
assert (
final_state in expected_states
), f"Final state mismatch at run {i + 1}. Expected {expected_states}, got {final_state}"
@pytest.mark.parametrize(
"test_name, manifest, mock_requests, expected_records, num_intermediate_states, initial_state, expected_state",
[
(
"test_incremental_parent_state",
SUBSTREAM_MANIFEST,
[
# Fetch the first page of posts
(
f"https://api.example.com/community/posts?per_page=100&start_time={PARENT_POSTS_CURSOR}",
{
"posts": [
{"id": 1, "updated_at": POST_1_UPDATED_AT},
{"id": 2, "updated_at": POST_2_UPDATED_AT},
],
"next_page": (
f"https://api.example.com/community/posts"
f"?per_page=100&start_time={PARENT_POSTS_CURSOR}&page=2"
),
},
),
# Fetch the second page of posts
(
f"https://api.example.com/community/posts?per_page=100&start_time={PARENT_POSTS_CURSOR}&page=2",
{"posts": [{"id": 3, "updated_at": POST_3_UPDATED_AT}]},
),
# Fetch the first page of comments for post 1
(
"https://api.example.com/community/posts/1/comments?per_page=100",
{
"comments": [
{
"id": 9,
"post_id": 1,
"updated_at": COMMENT_9_OLDEST,
},
{
"id": 10,
"post_id": 1,
"updated_at": COMMENT_10_UPDATED_AT,
},
{
"id": 11,
"post_id": 1,
"updated_at": COMMENT_11_UPDATED_AT,
},
],
"next_page": "https://api.example.com/community/posts/1/comments?per_page=100&page=2",
},
),
# Fetch the second page of comments for post 1
(
"https://api.example.com/community/posts/1/comments?per_page=100&page=2",
{"comments": [{"id": 12, "post_id": 1, "updated_at": COMMENT_12_UPDATED_AT}]},
),
# Fetch the first page of votes for comment 10 of post 1
(
f"https://api.example.com/community/posts/1/comments/10/votes?per_page=100&start_time={INITIAL_STATE_PARTITION_10_CURSOR}",
{
"votes": [
{
"id": 100,
"comment_id": 10,
"created_at": VOTE_100_CREATED_AT,
}
],
"next_page": (
f"https://api.example.com/community/posts/1/comments/10/votes"
f"?per_page=100&page=2&start_time={INITIAL_STATE_PARTITION_10_CURSOR}"
),
},
),
# Fetch the second page of votes for comment 10 of post 1
(
f"https://api.example.com/community/posts/1/comments/10/votes"
f"?per_page=100&page=2&start_time={INITIAL_STATE_PARTITION_10_CURSOR}",
{"votes": [{"id": 101, "comment_id": 10, "created_at": VOTE_101_CREATED_AT}]},
),
# Fetch the first page of votes for comment 11 of post 1
(
f"https://api.example.com/community/posts/1/comments/11/votes"
f"?per_page=100&start_time={INITIAL_STATE_PARTITION_11_CURSOR}",
{"votes": [{"id": 111, "comment_id": 11, "created_at": VOTE_111_CREATED_AT}]},
),
# Fetch the first page of votes for comment 12 of post 1
(
f"https://api.example.com/community/posts/1/comments/12/votes?per_page=100&start_time={LOOKBACK_DATE}",
{"votes": []},
),
# Fetch the first page of comments for post 2
(
"https://api.example.com/community/posts/2/comments?per_page=100",
{
"comments": [{"id": 20, "post_id": 2, "updated_at": COMMENT_20_UPDATED_AT}],
"next_page": "https://api.example.com/community/posts/2/comments?per_page=100&page=2",
},
),
# Fetch the second page of comments for post 2
(
"https://api.example.com/community/posts/2/comments?per_page=100&page=2",
{"comments": [{"id": 21, "post_id": 2, "updated_at": COMMENT_21_UPDATED_AT}]},
),
# Fetch the first page of votes for comment 20 of post 2
(
f"https://api.example.com/community/posts/2/comments/20/votes?per_page=100&start_time={LOOKBACK_DATE}",
{"votes": [{"id": 200, "comment_id": 20, "created_at": VOTE_200_CREATED_AT}]},
),
# Fetch the first page of votes for comment 21 of post 2
(
f"https://api.example.com/community/posts/2/comments/21/votes?per_page=100&start_time={LOOKBACK_DATE}",
{"votes": [{"id": 210, "comment_id": 21, "created_at": VOTE_210_CREATED_AT}]},
),
# Fetch the first page of comments for post 3
(
"https://api.example.com/community/posts/3/comments?per_page=100",
{"comments": [{"id": 30, "post_id": 3, "updated_at": COMMENT_30_UPDATED_AT}]},
),
# Fetch the first page of votes for comment 30 of post 3
(
f"https://api.example.com/community/posts/3/comments/30/votes?per_page=100&start_time={LOOKBACK_DATE}",
{
"votes": [
{
"id": 300,
"comment_id": 30,
"created_at": VOTE_300_CREATED_AT_TIMESTAMP,
}
]
},
),
# Requests with intermediate states
# Fetch votes for comment 10 of post 1
(
f"https://api.example.com/community/posts/1/comments/10/votes?per_page=100&start_time={VOTE_100_CREATED_AT}",