Skip to content

Commit

Permalink
Add HTTPXMock testing and drop Python 3.8 support (#298)
Browse files Browse the repository at this point in the history
* Test static data requests.

* Update python version and dependencies

* Move pytest-httpx to dev dependencies

* Removing further references to using 3.8

* poetry update

* restructure test folder

* add static data for service history

---------

Co-authored-by: GitOldGrumpy <[email protected]>
Co-authored-by: Simon Hörrle <[email protected]>
  • Loading branch information
3 people authored Jan 26, 2024
1 parent 43c8189 commit 1a17a62
Show file tree
Hide file tree
Showing 38 changed files with 2,011 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
matrix:
os: ["macos-latest", "ubuntu-latest", "windows-latest"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
fail-fast: false
steps:
- name: "⤵️ Check out code from GitHub"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: "🐍 Set up Python"
uses: "actions/setup-python@v5"
with:
python-version: 3.8
python-version: 3.9
- name: "⚙️ Install Poetry"
uses: "abatilo/[email protected]"
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: "Pytest"
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
runs-on: "ubuntu-latest"
env:
OS: "ubuntu-latest"
Expand Down
5 changes: 5 additions & 0 deletions mytoyota/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ async def _authenticate(self):
self._authenticate_url, json=data
) # , headers=standard_headers)
_LOGGER.debug(format_httpx_response(resp))

if resp.status_code != HTTPStatus.OK:
raise ToyotaLoginError(
f"Authentication Failed. {resp.status_code}, {resp.text}."
Expand All @@ -123,6 +124,7 @@ async def _authenticate(self):
headers={"cookie": f"iPlanetDirectoryPro={data['tokenId']}"},
)
_LOGGER.debug(format_httpx_response(resp))

if resp.status_code != HTTPStatus.FOUND:
raise ToyotaLoginError(f"Authorization failed. {resp.status_code}, {resp.text}.")
authentication_code = parse.parse_qs(
Expand All @@ -142,6 +144,7 @@ async def _authenticate(self):
},
)
_LOGGER.debug(format_httpx_response(resp))

if resp.status_code != HTTPStatus.OK:
raise ToyotaLoginError(f"Token retrieval failed. {resp.status_code}, {resp.text}.")

Expand All @@ -168,6 +171,7 @@ async def _refresh_tokens(self) -> None:
},
)
_LOGGER.debug(format_httpx_response(resp))

if resp.status_code != HTTPStatus.OK:
raise ToyotaLoginError(f"Token refresh failed. {resp.status_code}, {resp.text}.")

Expand Down Expand Up @@ -254,6 +258,7 @@ async def request_raw( # noqa: PLR0913
follow_redirects=True,
)
_LOGGER.debug(format_httpx_response(response))

if response.status_code in [
HTTPStatus.OK,
HTTPStatus.ACCEPTED,
Expand Down
30 changes: 30 additions & 0 deletions mytoyota/utils/logging/log_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utilities for manipulating returns for log output tasks."""
import json
from typing import Any, Dict, Optional

from httpx import Response
Expand Down Expand Up @@ -54,6 +55,35 @@ def format_httpx_response(response: Response) -> str:
)


def format_httpx_response_json(response: Response) -> str:
"""Format an HTTPX response into a json string representation.
Args:
----
response (Response): The HTTPX response object to format.
Returns:
-------
str: The formatted representation of the HTTPX response.
"""
return json.dumps(
{
"request": {
"method": response.request.method,
"url": str(response.request.url),
"headers": response.request.headers.multi_items(),
"content": response.request.content.decode("utf-8"),
},
"response": {
"status": response.status_code,
"headers": response.headers.multi_items(),
"content": "" if len(response.content) == 0 else response.json(),
},
}
)


def censor_all(dictionary: Dict[str, Any], to_censor: Optional[set] = None) -> Dict[str, Any]:
r"""Censor sensitive values in a dictionary.
Expand Down
48 changes: 32 additions & 16 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ packages = [
classifiers = [
"Development Status :: 4 - Beta",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
Expand All @@ -30,7 +29,7 @@ repository = "https://github.com/DurgNomis-drol/mytoyota"
"Release Notes" = "https://github.com/DurgNomis-drol/mytoyota/releases"

[tool.poetry.dependencies]
python = "^3.8.1"
python = "^3.9"
langcodes = "^3.1"
httpx = ">=0.18.1"
arrow = "^1.1"
Expand All @@ -47,6 +46,7 @@ codespell = "^2.0.0"
pytest = "^7.2.0"
pytest-pretty = "^1.2.0"
pytest-asyncio = "^0.21.1"
pytest-httpx = "^0.28.0"

[tool.ruff]
select = [
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py

This file was deleted.

20 changes: 20 additions & 0 deletions tests/integration_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Need for pytest or else it will cause an import error in pytest."""

from pathlib import Path

import pytest

from mytoyota.controller import CACHE_FILENAME


@pytest.fixture(scope="module")
def data_folder(request) -> str:
"""Return the folder containing test files."""
return str(Path(request.module.__file__).parent / "data")


@pytest.fixture(scope="function")
def remove_cache() -> None:
"""Remove the credentials cache file if it exists."""
# Remove cache file if exists
Path.unlink(CACHE_FILENAME, missing_ok=True)
Loading

0 comments on commit 1a17a62

Please sign in to comment.