|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Airbyte, Inc., all rights reserved. |
| 3 | +# |
| 4 | + |
| 5 | +import json |
| 6 | + |
| 7 | +from conftest import TEST_CONFIG, get_source |
| 8 | +from freezegun import freeze_time |
| 9 | + |
| 10 | +from airbyte_cdk.models import SyncMode |
| 11 | +from airbyte_cdk.test.catalog_builder import CatalogBuilder |
| 12 | +from airbyte_cdk.test.entrypoint_wrapper import read |
| 13 | +from airbyte_cdk.test.mock_http import HttpMocker, HttpRequest, HttpResponse |
| 14 | +from airbyte_cdk.test.state_builder import StateBuilder |
| 15 | + |
| 16 | + |
| 17 | +BASE = "https://api.twilio.com/2010-04-01" |
| 18 | + |
| 19 | +ACCOUNTS_JSON = { |
| 20 | + "accounts": [ |
| 21 | + { |
| 22 | + "sid": "AC123", |
| 23 | + "date_created": "2022-01-01T00:00:00Z", |
| 24 | + "subresource_uris": { |
| 25 | + "usage": "/2010-04-01/Accounts/AC123/Usage.json", |
| 26 | + }, |
| 27 | + } |
| 28 | + ], |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +class TestUsageRecords404Handling: |
| 33 | + """Test that usage_records stream handles 404 errors gracefully.""" |
| 34 | + |
| 35 | + @HttpMocker() |
| 36 | + @freeze_time("2022-11-16 12:03:11+00:00") |
| 37 | + def test_usage_records_ignores_404_responses(self, http_mocker: HttpMocker, caplog): |
| 38 | + """Test that the sync ignores 404 responses and logs the appropriate message.""" |
| 39 | + http_mocker.get( |
| 40 | + HttpRequest(url=f"{BASE}/Accounts.json", query_params={"PageSize": "1000"}), |
| 41 | + HttpResponse(body=json.dumps(ACCOUNTS_JSON), status_code=200), |
| 42 | + ) |
| 43 | + |
| 44 | + http_mocker.get( |
| 45 | + HttpRequest( |
| 46 | + url=f"{BASE}/Accounts/AC123/Usage/Records/Daily.json", |
| 47 | + query_params={"PageSize": "1000", "StartDate": "2022-11-15", "EndDate": "2022-11-16"}, |
| 48 | + ), |
| 49 | + HttpResponse( |
| 50 | + body=json.dumps({"code": 20404, "message": "The requested resource was not found"}), |
| 51 | + status_code=404, |
| 52 | + ), |
| 53 | + ) |
| 54 | + |
| 55 | + catalog = CatalogBuilder().with_stream("usage_records", SyncMode.full_refresh).build() |
| 56 | + config = {**TEST_CONFIG, "start_date": "2022-11-15T00:00:00Z"} |
| 57 | + |
| 58 | + output = read(get_source(config), config, catalog) |
| 59 | + |
| 60 | + assert len(output.records) == 0, "Expected no records when 404 is returned" |
| 61 | + |
| 62 | + expected_message = "Skipping this slice" |
| 63 | + log_messages = [record.message for record in caplog.records] |
| 64 | + assert any( |
| 65 | + expected_message in msg for msg in log_messages |
| 66 | + ), f"Expected log message containing '{expected_message}' not found in logs: {log_messages}" |
| 67 | + |
| 68 | + @HttpMocker() |
| 69 | + @freeze_time("2022-11-16 12:03:11+00:00") |
| 70 | + def test_usage_records_completes_with_mixed_responses(self, http_mocker: HttpMocker): |
| 71 | + """Test that sync completes successfully with a sequence of 200, 404, 404, 200 responses.""" |
| 72 | + accounts_json = { |
| 73 | + "accounts": [ |
| 74 | + {"sid": "AC001", "date_created": "2022-01-01T00:00:00Z", "subresource_uris": {}}, |
| 75 | + {"sid": "AC002", "date_created": "2022-01-02T00:00:00Z", "subresource_uris": {}}, |
| 76 | + {"sid": "AC003", "date_created": "2022-01-03T00:00:00Z", "subresource_uris": {}}, |
| 77 | + {"sid": "AC004", "date_created": "2022-01-04T00:00:00Z", "subresource_uris": {}}, |
| 78 | + ], |
| 79 | + } |
| 80 | + http_mocker.get( |
| 81 | + HttpRequest(url=f"{BASE}/Accounts.json", query_params={"PageSize": "1000"}), |
| 82 | + HttpResponse(body=json.dumps(accounts_json), status_code=200), |
| 83 | + ) |
| 84 | + |
| 85 | + http_mocker.get( |
| 86 | + HttpRequest( |
| 87 | + url=f"{BASE}/Accounts/AC001/Usage/Records/Daily.json", |
| 88 | + query_params={"PageSize": "1000", "StartDate": "2022-11-15", "EndDate": "2022-11-16"}, |
| 89 | + ), |
| 90 | + HttpResponse( |
| 91 | + body=json.dumps( |
| 92 | + { |
| 93 | + "usage_records": [ |
| 94 | + { |
| 95 | + "account_sid": "AC001", |
| 96 | + "category": "calls", |
| 97 | + "start_date": "2022-11-15", |
| 98 | + "end_date": "2022-11-16", |
| 99 | + "count": "10", |
| 100 | + "usage": "100", |
| 101 | + } |
| 102 | + ] |
| 103 | + } |
| 104 | + ), |
| 105 | + status_code=200, |
| 106 | + ), |
| 107 | + ) |
| 108 | + |
| 109 | + http_mocker.get( |
| 110 | + HttpRequest( |
| 111 | + url=f"{BASE}/Accounts/AC002/Usage/Records/Daily.json", |
| 112 | + query_params={"PageSize": "1000", "StartDate": "2022-11-15", "EndDate": "2022-11-16"}, |
| 113 | + ), |
| 114 | + HttpResponse( |
| 115 | + body=json.dumps({"code": 20404, "message": "The requested resource was not found"}), |
| 116 | + status_code=404, |
| 117 | + ), |
| 118 | + ) |
| 119 | + |
| 120 | + http_mocker.get( |
| 121 | + HttpRequest( |
| 122 | + url=f"{BASE}/Accounts/AC003/Usage/Records/Daily.json", |
| 123 | + query_params={"PageSize": "1000", "StartDate": "2022-11-15", "EndDate": "2022-11-16"}, |
| 124 | + ), |
| 125 | + HttpResponse( |
| 126 | + body=json.dumps({"code": 20404, "message": "The requested resource was not found"}), |
| 127 | + status_code=404, |
| 128 | + ), |
| 129 | + ) |
| 130 | + |
| 131 | + http_mocker.get( |
| 132 | + HttpRequest( |
| 133 | + url=f"{BASE}/Accounts/AC004/Usage/Records/Daily.json", |
| 134 | + query_params={"PageSize": "1000", "StartDate": "2022-11-15", "EndDate": "2022-11-16"}, |
| 135 | + ), |
| 136 | + HttpResponse( |
| 137 | + body=json.dumps( |
| 138 | + { |
| 139 | + "usage_records": [ |
| 140 | + { |
| 141 | + "account_sid": "AC004", |
| 142 | + "category": "sms", |
| 143 | + "start_date": "2022-11-15", |
| 144 | + "end_date": "2022-11-16", |
| 145 | + "count": "5", |
| 146 | + "usage": "50", |
| 147 | + } |
| 148 | + ] |
| 149 | + } |
| 150 | + ), |
| 151 | + status_code=200, |
| 152 | + ), |
| 153 | + ) |
| 154 | + |
| 155 | + catalog = CatalogBuilder().with_stream("usage_records", SyncMode.full_refresh).build() |
| 156 | + config = {**TEST_CONFIG, "start_date": "2022-11-15T00:00:00Z"} |
| 157 | + |
| 158 | + output = read(get_source(config), config, catalog) |
| 159 | + |
| 160 | + assert len(output.records) == 2, f"Expected 2 records from successful responses, got {len(output.records)}" |
| 161 | + |
| 162 | + account_sids = [record.record.data["account_sid"] for record in output.records] |
| 163 | + assert "AC001" in account_sids, "Expected record from AC001" |
| 164 | + assert "AC004" in account_sids, "Expected record from AC004" |
| 165 | + |
| 166 | + assert output.errors == [], f"Expected no errors, but got: {output.errors}" |
| 167 | + |
| 168 | + @HttpMocker() |
| 169 | + @freeze_time("2022-11-16 12:03:11+00:00") |
| 170 | + def test_usage_records_incremental_with_404_handling(self, http_mocker: HttpMocker): |
| 171 | + """Test that incremental sync handles 404 responses correctly.""" |
| 172 | + http_mocker.get( |
| 173 | + HttpRequest(url=f"{BASE}/Accounts.json", query_params={"PageSize": "1000"}), |
| 174 | + HttpResponse(body=json.dumps(ACCOUNTS_JSON), status_code=200), |
| 175 | + ) |
| 176 | + |
| 177 | + http_mocker.get( |
| 178 | + HttpRequest( |
| 179 | + url=f"{BASE}/Accounts/AC123/Usage/Records/Daily.json", |
| 180 | + query_params={"PageSize": "1000", "StartDate": "2022-11-15", "EndDate": "2022-11-16"}, |
| 181 | + ), |
| 182 | + HttpResponse( |
| 183 | + body=json.dumps({"code": 20404, "message": "The requested resource was not found"}), |
| 184 | + status_code=404, |
| 185 | + ), |
| 186 | + ) |
| 187 | + |
| 188 | + catalog = CatalogBuilder().with_stream("usage_records", SyncMode.incremental).build() |
| 189 | + config = {**TEST_CONFIG, "start_date": "2022-11-15T00:00:00Z"} |
| 190 | + |
| 191 | + state = ( |
| 192 | + StateBuilder() |
| 193 | + .with_stream_state( |
| 194 | + "usage_records", {"states": [{"partition": {"account_sid": "AC123"}, "cursor": {"start_date": "2022-11-13"}}]} |
| 195 | + ) |
| 196 | + .build() |
| 197 | + ) |
| 198 | + |
| 199 | + output = read(get_source(config, state), config, catalog, state) |
| 200 | + |
| 201 | + assert len(output.records) == 0, "Expected no records when 404 is returned in incremental sync" |
| 202 | + |
| 203 | + assert output.errors == [], f"Expected no errors, but got: {output.errors}" |
0 commit comments