|
| 1 | +# |
| 2 | +# Copyright (c) 2022 Airbyte, Inc., all rights reserved. |
| 3 | +# |
| 4 | + |
| 5 | +import logging |
| 6 | +from unittest.mock import ANY, Mock, patch |
| 7 | + |
| 8 | +import pytest |
| 9 | +from airbyte_cdk.models.airbyte_protocol import SyncMode |
| 10 | +from source_marketo.source import Activities, Campaigns, MarketoStream, Programs, SourceMarketo |
| 11 | + |
| 12 | + |
| 13 | +def test_create_export_job(mocker, send_email_stream, caplog): |
| 14 | + mocker.patch("time.sleep") |
| 15 | + caplog.set_level(logging.WARNING) |
| 16 | + slices = list(send_email_stream.stream_slices(sync_mode=SyncMode.incremental)) |
| 17 | + assert slices == [ |
| 18 | + {"endAt": ANY, "id": "2c09ce6d", "startAt": ANY}, |
| 19 | + {"endAt": ANY, "id": "cd465f55", "startAt": ANY}, |
| 20 | + {"endAt": ANY, "id": "232aafb4", "startAt": ANY}, |
| 21 | + ] |
| 22 | + assert "Failed to create export job! Status is failed!" in caplog.records[-1].message |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize( |
| 26 | + "activity, expected_schema", |
| 27 | + ( |
| 28 | + ( |
| 29 | + { |
| 30 | + "id": 1, |
| 31 | + "name": "daily_meeting", |
| 32 | + "description": "Connect to a daily meeting", |
| 33 | + "primaryAttribute": {"name": "Meeting ID", "dataType": "integer"}, |
| 34 | + "attributes": [ |
| 35 | + {"name": "Priority", "dataType": "number"}, |
| 36 | + {"name": "Speakers", "dataType": "integer"}, |
| 37 | + {"name": "Phone number", "dataType": "phone"}, |
| 38 | + {"name": "Cost per person", "dataType": "currency"}, |
| 39 | + {"name": "Is mandatory", "dataType": "boolean"}, |
| 40 | + {"name": "Participants", "dataType": "array"}, |
| 41 | + {"name": "Date", "dataType": "date"}, |
| 42 | + {"name": "Time spent per person", "dataType": "double"}, |
| 43 | + ], |
| 44 | + }, |
| 45 | + { |
| 46 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 47 | + "additionalProperties": True, |
| 48 | + "properties": { |
| 49 | + "activityDate": {"format": "date-time", "type": ["null", "string"]}, |
| 50 | + "activityTypeId": {"type": ["null", "integer"]}, |
| 51 | + "campaignId": {"type": ["null", "integer"]}, |
| 52 | + "costperperson": {"type": ["number", "null"]}, |
| 53 | + "date": {"format": "date-time", "type": ["string", "null"]}, |
| 54 | + "ismandatory": {"type": ["boolean", "null"]}, |
| 55 | + "leadId": {"type": ["null", "integer"]}, |
| 56 | + "marketoGUID": {"type": ["null", "string"]}, |
| 57 | + "participants": {"items": {"type": ["integer", "number", "string", "null"]}, "type": ["array", "null"]}, |
| 58 | + "phonenumber": {"type": ["string", "null"]}, |
| 59 | + "primaryAttributeValue": {"type": ["null", "string"]}, |
| 60 | + "primaryAttributeValueId": {"type": ["null", "string"]}, |
| 61 | + "priority": {"type": ["string", "null"]}, |
| 62 | + "speakers": {"type": ["integer", "null"]}, |
| 63 | + "timespentperperson": {"type": ["string", "null"]}, |
| 64 | + }, |
| 65 | + "type": ["null", "object"], |
| 66 | + }, |
| 67 | + ), |
| 68 | + ( |
| 69 | + { |
| 70 | + "id": 1, |
| 71 | + "name": "daily_meeting", |
| 72 | + "description": "Connect to a daily meeting", |
| 73 | + "primaryAttribute": {"name": "Meeting ID", "dataType": "integer"}, |
| 74 | + }, |
| 75 | + { |
| 76 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 77 | + "additionalProperties": True, |
| 78 | + "properties": { |
| 79 | + "activityDate": {"format": "date-time", "type": ["null", "string"]}, |
| 80 | + "activityTypeId": {"type": ["null", "integer"]}, |
| 81 | + "campaignId": {"type": ["null", "integer"]}, |
| 82 | + "leadId": {"type": ["null", "integer"]}, |
| 83 | + "marketoGUID": {"type": ["null", "string"]}, |
| 84 | + "primaryAttributeValue": {"type": ["null", "string"]}, |
| 85 | + "primaryAttributeValueId": {"type": ["null", "string"]}, |
| 86 | + }, |
| 87 | + "type": ["null", "object"], |
| 88 | + }, |
| 89 | + ), |
| 90 | + ), |
| 91 | +) |
| 92 | +def test_activities_schema(activity, expected_schema, config): |
| 93 | + cls = type(activity["name"], (Activities,), {"activity": activity}) |
| 94 | + assert cls(config).get_json_schema() == expected_schema |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize( |
| 98 | + "response_text, expected_records", |
| 99 | + ( |
| 100 | + ( |
| 101 | + """Campaign Run ID,Choice Number,Has Predictive,Step ID,Test Variant,attributes |
| 102 | +1,3,true,10,15,{"spam": "true"} |
| 103 | +2,3,false,11,16,{"spam": "false"}""", |
| 104 | + [ |
| 105 | + { |
| 106 | + "Campaign Run ID": "1", |
| 107 | + "Choice Number": "3", |
| 108 | + "Has Predictive": "true", |
| 109 | + "Step ID": "10", |
| 110 | + "Test Variant": "15", |
| 111 | + "spam": "true", |
| 112 | + }, |
| 113 | + { |
| 114 | + "Campaign Run ID": "2", |
| 115 | + "Choice Number": "3", |
| 116 | + "Has Predictive": "false", |
| 117 | + "Step ID": "11", |
| 118 | + "Test Variant": "16", |
| 119 | + "spam": "false", |
| 120 | + }, |
| 121 | + ], |
| 122 | + ), |
| 123 | + ), |
| 124 | +) |
| 125 | +def test_export_parse_response(send_email_stream, response_text, expected_records): |
| 126 | + assert list(send_email_stream.parse_response(Mock(text=response_text))) == expected_records |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.parametrize( |
| 130 | + "job_statuses", |
| 131 | + ( |
| 132 | + (("Created",), ("Completed",)), |
| 133 | + ( |
| 134 | + ("Created",), |
| 135 | + ("Cancelled",), |
| 136 | + ), |
| 137 | + ), |
| 138 | +) |
| 139 | +def test_export_sleep(send_email_stream, job_statuses): |
| 140 | + def tuple_to_generator(tuple_): |
| 141 | + yield from tuple_ |
| 142 | + |
| 143 | + job_statuses_side_effect = [tuple_to_generator(tuple_) for tuple_ in job_statuses] |
| 144 | + stream_slice = {"startAt": "2020-08-01", "endAt": "2020-08-02", "id": "1"} |
| 145 | + with patch("source_marketo.source.MarketoExportStart.read_records", return_value=iter([Mock()])) as export_start: |
| 146 | + with patch("source_marketo.source.MarketoExportStatus.read_records", side_effect=job_statuses_side_effect) as export_status: |
| 147 | + with patch("source_marketo.source.sleep") as sleep: |
| 148 | + if job_statuses[-1] == ("Cancelled",): |
| 149 | + with pytest.raises(Exception): |
| 150 | + send_email_stream.sleep_till_export_completed(stream_slice) |
| 151 | + else: |
| 152 | + assert send_email_stream.sleep_till_export_completed(stream_slice) is True |
| 153 | + export_start.assert_called() |
| 154 | + export_status.assert_called() |
| 155 | + sleep.assert_called() |
| 156 | + |
| 157 | + |
| 158 | +def test_programs_request_params(config): |
| 159 | + stream = Programs(config) |
| 160 | + params = stream.request_params( |
| 161 | + stream_slice={"startAt": "2020-08-01", "endAt": "2020-08-02"}, next_page_token={"nextPageToken": 2}, stream_state={} |
| 162 | + ) |
| 163 | + assert params == { |
| 164 | + "batchSize": 200, |
| 165 | + "maxReturn": 200, |
| 166 | + "earliestUpdatedAt": "2020-08-01", |
| 167 | + "latestUpdatedAt": "2020-08-02", |
| 168 | + "nextPageToken": 2, |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | +@pytest.mark.parametrize( |
| 173 | + "next_page_token", |
| 174 | + ( |
| 175 | + {"nextPageToken": 2}, |
| 176 | + {}, |
| 177 | + ), |
| 178 | +) |
| 179 | +def test_next_page_token(mocker, config, next_page_token): |
| 180 | + stream = MarketoStream(config) |
| 181 | + token = stream.next_page_token(Mock(json=Mock(return_value=next_page_token))) |
| 182 | + assert token == (next_page_token or None) |
| 183 | + |
| 184 | + |
| 185 | +@pytest.mark.parametrize( |
| 186 | + "response, state, expected_records", |
| 187 | + ( |
| 188 | + ( |
| 189 | + {"result": [{"id": "1", "createdAt": "2020-07-01T00:00:00Z"}, {"id": "2", "createdAt": "2020-08-02T00:00:00Z"}]}, |
| 190 | + {"createdAt": "2020-08-01T20:20:00Z"}, |
| 191 | + [{"id": "2", "createdAt": "2020-08-02T00:00:00Z"}], |
| 192 | + ), |
| 193 | + ), |
| 194 | +) |
| 195 | +def test_parse_response_incremental(config, response, state, expected_records): |
| 196 | + stream = Campaigns(config) |
| 197 | + records = stream.parse_response(Mock(json=Mock(return_value=response)), stream_state=state) |
| 198 | + assert list(records) == expected_records |
| 199 | + |
| 200 | + |
| 201 | +def test_source_streams(config, activity): |
| 202 | + source = SourceMarketo() |
| 203 | + with patch("source_marketo.source.ActivityTypes.read_records", Mock(return_value=[activity])): |
| 204 | + streams = source.streams(config) |
| 205 | + assert len(streams) == 6 |
| 206 | + assert all(isinstance(stream, MarketoStream) for stream in streams) |
| 207 | + |
| 208 | + |
| 209 | +@pytest.mark.parametrize( |
| 210 | + "status_code, response, is_connection_successful, error_msg", |
| 211 | + ( |
| 212 | + (200, "", True, None), |
| 213 | + ( |
| 214 | + 400, |
| 215 | + "Bad request", |
| 216 | + False, |
| 217 | + "HTTPError('400 Client Error: None for url: https://602-euo-598.mktorest.com/rest/v1/leads/describe')", |
| 218 | + ), |
| 219 | + ( |
| 220 | + 403, |
| 221 | + "Forbidden", |
| 222 | + False, |
| 223 | + "HTTPError('403 Client Error: None for url: https://602-euo-598.mktorest.com/rest/v1/leads/describe')", |
| 224 | + ), |
| 225 | + ), |
| 226 | +) |
| 227 | +def test_check_connection(config, requests_mock, status_code, response, is_connection_successful, error_msg): |
| 228 | + requests_mock.register_uri("GET", "https://602-euo-598.mktorest.com/rest/v1/leads/describe", status_code=status_code) |
| 229 | + source = SourceMarketo() |
| 230 | + success, error = source.check_connection(logger=None, config=config) |
| 231 | + assert success is is_connection_successful |
| 232 | + assert error == error_msg |
| 233 | + |
| 234 | + |
| 235 | +@pytest.mark.parametrize( |
| 236 | + "input, format, expected_result", |
| 237 | + ( |
| 238 | + ("2020-08-01T20:20:21Z", "%Y-%m-%dT%H:%M:%SZ%z", "2020-08-01T20:20:21Z"), |
| 239 | + ("2020-08-01 20:20", "%Y-%m-%d %H:%M", "2020-08-01T20:20:00Z"), |
| 240 | + ("2020-08-01", "%Y-%m-%dT%H:%M:%SZ%z", "2020-08-01"), |
| 241 | + ), |
| 242 | +) |
| 243 | +def test_normalize_datetime(config, input, format, expected_result): |
| 244 | + stream = Programs(config) |
| 245 | + assert stream.normalize_datetime(input, format) == expected_result |
0 commit comments