|
12 | 12 | from airbyte_cdk.models import ConfiguredAirbyteCatalog, SyncMode, Type
|
13 | 13 | from source_hubspot.errors import HubspotRateLimited
|
14 | 14 | from source_hubspot.source import SourceHubspot
|
15 |
| -from source_hubspot.streams import API, PROPERTIES_PARAM_MAX_LENGTH, Companies, Deals, Products, Stream, Workflows, split_properties |
| 15 | +from source_hubspot.streams import API, PROPERTIES_PARAM_MAX_LENGTH, Companies, Deals, Engagements, Products, Stream, Workflows, split_properties |
16 | 16 |
|
17 | 17 | NUMBER_OF_PROPERTIES = 2000
|
18 | 18 |
|
@@ -399,3 +399,106 @@ def test_search_based_stream_should_not_attempt_to_get_more_than_10k_records(req
|
399 | 399 | # Instead, it should use the new state to start a new search query.
|
400 | 400 | assert len(records) == 11000
|
401 | 401 | assert test_stream.state["updatedAt"] == "2022-03-01T00:00:00+00:00"
|
| 402 | + |
| 403 | + |
| 404 | +def test_engagements_stream_pagination_works(requests_mock, common_params): |
| 405 | + """ |
| 406 | + Tests the engagements stream handles pagination correctly, for both |
| 407 | + full_refresh and incremental sync modes. |
| 408 | + """ |
| 409 | + |
| 410 | + # Mocking Request |
| 411 | + requests_mock.register_uri("GET", "/engagements/v1/engagements/paged?hapikey=test_api_key&count=250", [ |
| 412 | + { |
| 413 | + "json": { |
| 414 | + "results": [{"engagement": {"id": f"{y}", "lastUpdated": 1641234593251}} for y in range(250)], |
| 415 | + "hasMore": True, |
| 416 | + "offset": 250 |
| 417 | + }, |
| 418 | + "status_code": 200, |
| 419 | + }, |
| 420 | + { |
| 421 | + "json": { |
| 422 | + "results": [{"engagement": {"id": f"{y}", "lastUpdated": 1641234593251}} for y in range(250, 500)], |
| 423 | + "hasMore": True, |
| 424 | + "offset": 500 |
| 425 | + }, |
| 426 | + "status_code": 200, |
| 427 | + }, |
| 428 | + { |
| 429 | + "json": { |
| 430 | + "results": [{"engagement": {"id": f"{y}", "lastUpdated": 1641234595251}} for y in range(500, 600)], |
| 431 | + "hasMore": False |
| 432 | + }, |
| 433 | + "status_code": 200, |
| 434 | + } |
| 435 | + ]) |
| 436 | + |
| 437 | + requests_mock.register_uri("GET", "/engagements/v1/engagements/recent/modified?hapikey=test_api_key&count=100", [ |
| 438 | + { |
| 439 | + "json": { |
| 440 | + "results": [{"engagement": {"id": f"{y}", "lastUpdated": 1641234595252}} for y in range(100)], |
| 441 | + "hasMore": True, |
| 442 | + "offset": 100 |
| 443 | + }, |
| 444 | + "status_code": 200, |
| 445 | + }, |
| 446 | + { |
| 447 | + "json": { |
| 448 | + "results": [{"engagement": {"id": f"{y}", "lastUpdated": 1641234595252}} for y in range(100, 200)], |
| 449 | + "hasMore": True, |
| 450 | + "offset": 200 |
| 451 | + }, |
| 452 | + "status_code": 200, |
| 453 | + }, |
| 454 | + { |
| 455 | + "json": { |
| 456 | + "results": [{"engagement": {"id": f"{y}", "lastUpdated": 1641234595252}} for y in range(200, 250)], |
| 457 | + "hasMore": False |
| 458 | + }, |
| 459 | + "status_code": 200, |
| 460 | + } |
| 461 | + ]) |
| 462 | + |
| 463 | + # Create test_stream instance for full refresh. |
| 464 | + test_stream = Engagements(**common_params) |
| 465 | + |
| 466 | + records = list(test_stream.read_records(sync_mode=SyncMode.full_refresh)) |
| 467 | + # The stream should handle pagination correctly and output 600 records. |
| 468 | + assert len(records) == 600 |
| 469 | + assert test_stream.state["lastUpdated"] == 1641234595251 |
| 470 | + |
| 471 | + records = list(test_stream.read_records(sync_mode=SyncMode.incremental)) |
| 472 | + # The stream should handle pagination correctly and output 600 records. |
| 473 | + assert len(records) == 250 |
| 474 | + assert test_stream.state["lastUpdated"] == 1641234595252 |
| 475 | + |
| 476 | + |
| 477 | +def test_incremental_engagements_stream_stops_at_10K_records(requests_mock, common_params, fake_properties_list): |
| 478 | + """ |
| 479 | + If there are more than 10,000 engagements that would be returned by the Hubspot recent engagements endpoint, |
| 480 | + the Engagements instance should stop at the 10Kth record. |
| 481 | + """ |
| 482 | + |
| 483 | + responses = [ |
| 484 | + { |
| 485 | + "json": { |
| 486 | + "results": [{"engagement": {"id": f"{y}", "lastUpdated": 1641234595252}} for y in range(100)], |
| 487 | + "hasMore": True, |
| 488 | + "offset": x*100 |
| 489 | + }, |
| 490 | + "status_code": 200, |
| 491 | + } |
| 492 | + for x in range(1, 102) |
| 493 | + ] |
| 494 | + |
| 495 | + # Create test_stream instance with some state |
| 496 | + test_stream = Engagements(**common_params) |
| 497 | + test_stream.state = {"lastUpdated": 1641234595251} |
| 498 | + |
| 499 | + # Mocking Request |
| 500 | + requests_mock.register_uri("GET", "/engagements/v1/engagements/recent/modified?hapikey=test_api_key&count=100", responses) |
| 501 | + records = list(test_stream.read_records(sync_mode=SyncMode.incremental)) |
| 502 | + # The stream should not attempt to get more than 10K records. |
| 503 | + assert len(records) == 10000 |
| 504 | + assert test_stream.state["lastUpdated"] == +1641234595252 |
0 commit comments