Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tolik0 committed Nov 8, 2024
1 parent 9858b34 commit 521a0e4
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 671 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2.0.0
version: 0.51.0

type: DeclarativeSource

Expand Down Expand Up @@ -597,11 +597,6 @@ definitions:
request_parameters:
sort_by: "updated_at"
sort_order: "asc"
record_selector:
type: RecordSelector
extractor:
type: DpathExtractor
field_path: ["articles"]
$parameters:
name: "articles"
path: "help_center/incremental/articles"
Expand Down Expand Up @@ -785,6 +780,7 @@ streams:
- $ref: "#/definitions/schedules_stream"
- $ref: "#/definitions/sla_policies_stream"
- $ref: "#/definitions/tags_stream"
- $ref: "#/definitions/tickets_stream"
- $ref: "#/definitions/ticket_fields_stream"
- $ref: "#/definitions/ticket_forms_stream"
- $ref: "#/definitions/topics_stream"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@

import os

from source_zendesk_support.source import SourceZendeskSupport

os.environ["REQUEST_CACHE_PATH"] = "REQUEST_CACHE_PATH"


def find_stream(stream_name, config):
streams = SourceZendeskSupport().streams(config=config)

# find by name
for stream in streams:
if stream.name == stream_name:
return stream
raise ValueError(f"Stream {stream_name} not found")
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def given_posts(http_mocker: HttpMocker, start_date: DateTime, api_token_authent
)
http_mocker.get(
PostsRequestBuilder.posts_endpoint(api_token_authenticator)
.with_start_time(datetime_to_string(start_date))
.with_page_size(100)
.build(),
PostsResponseBuilder.posts_response().with_record(posts_record_builder).build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ def test_given_one_page_when_read_posts_comments_then_return_records(self, http_
post = posts_record_builder.build()

http_mocker.get(
PostsVotesRequestBuilder.posts_votes_endpoint(api_token_authenticator, post["id"])
.with_start_time(self._config["start_date"])
.with_page_size(100)
.build(),
PostsVotesRequestBuilder.posts_votes_endpoint(api_token_authenticator, post["id"]).with_page_size(100).build(),
PostsVotesResponseBuilder.posts_votes_response().with_record(PostsVotesRecordBuilder.posts_votes_record()).build(),
)

Expand All @@ -73,18 +70,18 @@ def test_given_403_error_when_read_posts_comments_then_skip_stream(self, http_mo
post = posts_record_builder.build()

http_mocker.get(
PostsVotesRequestBuilder.posts_votes_endpoint(api_token_authenticator, post["id"])
.with_start_time(self._config["start_date"])
.with_page_size(100)
.build(),
PostsVotesRequestBuilder.posts_votes_endpoint(api_token_authenticator, post["id"]).with_page_size(100).build(),
ErrorResponseBuilder.response_with_status(403).build(),
)

output = read_stream("post_votes", SyncMode.full_refresh, self._config)
assert len(output.records) == 0

info_logs = get_log_messages_by_log_level(output.logs, LogLevel.INFO)
assert any(["Forbidden. Please ensure the authenticated user has access to this stream. If the issue persists, contact Zendesk support." in error for error in info_logs])
error_message = output.errors[-1].trace.error.message
assert (
error_message
== "Unable to read data for stream post_votes due to an issue with permissions. Please ensure that your account has the necessary access level. You can try to re-authenticate on the Set Up Connector page or you can disable stream post_votes on the Schema Tab. Error message: the 403 error"
)

@HttpMocker()
def test_given_404_error_when_read_posts_comments_then_skip_stream(self, http_mocker):
Expand All @@ -98,18 +95,18 @@ def test_given_404_error_when_read_posts_comments_then_skip_stream(self, http_mo
post = posts_record_builder.build()

http_mocker.get(
PostsVotesRequestBuilder.posts_votes_endpoint(api_token_authenticator, post["id"])
.with_start_time(self._config["start_date"])
.with_page_size(100)
.build(),
PostsVotesRequestBuilder.posts_votes_endpoint(api_token_authenticator, post["id"]).with_page_size(100).build(),
ErrorResponseBuilder.response_with_status(404).build(),
)

output = read_stream("post_votes", SyncMode.full_refresh, self._config)
assert len(output.records) == 0

info_logs = get_log_messages_by_log_level(output.logs, LogLevel.INFO)
assert any(["Not found. Please ensure the authenticated user has access to this stream. If the issue persists, contact Zendesk support." in error for error in info_logs])
error_message = output.errors[-1].trace.error.message
assert (
error_message
== "Unable to read data for stream post_votes due to an issue with permissions. Please ensure that your account has the necessary access level. You can try to re-authenticate on the Set Up Connector page or you can disable stream post_votes on the Schema Tab. Error message: the 404 error"
)

@HttpMocker()
def test_given_500_error_when_read_posts_comments_then_stop_syncing(self, http_mocker):
Expand All @@ -123,20 +120,16 @@ def test_given_500_error_when_read_posts_comments_then_stop_syncing(self, http_m
post = posts_record_builder.build()

http_mocker.get(
PostsVotesRequestBuilder.posts_votes_endpoint(api_token_authenticator, post["id"])
.with_start_time(self._config["start_date"])
.with_page_size(100)
.build(),
PostsVotesRequestBuilder.posts_votes_endpoint(api_token_authenticator, post["id"]).with_page_size(100).build(),
ErrorResponseBuilder.response_with_status(500).build(),
)

with patch("time.sleep", return_value=None):
output = read_stream("post_votes", SyncMode.full_refresh, self._config)

assert len(output.records) == 0

error_logs = get_log_messages_by_log_level(output.logs, LogLevel.ERROR)
assert any(["Internal server error" in error for error in error_logs])
error_message = output.errors[-1].trace.error.internal_message
assert error_message == "Internal server error."


@freezegun.freeze_time(_NOW.isoformat())
Expand Down Expand Up @@ -164,22 +157,25 @@ def test_given_no_state_and_successful_sync_when_read_then_set_state_to_now(self
posts_record_builder = given_posts(http_mocker, string_to_datetime(self._config["start_date"]), api_token_authenticator)

post = posts_record_builder.build()
post_comments_record_builder = PostsVotesRecordBuilder.posts_votes_record()
post_votes_record_builder = PostsVotesRecordBuilder.posts_votes_record()

http_mocker.get(
PostsVotesRequestBuilder.posts_votes_endpoint(api_token_authenticator, post["id"])
.with_start_time(self._config["start_date"])
.with_page_size(100)
.build(),
PostsVotesResponseBuilder.posts_votes_response().with_record(post_comments_record_builder).build(),
PostsVotesRequestBuilder.posts_votes_endpoint(api_token_authenticator, post["id"]).with_page_size(100).build(),
PostsVotesResponseBuilder.posts_votes_response().with_record(post_votes_record_builder).build(),
)

output = read_stream("post_votes", SyncMode.incremental, self._config)
assert len(output.records) == 1

post_comment = post_comments_record_builder.build()
post_comment = post_votes_record_builder.build()
assert output.most_recent_state.stream_descriptor.name == "post_votes"
assert output.most_recent_state.stream_state == AirbyteStateBlob({"updated_at": post_comment["updated_at"]})
assert output.most_recent_state.stream_state.__dict__ == {
"lookback_window": 0,
"parent_state": {"posts": {"updated_at": post["updated_at"]}},
"state": {"created_at": post_comment["updated_at"]},
"states": [{"cursor": {"created_at": post_comment["updated_at"]}, "partition": {"id": 7253351904271, "parent_slice": {}}}],
"use_global_cursor": False,
}

@HttpMocker()
def test_given_state_and_pagination_when_read_then_return_records(self, http_mocker):
Expand All @@ -203,10 +199,7 @@ def test_given_state_and_pagination_when_read_then_return_records(self, http_moc

# Read first page request mock
http_mocker.get(
PostsVotesRequestBuilder.posts_votes_endpoint(api_token_authenticator, post["id"])
.with_start_time(datetime_to_string(state_start_date))
.with_page_size(100)
.build(),
PostsVotesRequestBuilder.posts_votes_endpoint(api_token_authenticator, post["id"]).with_page_size(100).build(),
PostsVotesResponseBuilder.posts_votes_response().with_pagination().with_record(post_comments_first_record_builder).build(),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ def query_params(self):
if self._start_time:
params["start_time"] = self._start_time
if self._page_size:
params["page[size]"] = self._page_size
params["per_page"] = self._page_size
if self._page_after:
params["page[after]"] = self._page_after
return params

def with_start_time(self, start_time: int) -> "PostsVotesRequestBuilder":
self._start_time: int = calendar.timegm(pendulum.parse(start_time).utctimetuple())
return self

def with_page_size(self, page_size: int) -> "PostsVotesRequestBuilder":
self._page_size: int = page_size
return self
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.

import calendar

import pendulum
from collections.abc import Mapping

from .base_request_builder import ZendeskSupportBaseRequestBuilder
from .request_authenticators.authenticator import Authenticator
Expand All @@ -17,20 +15,16 @@ def __init__(self, subdomain: str, resource: str) -> None:
super().__init__(subdomain, resource)
self._start_time: int = None
self._page_size: int = None
self._sorting: Mapping[str, str] = {"sort_by": "updated_at"}

@property
def query_params(self):
params = super().query_params or {}
if self._start_time:
params["start_time"] = self._start_time
if self._page_size:
params["page[size]"] = self._page_size
params.update(self._sorting)
return params

def with_start_time(self, start_time: int) -> "PostsRequestBuilder":
self._start_time: int = calendar.timegm(pendulum.parse(start_time).utctimetuple())
return self

def with_page_size(self, page_size: int) -> "PostsRequestBuilder":
self._page_size: int = page_size
return self
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import pytest
import requests

from source_zendesk_support.source import SourceZendeskSupport
from source_zendesk_support.streams import Users
from source_zendesk_support.streams import UserSettingsStream

_ANY_ATTEMPT_COUNT = 10

Expand Down Expand Up @@ -39,7 +40,7 @@ def test_backoff(requests_mock, config, x_rate_limit, retry_after, expected):
# create client
config = prepare_config(config)
# test stream
test_stream = Users(**config)
test_stream = UserSettingsStream(**config)

url = f"{test_stream.url_base}{test_stream.path()}/count.json"
requests_mock.get(url, json=test_response_json, headers=test_response_header, status_code=429)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
"event_type": "Audit",
}
],
"next_page": "https://subdomain.zendesk.com/api/v2/stream.json?&start_time=1122334455&page=2",
"count": 215,
"end_of_stream": False,
"end_of_stream": True,
"end_time": 1647532987,
}
Loading

0 comments on commit 521a0e4

Please sign in to comment.