Skip to content

Commit

Permalink
Source Google Analytics Data API: increase test coverage (#32289)
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 ed712ab commit 2007fda
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ def config_without_date_range():
"property_id": "108176369",
"property_ids": ["108176369"],
"credentials": {"auth_type": "Service", "credentials_json": json_credentials},
"dimensions": ["deviceCategory", "operatingSystem", "browser"],
"metrics": [
"totalUsers",
"newUsers",
"sessions",
"sessionsPerUser",
"averageSessionDuration",
"screenPageViews",
"screenPageViewsPerSession",
"bounceRate",
],
"custom_reports": [],
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,25 @@ def test_check(requests_mock, config_gen, config_values, is_successful, message)
"rows": [],
},
)

source = SourceGoogleAnalyticsDataApi()
logger = MagicMock()
assert source.check(logger, config_gen(**config_values)) == AirbyteConnectionStatus(status=is_successful, message=message)


def test_check_failure(requests_mock, config_gen):
requests_mock.register_uri(
"POST", "https://oauth2.googleapis.com/token", json={"access_token": "access_token", "expires_in": 3600, "token_type": "Bearer"}
)
requests_mock.register_uri(
"GET", "https://analyticsdata.googleapis.com/v1beta/properties/UA-11111111/metadata", json={}, status_code=403
)

source = SourceGoogleAnalyticsDataApi()
logger = MagicMock()
assert source.check(logger, config_gen(**config_values)) == AirbyteConnectionStatus(status=is_successful, message=message)
if not is_successful:
with pytest.raises(AirbyteTracedException) as e:
source.check(logger, config_gen(property_ids=["UA-11111111"]))
assert e.value.failure_type == FailureType.config_error
with pytest.raises(AirbyteTracedException) as e:
source.check(logger, config_gen(property_ids=["UA-11111111"]))
assert e.value.failure_type == FailureType.config_error
assert "Access was denied to the property ID entered." in e.value.message


@pytest.mark.parametrize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,39 @@


@pytest.fixture
def patch_base_class(mocker, config):
def patch_base_class(mocker, config, config_without_date_range):
# Mock abstract methods to enable instantiating abstract class
mocker.patch.object(GoogleAnalyticsDataApiBaseStream, "path", f"{random.randint(100000000, 999999999)}:runReport")
mocker.patch.object(GoogleAnalyticsDataApiBaseStream, "primary_key", "test_primary_key")
mocker.patch.object(GoogleAnalyticsDataApiBaseStream, "__abstractmethods__", set())

return {"config": config}
return {"config": config, "config_without_date_range": config_without_date_range}


def test_json_schema(requests_mock, patch_base_class):
requests_mock.register_uri(
"POST", "https://oauth2.googleapis.com/token", json={"access_token": "access_token", "expires_in": 3600, "token_type": "Bearer"}
)
requests_mock.register_uri(
"GET",
"https://analyticsdata.googleapis.com/v1beta/properties/108176369/metadata",
json={
"dimensions": [{"apiName": "date"}, {"apiName": "country"}, {"apiName": "language"}, {"apiName": "browser"}],
"metrics": [{"apiName": "totalUsers"}, {"apiName": "screenPageViews"}, {"apiName": "sessions"}],
},
)
schema = GoogleAnalyticsDataApiBaseStream(
authenticator=MagicMock(), config={"authenticator": MagicMock(), **patch_base_class["config_without_date_range"]}
).get_json_schema()

for d in patch_base_class["config_without_date_range"]["dimensions"]:
assert d in schema["properties"]

for p in patch_base_class["config_without_date_range"]["metrics"]:
assert p in schema["properties"]

assert "startDate" in schema["properties"]
assert "endDate" in schema["properties"]


def test_request_params(patch_base_class):
Expand Down

0 comments on commit 2007fda

Please sign in to comment.