Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use new SDK tests framework #99

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"python.testing.pytestArgs": ["tests"],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
752 changes: 433 additions & 319 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ license = "Apache 2.0"

[tool.poetry.dependencies]
python = ">=3.7.1,<3.12" # Tested up to 3.11
singer-sdk = "^0.17.0"
singer-sdk = {git = "https://github.com/meltano/sdk.git", rev = "kgpayne/issue1169", extras=["testing"]}
google-api-python-client = "^2.73.0"
oauth2client = "4.1.3"

[tool.poetry.dev-dependencies]
pytest = "^7.0.1"
tox = "^3.28.0"
flake8 = "^5.0.4"
black = "^22.10"
Expand Down
4 changes: 2 additions & 2 deletions tap_google_analytics/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ def _request_records(self, context: Optional[dict]) -> Iterable[dict]:
# Cycle until get_next_page_token() no longer returns a value
finished = not next_page_token

def _get_next_page_token(self, response: dict) -> Any:
def _get_next_page_token(self, response: dict) -> Any: # noqa: D417
"""Return token identifying next page or None if all records have been read.

Args:
----
response: A dict object.
response: The response in dict form.

Return:
------
Expand Down
18 changes: 0 additions & 18 deletions tap_google_analytics/tests/conftest.py

This file was deleted.

53 changes: 0 additions & 53 deletions tap_google_analytics/tests/test_core.py

This file was deleted.

File renamed without changes.
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Conftest fixtures."""

pytest_plugins = ("singer_sdk.testing.pytest_plugin",)
53 changes: 53 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Tests standard tap features using the built-in SDK tests library."""

from datetime import datetime, timedelta, timezone

import pytest
from singer_sdk.testing import SuiteConfig, get_tap_test_class

from tap_google_analytics.tap import TapGoogleAnalytics

from .utilities import create_secrets_file, get_secrets_dict

# suite config
suite_config = SuiteConfig(ignore_no_records_for_streams=["pages"])


# Run standard built-in tap tests from the SDK with SAMPLE_CONFIG_CLIENT_SECRETS
SAMPLE_CONFIG_CLIENT_SECRETS = {
"view_id": "188392047",
"end_date": datetime.now(timezone.utc).strftime("%Y-%m-%d"),
"start_date": (datetime.now(timezone.utc) - timedelta(days=2)).strftime("%Y-%m-%d"),
"client_secrets": get_secrets_dict(),
}


TestTapGoogleAnalyticsClientSecrets = get_tap_test_class(
tap_class=TapGoogleAnalytics,
config=SAMPLE_CONFIG_CLIENT_SECRETS,
suite_config=suite_config,
)


with create_secrets_file() as secrets_file_path:
# Run standard built-in tap tests from the SDK with SAMPLE_CONFIG_SERVICE
SAMPLE_CONFIG_SERVICE = {
"view_id": "188392047",
"end_date": datetime.now(timezone.utc).strftime("%Y-%m-%d"),
"start_date": (datetime.now(timezone.utc) - timedelta(days=2)).strftime(
"%Y-%m-%d"
),
"key_file_location": secrets_file_path,
}

TapGoogleAnalyticsService = get_tap_test_class(
tap_class=TapGoogleAnalytics,
config=SAMPLE_CONFIG_SERVICE,
suite_config=suite_config,
)

class TestTapGoogleAnalyticsService(TapGoogleAnalyticsService):
@pytest.fixture(scope="class")
def resource(self):
with create_secrets_file() as secrets_file_path:
yield secrets_file_path
File renamed without changes.
11 changes: 11 additions & 0 deletions tap_google_analytics/tests/utilities.py → tests/utilities.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
"""Test utility funtions."""
import base64
import contextlib
import json
import os


@contextlib.contextmanager
def create_secrets_file():
"""Write secrets file then clean it up after tests."""
secrets_path = f"{os.path.dirname(__file__)}/test_data/client_secrets.json"
with open(secrets_path, "w") as f:
json.dump(get_secrets_dict(), f)
yield secrets_path
os.remove(secrets_path)


def get_secrets_dict():
"""Return a secrets dictionary of based off the env var."""
secrets_var = os.environ["CLIENT_SECRETS"]
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ commands =
poetry run black --check tap_google_analytics/
poetry run flake8 tap_google_analytics
poetry run pydocstyle tap_google_analytics
poetry run mypy tap_google_analytics --exclude='tap_google_analytics/tests'
poetry run mypy tap_google_analytics

[testenv:pytest]
# Run the python tests.
Expand Down Expand Up @@ -44,7 +44,7 @@ commands =
poetry run isort --check tap_google_analytics
poetry run flake8 tap_google_analytics
poetry run pydocstyle tap_google_analytics
poetry run mypy tap_google_analytics --exclude='tap_google_analytics/tests' --ignore-missing-imports
poetry run mypy tap_google_analytics --ignore-missing-imports

[flake8]
ignore = W503
Expand Down