|
| 1 | +# |
| 2 | +# Copyright (c) 2022 Airbyte, Inc., all rights reserved. |
| 3 | +# |
| 4 | + |
| 5 | +import pytest |
| 6 | +from airbyte_cdk.sources.declarative.extractors.record_filter import RecordFilter |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize( |
| 10 | + "test_name, filter_template, records, expected_records", |
| 11 | + [ |
| 12 | + ( |
| 13 | + "test_using_state_filter", |
| 14 | + "{{ record['created_at'] > stream_state['created_at'] }}", |
| 15 | + [{"id": 1, "created_at": "06-06-21"}, {"id": 2, "created_at": "06-07-21"}, {"id": 3, "created_at": "06-08-21"}], |
| 16 | + [{"id": 2, "created_at": "06-07-21"}, {"id": 3, "created_at": "06-08-21"}], |
| 17 | + ), |
| 18 | + ( |
| 19 | + "test_with_slice_filter", |
| 20 | + "{{ record['last_seen'] >= stream_slice['last_seen'] }}", |
| 21 | + [{"id": 1, "last_seen": "06-06-21"}, {"id": 2, "last_seen": "06-07-21"}, {"id": 3, "last_seen": "06-10-21"}], |
| 22 | + [{"id": 3, "last_seen": "06-10-21"}], |
| 23 | + ), |
| 24 | + ( |
| 25 | + "test_with_next_page_token_filter", |
| 26 | + "{{ record['id'] >= next_page_token['last_seen_id'] }}", |
| 27 | + [{"id": 11}, {"id": 12}, {"id": 13}, {"id": 14}, {"id": 15}], |
| 28 | + [{"id": 14}, {"id": 15}], |
| 29 | + ), |
| 30 | + ( |
| 31 | + "test_missing_filter_fields_return_no_results", |
| 32 | + "{{ record['id'] >= next_page_token['path_to_nowhere'] }}", |
| 33 | + [{"id": 11}, {"id": 12}, {"id": 13}, {"id": 14}, {"id": 15}], |
| 34 | + [], |
| 35 | + ), |
| 36 | + ], |
| 37 | +) |
| 38 | +def test_record_filter(test_name, filter_template, records, expected_records): |
| 39 | + config = {"response_override": "stop_if_you_see_me"} |
| 40 | + stream_state = {"created_at": "06-06-21"} |
| 41 | + stream_slice = {"last_seen": "06-10-21"} |
| 42 | + next_page_token = {"last_seen_id": 14} |
| 43 | + record_filter = RecordFilter(config=config, condition=filter_template) |
| 44 | + |
| 45 | + actual_records = record_filter.filter_records( |
| 46 | + records, stream_state=stream_state, stream_slice=stream_slice, next_page_token=next_page_token |
| 47 | + ) |
| 48 | + assert actual_records == expected_records |
0 commit comments