Skip to content

Commit

Permalink
Source Linkedin Ads: increase test coverage (#32343)
Browse files Browse the repository at this point in the history
Co-authored-by: roman-yermilov-gl <roman-yermilov-gl@users.noreply.github.com>
  • Loading branch information
roman-yermilov-gl and roman-yermilov-gl authored Nov 9, 2023
1 parent 06b22cd commit f8de88d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#


from unittest.mock import Mock, patch

import pytest
import requests
from airbyte_cdk.sources.streams.http.requests_native_auth import Oauth2Authenticator, TokenAuthenticator
Expand Down Expand Up @@ -101,6 +103,25 @@ def test_streams(self, stream_cls):
if stream_cls in streams:
assert isinstance(stream, stream_cls)

def test_custom_streams(self):
config = {"ad_analytics_reports": [{"name": "ShareAdByMonth", "pivot_by": "COMPANY", "time_granularity": "MONTHLY"}], **TEST_CONFIG}
for stream in self._instance.get_custom_ad_analytics_reports(config=config):
assert isinstance(stream, AdCampaignAnalytics)

@patch("source_linkedin_ads.source.Accounts.check_availability")
def test_check_connection(self, check_availability_mock):
check_availability_mock.return_value = (True, None)
is_available, error = self._instance.check_connection(logger=Mock(), config=TEST_CONFIG)
assert is_available
assert not error

@patch("source_linkedin_ads.source.Accounts.check_availability")
def test_check_connection_failure(self, check_availability_mock):
check_availability_mock.side_effect = Exception("Not available")
is_available, error = self._instance.check_connection(logger=Mock(), config=TEST_CONFIG)
assert not is_available
assert str(error) == "Not available"

@pytest.mark.parametrize(
"stream_cls, stream_slice, expected",
[
Expand Down Expand Up @@ -136,11 +157,17 @@ def test_accounts(self):
result = self.stream.accounts
assert result == ",".join(map(str, TEST_CONFIG["account_ids"]))

def test_next_page_token(self, requests_mock):
requests_mock.get(self.url, json={"elements": []})
@pytest.mark.parametrize(
"response_json, expected",
(
({"elements": []}, None),
({"elements": [{"data": []}] * 500, "paging": {"start": 0}}, {"start": 500}),
),
)
def test_next_page_token(self, requests_mock, response_json, expected):
requests_mock.get(self.url, json=response_json)
test_response = requests.get(self.url)

expected = None
result = self.stream.next_page_token(test_response)
assert expected == result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
},
{"or": {"urn:li:adTargetingFacet:locations": ["urn:li:geo:103644278"]}},
{"or": {"urn:li:adTargetingFacet:interfaceLocales": ["urn:li:locale:en_US"]}},
{"or": {"empty_dict_with_empty_list_of_dicts": [{"empty_dict_value": "the value"}]}},
{"or": {"empty_dict_with_empty_dict": {"empty_dict_value": "the value"}}},
{"or": {"empty_dict_with_empty_list": []}}, # dict is present, but list is empty
{"or": {}}, # empty dict
]
Expand Down Expand Up @@ -92,6 +94,14 @@
"type": "urn:li:adTargetingFacet:interfaceLocales",
"values": ["urn:li:locale:en_US"],
},
{
"type": "empty_dict_with_empty_list_of_dicts",
"values": [{"empty_dict_value": "the value"}],
},
{
"type": "empty_dict_with_empty_dict",
"values": [{"empty_dict_value": "the value"}],
},
{
"type": "empty_dict_with_empty_list",
"values": [],
Expand Down

0 comments on commit f8de88d

Please sign in to comment.