|
8 | 8 | from unittest.mock import MagicMock |
9 | 9 |
|
10 | 10 | import freezegun |
| 11 | +import pytest |
11 | 12 |
|
12 | 13 | from airbyte_cdk.models import ( |
13 | 14 | AirbyteStateBlob, |
@@ -405,3 +406,148 @@ def test_cursor_age_validation_emits_warning_when_falling_back(caplog): |
405 | 406 | assert any( |
406 | 407 | "TestStream" in msg and "older than" in msg and "P7D" in msg for msg in warning_messages |
407 | 408 | ), f"Expected warning about stale cursor not found. Warnings: {warning_messages}" |
| 409 | + |
| 410 | + |
| 411 | +@freezegun.freeze_time("2024-07-15") |
| 412 | +def test_cursor_age_validation_with_per_partition_state_uses_global_cursor(): |
| 413 | + """Test that per-partition state structure uses global cursor for age validation.""" |
| 414 | + manifest = _create_manifest_with_retention_period("P7D") |
| 415 | + |
| 416 | + with HttpMocker() as http_mocker: |
| 417 | + http_mocker.get( |
| 418 | + HttpRequest(url="https://api.test.com/items"), |
| 419 | + HttpResponse( |
| 420 | + body=json.dumps( |
| 421 | + [ |
| 422 | + {"id": 1, "name": "item_1", "updated_at": "2024-07-13"}, |
| 423 | + ] |
| 424 | + ) |
| 425 | + ), |
| 426 | + ) |
| 427 | + |
| 428 | + state = [ |
| 429 | + AirbyteStateMessage( |
| 430 | + type=AirbyteStateType.STREAM, |
| 431 | + stream=AirbyteStreamState( |
| 432 | + stream_descriptor=StreamDescriptor(name="TestStream", namespace=None), |
| 433 | + stream_state=AirbyteStateBlob( |
| 434 | + state={"updated_at": "2024-07-01"}, |
| 435 | + states=[ |
| 436 | + { |
| 437 | + "partition": {"parent_id": "1"}, |
| 438 | + "cursor": {"updated_at": "2024-07-10"}, |
| 439 | + }, |
| 440 | + { |
| 441 | + "partition": {"parent_id": "2"}, |
| 442 | + "cursor": {"updated_at": "2024-07-05"}, |
| 443 | + }, |
| 444 | + ], |
| 445 | + use_global_cursor=False, |
| 446 | + ), |
| 447 | + ), |
| 448 | + ) |
| 449 | + ] |
| 450 | + source = ConcurrentDeclarativeSource( |
| 451 | + source_config=manifest, config=_CONFIG, catalog=None, state=state |
| 452 | + ) |
| 453 | + configured_catalog = create_configured_catalog(source, _CONFIG) |
| 454 | + |
| 455 | + records = get_records(source, _CONFIG, configured_catalog, state) |
| 456 | + assert len(records) == 1 |
| 457 | + |
| 458 | + |
| 459 | +@freezegun.freeze_time("2024-07-15") |
| 460 | +def test_cursor_age_validation_with_per_partition_state_within_retention(): |
| 461 | + """Test per-partition state with global cursor within retention uses incremental. |
| 462 | +
|
| 463 | + This test verifies that when the global cursor in a per-partition state structure |
| 464 | + is within the retention period, the incremental stream is selected (not full refresh). |
| 465 | + We verify this by checking that the incremental endpoint is called, not the full refresh one. |
| 466 | + """ |
| 467 | + manifest = _create_manifest_with_retention_period("P30D") |
| 468 | + |
| 469 | + with HttpMocker() as http_mocker: |
| 470 | + http_mocker.get( |
| 471 | + HttpRequest( |
| 472 | + url="https://api.test.com/items_with_filtration", |
| 473 | + query_params={"start": "2024-07-01", "end": "2024-07-15"}, |
| 474 | + ), |
| 475 | + HttpResponse( |
| 476 | + body=json.dumps( |
| 477 | + [ |
| 478 | + {"id": 3, "name": "item_3", "updated_at": "2024-07-14"}, |
| 479 | + ] |
| 480 | + ) |
| 481 | + ), |
| 482 | + ) |
| 483 | + |
| 484 | + state = [ |
| 485 | + AirbyteStateMessage( |
| 486 | + type=AirbyteStateType.STREAM, |
| 487 | + stream=AirbyteStreamState( |
| 488 | + stream_descriptor=StreamDescriptor(name="TestStream", namespace=None), |
| 489 | + stream_state=AirbyteStateBlob( |
| 490 | + state={"updated_at": "2024-07-10"}, |
| 491 | + states=[ |
| 492 | + { |
| 493 | + "partition": {"parent_id": "1"}, |
| 494 | + "cursor": {"updated_at": "2024-07-10"}, |
| 495 | + }, |
| 496 | + ], |
| 497 | + use_global_cursor=False, |
| 498 | + ), |
| 499 | + ), |
| 500 | + ) |
| 501 | + ] |
| 502 | + source = ConcurrentDeclarativeSource( |
| 503 | + source_config=manifest, config=_CONFIG, catalog=None, state=state |
| 504 | + ) |
| 505 | + configured_catalog = create_configured_catalog(source, _CONFIG) |
| 506 | + |
| 507 | + records = get_records(source, _CONFIG, configured_catalog, state) |
| 508 | + assert len(records) == 1 |
| 509 | + |
| 510 | + |
| 511 | +def _create_manifest_with_incrementing_count_cursor(api_retention_period: str) -> dict: |
| 512 | + """Create a manifest with IncrementingCountCursor and api_retention_period.""" |
| 513 | + manifest = copy.deepcopy(_MANIFEST) |
| 514 | + manifest["definitions"]["TestStream"]["api_retention_period"] = api_retention_period |
| 515 | + |
| 516 | + incrementing_cursor = { |
| 517 | + "type": "IncrementingCountCursor", |
| 518 | + "cursor_field": "id", |
| 519 | + "start_value": 0, |
| 520 | + } |
| 521 | + manifest["definitions"]["TestStream"]["full_refresh_stream"]["incremental_sync"] = ( |
| 522 | + incrementing_cursor |
| 523 | + ) |
| 524 | + manifest["definitions"]["TestStream"]["incremental_stream"]["incremental_sync"] = ( |
| 525 | + incrementing_cursor |
| 526 | + ) |
| 527 | + return manifest |
| 528 | + |
| 529 | + |
| 530 | +def test_cursor_age_validation_raises_error_for_incrementing_count_cursor(): |
| 531 | + """Test that IncrementingCountCursor with api_retention_period raises an error.""" |
| 532 | + manifest = _create_manifest_with_incrementing_count_cursor("P7D") |
| 533 | + |
| 534 | + state = [ |
| 535 | + AirbyteStateMessage( |
| 536 | + type=AirbyteStateType.STREAM, |
| 537 | + stream=AirbyteStreamState( |
| 538 | + stream_descriptor=StreamDescriptor(name="TestStream", namespace=None), |
| 539 | + stream_state=AirbyteStateBlob(id=100), |
| 540 | + ), |
| 541 | + ) |
| 542 | + ] |
| 543 | + |
| 544 | + source = ConcurrentDeclarativeSource( |
| 545 | + source_config=manifest, config=_CONFIG, catalog=None, state=state |
| 546 | + ) |
| 547 | + |
| 548 | + with pytest.raises(ValueError) as exc_info: |
| 549 | + source.discover(logger=MagicMock(), config=_CONFIG) |
| 550 | + |
| 551 | + assert "IncrementingCountCursor" in str(exc_info.value) |
| 552 | + assert "not supported" in str(exc_info.value) |
| 553 | + assert "api_retention_period" in str(exc_info.value) |
0 commit comments