diff --git a/airbyte-integrations/connectors/source-zendesk-support/unit_tests/test_other.py b/airbyte-integrations/connectors/source-zendesk-support/unit_tests/test_other.py index 367973ff79b87..902cd43ea8859 100644 --- a/airbyte-integrations/connectors/source-zendesk-support/unit_tests/test_other.py +++ b/airbyte-integrations/connectors/source-zendesk-support/unit_tests/test_other.py @@ -6,20 +6,59 @@ from datetime import datetime from urllib.parse import parse_qsl, urlparse +import pendulum import pytz import requests -from source_zendesk_support.streams import DATETIME_FORMAT, BaseSourceZendeskSupportStream +from source_zendesk_support.source import BasicApiTokenAuthenticator +from source_zendesk_support.streams import DATETIME_FORMAT, END_OF_STREAM_KEY, BaseSourceZendeskSupportStream, TicketComments + +# config +STREAM_ARGS = { + "subdomain": "test", + "start_date": "2022-01-27T00:00:00Z", + "authenticator": BasicApiTokenAuthenticator("test@airbyte.io", "api_token"), +} DATETIME_STR = "2021-07-22T06:55:55Z" DATETIME_FROM_STR = datetime.strptime(DATETIME_STR, DATETIME_FORMAT) STREAM_URL = "https://subdomain.zendesk.com/api/v2/stream.json?&start_time=1647532987&page=1" STREAM_RESPONSE: dict = { - "data": [], - "next_page": "https://subdomain.zendesk.com/api/v2/stream.json?&start_time=1647532987&page=2", + "ticket_events": [ + { + "child_events": [ + { + "id": 99999, + "via": {}, + "via_reference_id": None, + "type": "Comment", + "author_id": 10, + "body": "test_comment", + "html_body": '
test_comment
', + "plain_body": "test_comment", + "public": True, + "attachments": [], + "audit_id": 123456, + "created_at": "2022-03-17T16:03:07Z", + "event_type": "Comment", + } + ], + "id": 999999, + "ticket_id": 3, + "timestamp": 1647532987, + "created_at": "2022-03-17T16:03:07Z", + "updater_id": 9999999, + "via": "Web form", + "system": {}, + "metadata": {}, + "event_type": "Audit", + } + ], + "next_page": "https://subdomain.zendesk.com/api/v2/stream.json?&start_time=1122334455&page=2", "count": 215, - "end_of_stream": True, + "end_of_stream": False, "end_time": 1647532987, } +TEST_STREAM = TicketComments(**STREAM_ARGS) def test_str2datetime(): @@ -46,3 +85,36 @@ def test_parse_next_page_number(requests_mock): test_response = requests.get(STREAM_URL) output = BaseSourceZendeskSupportStream._parse_next_page_number(test_response) assert output == expected + + +def test_next_page_token(requests_mock): + # mocking the logic of next_page_token + if STREAM_RESPONSE.get(END_OF_STREAM_KEY) is False: + expected = {"created_at": "1122334455"} + else: + expected = None + requests_mock.get(STREAM_URL, json=STREAM_RESPONSE) + test_response = requests.get(STREAM_URL) + output = TEST_STREAM.next_page_token(test_response) + assert expected == output + + +def test_request_params(requests_mock): + expected = {"start_time": calendar.timegm(pendulum.parse(STREAM_ARGS.get("start_date")).utctimetuple()), "include": "comment_events"} + stream_state = None + requests_mock.get(STREAM_URL, json=STREAM_RESPONSE) + test_response = requests.get(STREAM_URL) + next_page_token = TEST_STREAM.next_page_token(test_response) + output = TEST_STREAM.request_params(stream_state, next_page_token) + assert expected == output + + +def test_parse_response(requests_mock): + requests_mock.get(STREAM_URL, json=STREAM_RESPONSE) + test_response = requests.get(STREAM_URL) + output = TEST_STREAM.parse_response(test_response) + # get the first parsed element from generator + parsed_output = list(output)[0] + # check, if we have all transformations correctly + for entity in TicketComments.list_entities_from_event: + assert True if entity in parsed_output else False