Skip to content

Commit

Permalink
updated unit_tests based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
bazarnov committed Mar 20, 2022
1 parent b3bf785 commit 94c1310
Showing 1 changed file with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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": '<div class="zd-comment" dir="auto">test_comment<br></div>',
"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():
Expand All @@ -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

1 comment on commit 94c1310

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SonarQube Report

SonarQube report for Airbyte Connectors Source Zendesk Support(#11237)

Measures

Name Value Name Value Name Value
Security Rating A Lines to Cover 41 Duplicated Lines (%) 0.0
Duplicated Blocks 0 Reliability Rating A Quality Gate Status OK
Coverage 95.1 Vulnerabilities 0 Bugs 0
Code Smells 8 Lines of Code 432 Blocker Issues 0
Critical Issues 0 Major Issues 14 Minor Issues 48

Detected Issues

Rule File Description Message
python:mypy_return_value (MINOR) source_zendesk_support/streams.py:122 Check that return value is compatible with signature Incompatible return value type (got "Optional[str]", expected "Optional[int]") . Code line: return dict(parse_qsl(urlparse(next_page).query)).get("page") ...
python:mypy_override (MINOR) source_zendesk_support/streams.py:403 Check that method override is compatible with base class Signature of "parse_response" incompatible with supertype "BaseSourceZendeskSupportStream" . Code line: def parse_response(self, response: requests.Response, **kwargs) ->...
python:S5890 (MAJOR) source_zendesk_support/streams.py:376 Values assigned to variables should match their type annotations Assign to "list_entities_from_event" a value of type "list[str]" instead of "NoneType" or update its type hint.
python:S5890 (MAJOR) source_zendesk_support/streams.py:377 Values assigned to variables should match their type annotations Assign to "sideload_param" a value of type "str" instead of "NoneType" or update its type hint.
python:S5890 (MAJOR) source_zendesk_support/streams.py:378 Values assigned to variables should match their type annotations Assign to "event_type" a value of type "str" instead of "NoneType" or update its type hint.
flake8:W293 (MAJOR) source_zendesk_support/streams.py blank line contains whitespace blank line contains whitespace
flake8:W293 (MAJOR) source_zendesk_support/streams.py blank line contains whitespace blank line contains whitespace
flake8:W293 (MAJOR) source_zendesk_support/streams.py blank line contains whitespace blank line contains whitespace
flake8:W293 (MAJOR) source_zendesk_support/streams.py blank line contains whitespace blank line contains whitespace
flake8:W293 (MAJOR) source_zendesk_support/streams.py blank line contains whitespace blank line contains whitespace
flake8:W293 (MAJOR) source_zendesk_support/streams.py blank line contains whitespace blank line contains whitespace
python:black_need_format (MINOR) source_zendesk_support/streams.py Please run one of the commands: "black --config ./pyproject.toml <path_to_updated_folder>" or "./gradlew format" 1 code part(s) should be updated.
python:mypy_union_attr (MINOR) source_zendesk_support/streams.py Check that attribute exists in each item of a union Item "None" of "Optional[Dict[Any, Any]]" has no attribute "get" . Code line: current_state = stream_state.get(self.cursor_field, {})
python:mypy_union_attr (MINOR) source_zendesk_support/streams.py Check that attribute exists in each item of a union Item "None" of "Optional[Dict[Any, Any]]" has no attribute "get" . Code line: updated_state = event.get(self.cursor_field, {})
python:mypy_misc (MINOR) source_zendesk_support/streams.py Miscellaneous other checks Incompatible types in "yield" (actual type "Optional[Dict[Any, Any]]", expected type "Mapping[Any, Any]") . Code line: yield event
python:mypy_arg_type (MINOR) source_zendesk_support/streams.py Check argument types in calls Argument 2 to "filter_by_state" of "ZendeskSupportTicektEventsExportStream" has incompatible type "Mapping[str, Any]"; expected "Optional[Dict[Any, Any]]" . Code line: ... yield from self.filter_by_state(event, stream_state)
flake8:W293 (MAJOR) source_zendesk_support/streams.py blank line contains whitespace blank line contains whitespace
python:mypy_assignment (MINOR) source_zendesk_support/streams.py:376 Check that assigned value is compatible with target Incompatible types in assignment (expression has type "None", variable has type "List[str]") . Code line: list_entities_from_event: List[str] = None
python:mypy_assignment (MINOR) source_zendesk_support/streams.py:377 Check that assigned value is compatible with target Incompatible types in assignment (expression has type "None", variable has type "str") . Code line: sideload_param: str = None
python:mypy_assignment (MINOR) source_zendesk_support/streams.py:378 Check that assigned value is compatible with target Incompatible types in assignment (expression has type "None", variable has type "str") . Code line: event_type: str = None
python:mypy_override (MINOR) source_zendesk_support/streams.py:345 Check that method override is compatible with base class Signature of "request_params" incompatible with supertype "SourceZendeskSupportFullRefreshStream" . Code line: def request_params(
python:mypy_override (MINOR) source_zendesk_support/streams.py:395 Check that method override is compatible with base class Signature of "request_params" incompatible with supertype "SourceZendeskSupportFullRefreshStream" . Code line: def request_params(
python:mypy_union_attr (MINOR) source_zendesk_support/streams.py Check that attribute exists in each item of a union Item "None" of "Optional[List[Any]]" has no attribute "iter" (not iterable) . Code line: for prop in props:
python:mypy_union_attr (MINOR) source_zendesk_support/streams.py Check that attribute exists in each item of a union Item "None" of "Optional[Dict[Any, Any]]" has no attribute "get" . Code line: target_prop = record.get(prop)
python:mypy_index (MINOR) source_zendesk_support/streams.py Check indexing operations Unsupported target for indexed assignment ("Optional[Dict[Any, Any]]") . Code line: event[prop] = target_prop if target_prop else None
python:mypy_return_value (MINOR) source_zendesk_support/streams.py Check that return value is compatible with signature Incompatible return value type (got "Optional[Dict[Any, Any]]", expected "MutableMapping[str, Any]") . Code line: return event
python:mypy_attr_defined (MINOR) source_zendesk_support/streams.py:351 Check that attribute exists Module has no attribute "parse" . Code line: parsed_state = calendar.timegm(pendulum.parse(stream_state...
python:mypy_attr_defined (MINOR) source_zendesk_support/streams.py:354 Check that attribute exists Module has no attribute "parse" . Code line: parsed_state = calendar.timegm(pendulum.parse(self.start...
python:S1066 (MAJOR) source_zendesk_support/source.py:58 Collapsible "if" statements should be merged Merge this if statement with the enclosing one.
python:mypy_no_any_return (MINOR) source_zendesk_support/source.py:64 Reject returning value with "Any" type if return type is not "Any" Returning Any from function declared to return "BasicApiTokenAuthenticator" . Code line: return TokenAuthenticator(token=config["credentials"][...
python:mypy_arg_type (MINOR) source_zendesk_support/source.py:81 Check argument types in calls Argument "start_date" to "UserSettingsStream" has incompatible type "None"; expected "str" . Code line: ...nfig["subdomain"], authenticator=auth, start_date=None).get_settings()
python:mypy_return_value (MINOR) source_zendesk_support/streams.py Check that return value is compatible with signature Incompatible return value type (got "Optional[str]", expected "Optional[int]") . Code line: return dict(parse_qsl(urlparse(next_page).query)).get("pag...
python:mypy_return_value (MINOR) source_zendesk_support/streams.py Check that return value is compatible with signature Incompatible return value type (got "Optional[str]", expected "Optional[int]") . Code line: return dict(parse_qsl(urlparse(next_page).query)).get("pag...
python:mypy_import (MINOR) source_zendesk_support/streams.py:25 Require that imported module can be found or has stubs Library stubs not installed for "requests.auth" (or incompatible with Python 3.7) . Code line: from requests.auth import AuthBase
python:mypy_no_any_return (MINOR) source_zendesk_support/streams.py:62 Reject returning value with "Any" type if return type is not "Any" Returning Any from function declared to return "Future[Any]" . Code line: return self.executor.submit(func, request, **kwargs)
python:S5890 (MAJOR) source_zendesk_support/streams.py:146 Values assigned to variables should match their type annotations Assign to "response_list_name" a value of type "str" instead of "NoneType" or update its type hint.
python:mypy_assignment (MINOR) source_zendesk_support/streams.py:146 Check that assigned value is compatible with target Incompatible types in assignment (expression has type "None", variable has type "str") . Code line: response_list_name: str = None
python:mypy_assignment (MINOR) source_zendesk_support/streams.py:147 Check that assigned value is compatible with target Incompatible types in assignment (expression has type "None", variable has type "SourceZendeskSupportStream") . Code line: parent: "SourceZendeskSupportStream" = None
python:mypy_assignment (MINOR) source_zendesk_support/streams.py:148 Check that assigned value is compatible with target Incompatible types in assignment (expression has type "None", variable has type "deque[Any]") . Code line: future_requests: deque = None
python:mypy_assignment (MINOR) source_zendesk_support/streams.py:186 Check that assigned value is compatible with target Incompatible types in assignment (expression has type "Optional[Any]", variable has type "str") . Code line: start_date = stream_state.get(self.cursor_field)
python:mypy_attr_defined (MINOR) source_zendesk_support/streams.py:245 Check that attribute exists Module has no attribute "parse" . Code line: ... start_time = current_state or calendar.timegm(pendulum.parse(self._...
python:mypy_arg_type (MINOR) source_zendesk_support/streams.py:288 Check argument types in calls Argument "stream_state" to "parse_response" of "BaseSourceZendeskSupportStream" has incompatible type "Optional[Mapping[str, Any]]"; expected "Mapping[str, Any]" . Code line: ... from self.parse_response(response, stream_state=stream_state, stream_...
python:S5890 (MAJOR) source_zendesk_support/streams.py:298 Values assigned to variables should match their type annotations Assign to "response_list_name" a value of type "str" instead of "NoneType" or update its type hint.
python:mypy_assignment (MINOR) source_zendesk_support/streams.py:298 Check that assigned value is compatible with target Incompatible types in assignment (expression has type "None", variable has type "str") . Code line: response_list_name: str = None
python:mypy_return (MINOR) source_zendesk_support/streams.py:339 Check that function always returns a value Missing return statement . Code line: def next_page_token(self, response: requests.Response) -> Optional...
python:mypy_attr_defined (MINOR) source_zendesk_support/streams.py:358 Check that attribute exists Module has no attribute "parse" . Code line: ... params = {"start_time": calendar.timegm(pendulum.parse(self._...
python:mypy_override (MINOR) source_zendesk_support/streams.py:569 Check that method override is compatible with base class Signature of "parse_response" incompatible with supertype "BaseSourceZendeskSupportStream" . Code line: def parse_response(self, response: requests.Response, **kwargs) ->...
python:mypy_no_any_return (MINOR) source_zendesk_support/streams.py:577 Reject returning value with "Any" type if return type is not "Any" Returning Any from function declared to return "Mapping[str, Any]" . Code line: return resp
python:mypy_override (MINOR) source_zendesk_support/streams.py:519 Check that method override is compatible with base class Signature of "request_params" incompatible with supertype "SourceZendeskSupportCursorPaginationStream" . Code line: def request_params(self, next_page_token: Mapping[str, Any] = None...
python:mypy_no_any_return (MINOR) source_zendesk_support/streams.py:527 Reject returning value with "Any" type if return type is not "Any" Returning Any from function declared to return "Optional[Mapping[str, Any]]" . Code line: return response.json().get("before_cursor")
python:mypy_arg_type (MINOR) source_zendesk_support/streams.py:468 Check argument types in calls Argument 1 to "str2unixtime" of "BaseSourceZendeskSupportStream" has incompatible type "Optional[Any]"; expected "str" . Code line: start_time = self.str2unixtime((stream_state or {}).get(self.c...
python:mypy_no_any_return (MINOR) source_zendesk_support/streams.py:89 Reject returning value with "Any" type if return type is not "Any" Returning Any from function declared to return "Union[int, float]" . Code line: return super().backoff_time(response)
python:mypy_import (MINOR) source_zendesk_support/source.py:8 Require that imported module can be found or has stubs Library stubs not installed for "requests" (or incompatible with Python 3.7) . Code line: import requests
python:mypy_return (MINOR) source_zendesk_support/source.py:53 Check that function always returns a value Missing return statement . Code line: def get_authenticator(cls, config: Mapping[str, Any]) -> BasicApiT...
python:mypy_valid_type (MINOR) source_zendesk_support/source.py:70 Check that type (annotation) is valid Function "builtins.any" is not valid as a type . Code line: def check_connection(self, logger, config) -> Tuple[bool, any]:
python:mypy_import (MINOR) source_zendesk_support/streams.py:18 Require that imported module can be found or has stubs Library stubs not installed for "pytz" (or incompatible with Python 3.7) . Code line: import pytz
python:mypy_import (MINOR) source_zendesk_support/streams.py:19 Require that imported module can be found or has stubs Library stubs not installed for "requests" (or incompatible with Python 3.7) . Code line: import requests
python:S5886 (MAJOR) source_zendesk_support/streams.py:97 Function return types should be consistent with their type hint Return a value of type "datetime" instead of "NoneType" or update function "str2datetime" type hint.
python:mypy_return_value (MINOR) source_zendesk_support/streams.py:97 Check that return value is compatible with signature Incompatible return value type (got "None", expected "datetime") . Code line: return None
python:mypy_return_value (MINOR) source_zendesk_support/streams.py:312 Check that return value is compatible with signature Incompatible return value type (got "int", expected "Optional[Mapping[str, Any]]") . Code line: return next_page
python:mypy_no_any_return (MINOR) source_zendesk_support/streams.py:322 Reject returning value with "Any" type if return type is not "Any" Returning Any from function declared to return "MutableMapping[str, Any]" . Code line: return params
python:mypy_override (MINOR) source_zendesk_support/streams.py:429 Check that method override is compatible with base class Signature of "request_params" incompatible with supertype "SourceZendeskSupportStream" . Code line: def request_params(self, **kwargs) -> MutableMapping[str, Any]:

Coverage (95.1%)

File Coverage File Coverage
source_zendesk_support/init.py 100.0 source_zendesk_support/source.py 44.4
source_zendesk_support/streams.py 79.9

Please sign in to comment.