-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
airbyte-integrations/connectors/source-freshdesk/unit_tests/test_streams.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# | ||
# Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import random | ||
import pytest | ||
from typing import Any, MutableMapping | ||
|
||
from source_freshdesk.streams import Agents, Companies, Contacts, Conversations, Groups, Roles, SatisfactionRatings, Skills, Tickets, TimeEntries | ||
from airbyte_cdk.models import SyncMode | ||
from airbyte_cdk.sources.streams import Stream | ||
|
||
|
||
def _read_full_refresh(stream_instance: Stream): | ||
records = [] | ||
slices = stream_instance.stream_slices(sync_mode=SyncMode.full_refresh) | ||
for slice in slices: | ||
records.extend(list(stream_instance.read_records(stream_slice=slice, sync_mode=SyncMode.full_refresh))) | ||
return records | ||
|
||
|
||
def _read_incremental(stream_instance: Stream, stream_state: MutableMapping[str, Any]): | ||
res = [] | ||
slices = stream_instance.stream_slices(sync_mode=SyncMode.incremental, stream_state=stream_state) | ||
for slice in slices: | ||
records = stream_instance.read_records(sync_mode=SyncMode.incremental, stream_slice=slice, stream_state=stream_state) | ||
for record in records: | ||
stream_state = stream_instance.get_updated_state(stream_state, record) | ||
res.append(record) | ||
return res, stream_state | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"stream, resource", | ||
[ | ||
(Agents, "agents"), | ||
(Companies, "companies"), | ||
(Contacts, "contacts"), | ||
(Groups, "groups"), | ||
(Roles, "roles"), | ||
(Skills, "skills"), | ||
(TimeEntries, "time_entries"), | ||
(SatisfactionRatings, "surveys/satisfaction_ratings"), | ||
], | ||
) | ||
def test_full_refresh(stream, resource, authenticator, config, requests_mock): | ||
requests_mock.register_uri("GET", f"/api/{resource}", json=[{"id": x} for x in range(25)]) | ||
|
||
stream = stream(authenticator=authenticator, config=config) | ||
records = _read_full_refresh(stream) | ||
|
||
assert len(records) == 25 | ||
|
||
|
||
def test_full_refresh_conversations(authenticator, config, requests_mock): | ||
requests_mock.register_uri("GET", f"/api/tickets", json=[{"id": x} for x in range(5)]) | ||
for i in range(5): | ||
requests_mock.register_uri("GET", f"/api/tickets/{i}/conversations", json=[{"id": x} for x in range(10)]) | ||
|
||
stream = Conversations(authenticator=authenticator, config=config) | ||
records = _read_full_refresh(stream) | ||
|
||
assert len(records) == 50 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"stream, resource", | ||
[ | ||
(Contacts, "contacts"), | ||
(Tickets, "tickets"), | ||
(SatisfactionRatings, "surveys/satisfaction_ratings"), | ||
], | ||
) | ||
def test_incremental(stream, resource, authenticator, config, requests_mock): | ||
highest_updated_at = "2022-04-25T22:00:00Z" | ||
other_updated_at = "2022-04-01T00:00:00Z" | ||
highest_index = random.randint(0, 25) | ||
requests_mock.register_uri( | ||
"GET", | ||
f"/api/{resource}", | ||
json=[{"id": x, "updated_at": highest_updated_at if x == highest_index else other_updated_at} for x in range(25)] | ||
) | ||
|
||
stream = stream(authenticator=authenticator, config=config) | ||
records, state = _read_incremental(stream, {}) | ||
|
||
assert len(records) == 25 | ||
assert "updated_at" in state | ||
assert state["updated_at"] == highest_updated_at |