Skip to content

Commit

Permalink
refactor client secret resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Payne committed Jan 26, 2023
1 parent 44d7308 commit ba35f4d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 45 deletions.
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
}
17 changes: 0 additions & 17 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
"""Conftest fixtures."""

import json
import os

import pytest

from .utilities import get_secrets_dict

pytest_plugins = ("singer_sdk.testing.pytest_plugin",)


@pytest.fixture(scope="module")
def config():
"""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
os.remove(secrets_path)
45 changes: 17 additions & 28 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
"""Tests standard tap features using the built-in SDK tests library."""

import os
from datetime import datetime, timedelta, timezone

import pytest
from singer_sdk.testing import get_tap_test_class

from tap_google_analytics.tap import TapGoogleAnalytics

from .utilities import get_secrets_dict

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": f"{os.path.dirname(__file__)}/test_data/client_secrets.json",
}
from .utilities import create_secrets_file, get_secrets_dict

# 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"),
Expand All @@ -25,25 +17,22 @@
}


# Run standard built-in tap tests from the SDK with SAMPLE_CONFIG_SERVICE
TapGoogleAnalyticsService = get_tap_test_class(
tap_class=TapGoogleAnalytics, config=SAMPLE_CONFIG_SERVICE
)


class TestTapGoogleAnalyticsService(TapGoogleAnalyticsService):
@pytest.fixture
def resource(self, config):
yield


# Run standard built-in tap tests from the SDK with SAMPLE_CONFIG_CLIENT_SECRETS
TapGoogleAnalyticsClientSecrets = get_tap_test_class(
TestTapGoogleAnalyticsClientSecrets = get_tap_test_class(
tap_class=TapGoogleAnalytics, config=SAMPLE_CONFIG_CLIENT_SECRETS
)


class TestTapGoogleAnalyticsClientSecrets(TapGoogleAnalyticsClientSecrets):
@pytest.fixture
def resource(self, config):
yield
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,
}

TestTapGoogleAnalyticsService = get_tap_test_class(
tap_class=TapGoogleAnalytics, config=SAMPLE_CONFIG_SERVICE
)
11 changes: 11 additions & 0 deletions 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

0 comments on commit ba35f4d

Please sign in to comment.