Skip to content

Commit

Permalink
added additional type annotations to conftest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
charlottekostelic committed Nov 7, 2024
1 parent a5b0fff commit 78068fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
20 changes: 10 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


@pytest.fixture
def live_keys():
def live_keys() -> None:
if os.name == "nt" and not os.getenv("GITHUB_ACTIONS"):
fh = os.path.join(os.environ["USERPROFILE"], ".oclc/nyp_wc_test.json")
with open(fh, "r") as file:
Expand Down Expand Up @@ -40,12 +40,12 @@ def stub_marc21() -> bytes:

class FakeUtcNow(datetime.datetime):
@classmethod
def now(cls, tzinfo=datetime.timezone.utc):
def now(cls, tzinfo=datetime.timezone.utc) -> "FakeUtcNow":
return cls(2020, 1, 1, 17, 0, 0, 0, tzinfo=datetime.timezone.utc)


@pytest.fixture
def mock_now(monkeypatch):
def mock_now(monkeypatch) -> None:
monkeypatch.setattr(datetime, "datetime", FakeUtcNow)


Expand Down Expand Up @@ -130,7 +130,7 @@ def __init__(self, http_code) -> None:


@pytest.fixture
def mock_session_response(request, monkeypatch):
def mock_session_response(request, monkeypatch) -> None:
"""
Use together with `pytest.mark.http_code` marker to pass
specific HTTP code to be returned to simulate various
Expand Down Expand Up @@ -165,44 +165,44 @@ def mock_oauth_server_response(


@pytest.fixture
def mock_successful_post_token_response(mock_now, monkeypatch):
def mock_successful_post_token_response(mock_now, monkeypatch) -> None:
def mock_oauth_server_response(*args, **kwargs):
return MockAuthServerResponseSuccess()

monkeypatch.setattr(requests, "post", mock_oauth_server_response)


@pytest.fixture
def mock_failed_post_token_response(monkeypatch):
def mock_failed_post_token_response(monkeypatch) -> None:
def mock_oauth_server_response(*args, **kwargs):
return MockAuthServerResponseFailure()

monkeypatch.setattr(requests, "post", mock_oauth_server_response)


@pytest.fixture
def mock_unexpected_error(monkeypatch):
def mock_unexpected_error(monkeypatch) -> None:
monkeypatch.setattr("requests.post", MockUnexpectedException)
monkeypatch.setattr("requests.get", MockUnexpectedException)
monkeypatch.setattr("requests.Session.send", MockUnexpectedException)


@pytest.fixture
def mock_timeout(monkeypatch):
def mock_timeout(monkeypatch) -> None:
monkeypatch.setattr("requests.post", MockTimeout)
monkeypatch.setattr("requests.get", MockTimeout)
monkeypatch.setattr("requests.Session.send", MockTimeout)


@pytest.fixture
def mock_connection_error(monkeypatch):
def mock_connection_error(monkeypatch) -> None:
monkeypatch.setattr("requests.post", MockConnectionError)
monkeypatch.setattr("requests.get", MockConnectionError)
monkeypatch.setattr("requests.Session.send", MockConnectionError)


@pytest.fixture
def mock_retry_error(monkeypatch):
def mock_retry_error(monkeypatch) -> None:
monkeypatch.setattr("requests.post", MockRetryError)
monkeypatch.setattr("requests.get", MockRetryError)
monkeypatch.setattr("requests.Session.send", MockRetryError)
Expand Down
14 changes: 8 additions & 6 deletions tests/webtests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from collections.abc import Callable
import inspect
import os
from typing import Generator
import yaml
import pytest
import requests
Expand All @@ -9,20 +11,20 @@


@pytest.fixture
def live_token():
def live_token() -> Generator[WorldcatAccessToken, None, None]:
"""
Gets live token from environment variables. For use with live tests so that
the service does not need to request a new token for each test.
"""
yield WorldcatAccessToken(
key=os.getenv("WCKey"),
secret=os.getenv("WCSecret"),
scopes=os.getenv("WCScopes"),
key=os.environ["WCKey"],
secret=os.environ["WCSecret"],
scopes=os.environ["WCScopes"],
)


@pytest.fixture
def method_params():
def method_params() -> Callable:
"""
Inspects signature of `MetadataSession` method and and returns list
of parameters. Filters "responseFormat", "hooks", and "Accept" parameters
Expand Down Expand Up @@ -52,7 +54,7 @@ def metadata_session_open_api_spec() -> dict:


@pytest.fixture
def endpoint_params(metadata_session_open_api_spec):
def endpoint_params(metadata_session_open_api_spec) -> Callable:
"""
Reads yaml file from OCLC API documentation (available here:
https://developer.api.oclc.org/wc-metadata-v2) and returns list of
Expand Down

0 comments on commit 78068fe

Please sign in to comment.