Skip to content

Commit

Permalink
Merge pull request #37 from robberwick/dry-test-fixtures
Browse files Browse the repository at this point in the history
refactor test fixtures to reduce repetition
  • Loading branch information
robberwick authored Nov 16, 2024
2 parents 30039c0 + 0fc1a9e commit b69b3ae
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 61 deletions.
54 changes: 11 additions & 43 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,27 @@ def mocked_responses():


@pytest.fixture
def login_response_json():
with open(Path(__file__).parent / "data" / "login_response.json") as f:
return json.loads(f.read())
def get_response_json():
def _load_json(filename: str) -> dict:
with open(Path(__file__).parent / "data" / filename) as f:
return json.loads(f.read())


@pytest.fixture
def graph_response_json():
with open(Path(__file__).parent / "data" / "graph_response.json") as f:
return json.loads(f.read())


@pytest.fixture
def graph_response_no_sd_json():
with open(Path(__file__).parent / "data" / "graph_response_no_sd.json") as f:
return json.loads(f.read())


@pytest.fixture
def terms_of_use_response_json():
with open(Path(__file__).parent / "data" / "terms_of_use_response.json") as f:
return json.loads(f.read())


@pytest.fixture
def privacy_policy_response_json():
with open(Path(__file__).parent / "data" / "privacy_policy_response.json") as f:
return json.loads(f.read())


@pytest.fixture
def redirect_response_json():
with open(Path(__file__).parent / "data" / "redirect_response.json") as f:
return json.loads(f.read())
return _load_json


@pytest.fixture
def email_verification_response_json():
with open(Path(__file__).parent / "data" / "email_verification_response.json") as f:
return json.loads(f.read())
def graph_response_json(get_response_json):
return get_response_json("graph_response.json")


@pytest.fixture
def graph_response_no_alarm_rules_c_json():
with open(
Path(__file__).parent / "data" / "graph_response_no_alarm_rules_c.json"
) as f:
return json.loads(f.read())
def graph_response_no_sd_json(get_response_json):
return get_response_json("graph_response_no_sd.json")


@pytest.fixture
def logbook_response_json():
with open(Path(__file__).parent / "data" / "logbook_response.json") as f:
return json.loads(f.read())
def graph_response_no_alarm_rules_c_json(get_response_json):
return get_response_json("graph_response_no_alarm_rules_c.json")


@dataclass
Expand Down
28 changes: 11 additions & 17 deletions tests/test_client_authentication.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

import pytest
import requests
import responses
Expand All @@ -12,11 +10,7 @@
RedirectError,
)
from pylibrelinkup.exceptions import PrivacyPolicyError, EmailVerificationError
from pylibrelinkup.models.login import (
LoginResponse,
)
from tests.conftest import mocked_responses, pylibrelinkup_client
from tests.factories import LoginResponseFactory
from tests.conftest import mocked_responses, pylibrelinkup_client, get_response_json


def test_client_uses_correct_api_url_default():
Expand All @@ -41,13 +35,13 @@ def test_authenticate_raises_error_on_incorrect_login(


def test_authenticate_sets_token_on_correct_login(
mocked_responses, pylibrelinkup_client, login_response_json
mocked_responses, pylibrelinkup_client, get_response_json
):
"""Test that the authenticate method sets the token on a successful login."""
mocked_responses.add(
responses.POST,
f"{pylibrelinkup_client.api_url.value}/llu/auth/login",
json=login_response_json,
json=get_response_json("login_response.json"),
status=200,
)

Expand Down Expand Up @@ -87,14 +81,14 @@ def test_authenticate_raises_error_on_http_error(


def test_authenticate_raises_terms_of_use_error(
mocked_responses, pylibrelinkup_client, terms_of_use_response_json
mocked_responses, pylibrelinkup_client, get_response_json
):
"""Test that the authenticate method raises a TermsOfUseError, when the user needs to accept terms of use."""

mocked_responses.add(
responses.POST,
f"{pylibrelinkup_client.api_url.value}/llu/auth/login",
json=terms_of_use_response_json,
json=get_response_json("terms_of_use_response.json"),
status=200,
)

Expand All @@ -103,14 +97,14 @@ def test_authenticate_raises_terms_of_use_error(


def test_authenticate_raises_privacy_policy_error(
mocked_responses, pylibrelinkup_client, privacy_policy_response_json
mocked_responses, pylibrelinkup_client, get_response_json
):
"""Test that the authenticate method raises a PrivacyPolicyError, when the user needs to accept the privacy policy."""

mocked_responses.add(
responses.POST,
f"{pylibrelinkup_client.api_url.value}/llu/auth/login",
json=privacy_policy_response_json,
json=get_response_json("privacy_policy_response.json"),
status=200,
)

Expand All @@ -119,14 +113,14 @@ def test_authenticate_raises_privacy_policy_error(


def test_authenticate_raise_email_verification_error(
mocked_responses, pylibrelinkup_client, email_verification_response_json
mocked_responses, pylibrelinkup_client, get_response_json
):
"""Test that the authenticate method raises an EmailVerificationError, when the user needs to verify their email."""

mocked_responses.add(
responses.POST,
f"{pylibrelinkup_client.api_url.value}/llu/auth/login",
json=email_verification_response_json,
json=get_response_json("email_verification_response.json"),
status=200,
)

Expand All @@ -135,14 +129,14 @@ def test_authenticate_raise_email_verification_error(


def test_redirection_response_raises_redirect_error(
mocked_responses, pylibrelinkup_client, redirect_response_json
mocked_responses, pylibrelinkup_client, get_response_json
):
"""Test that the authenticate method raises a RedirectError, when the user is redirected to a different region."""

mocked_responses.add(
responses.POST,
f"{pylibrelinkup_client.api_url.value}/llu/auth/login",
json=redirect_response_json,
json=get_response_json("redirect_response.json"),
status=200,
)

Expand Down
6 changes: 5 additions & 1 deletion tests/test_client_logbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from pylibrelinkup import AuthenticationError
from pylibrelinkup.models.data import GlucoseMeasurement
from tests.conftest import logbook_response_json
from tests.factories import PatientFactory


Expand Down Expand Up @@ -110,3 +109,8 @@ def test_logbook_raises_value_error_for_invalid_patient_id_type(pylibrelinkup_cl

with pytest.raises(ValueError, match="Invalid patient_identifier"):
pylibrelinkup_client.client.logbook(123456) # type: ignore


@pytest.fixture
def logbook_response_json(get_response_json):
return get_response_json("logbook_response.json")

0 comments on commit b69b3ae

Please sign in to comment.