Skip to content

Commit

Permalink
snapshottest -> syrupy - Python 3.12 support
Browse files Browse the repository at this point in the history
Migrate from snapshottest to syrupy. Snapshottest was the issue why the tests were failing on python 3.12
  • Loading branch information
joakimnordling committed May 27, 2024
1 parent 597fcfc commit 5689838
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 170 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
steps:
- name: Checkout 🔁
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 #v4.1.1
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ repos:
rev: v3.1.0
hooks:
- id: prettier
exclude: "^openapi_to_fastapi/tests/__snapshots__/"
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"detail": [
{
"input": null,
"loc": [
"query",
"vendor"
],
"msg": "Field required",
"type": "missing"
},
{
"input": null,
"loc": [
"header",
"auth-header"
],
"msg": "Field required",
"type": "missing"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"detail": [
{
"input": "1,1.2",
"loc": [
"body",
"lat"
],
"msg": "Input should be a valid number, unable to parse string as a number",
"type": "float_parsing"
},
{
"ctx": {
"le": 180.0
},
"input": "99999",
"loc": [
"body",
"lon"
],
"msg": "Input should be less than or equal to 180",
"type": "less_than_equal"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"detail": [
{
"input": {},
"loc": [
"body",
"lat"
],
"msg": "Field required",
"type": "missing"
},
{
"input": {},
"loc": [
"body",
"lon"
],
"msg": "Field required",
"type": "missing"
}
]
}
6 changes: 6 additions & 0 deletions openapi_to_fastapi/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from fastapi import FastAPI
from starlette.testclient import TestClient
from syrupy.extensions.json import JSONSnapshotExtension

from openapi_to_fastapi.routes import SpecRouter

Expand All @@ -28,3 +29,8 @@ def definitions_client(specs_root):
spec_router = SpecRouter(specs_root / "definitions")
app.include_router(spec_router.to_fastapi_router())
return TestClient(app)


@pytest.fixture
def json_snapshot(snapshot):
return snapshot.use_extension(JSONSnapshotExtension)
Empty file.
59 changes: 0 additions & 59 deletions openapi_to_fastapi/tests/snapshots/snap_test_router.py

This file was deleted.

14 changes: 7 additions & 7 deletions openapi_to_fastapi/tests/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_pydantic_model_loading(specs_root):
assert module.ValidationError(loc=[], msg="Crap", type="Error")


def test_weather_route_payload_errors(app, specs_root, client, snapshot):
def test_weather_route_payload_errors(app, specs_root, client, json_snapshot):
spec_router = SpecRouter(specs_root / "definitions")

@spec_router.post("/Weather/Current/Metric")
Expand All @@ -55,14 +55,14 @@ def weather_metric(request):

resp = client.post("/Weather/Current/Metric", json={})
assert resp.status_code == 422
snapshot.assert_match(resp.json(), "Missing payload")
assert json_snapshot == resp.json(), "Missing payload"

resp = client.post("/Weather/Current/Metric", json={"lat": "1,1.2", "lon": "99999"})
assert resp.status_code == 422
snapshot.assert_match(resp.json(), "Incorrect payload type")
assert json_snapshot == resp.json(), "Incorrect payload type"


def test_company_custom_post_route(app, client, specs_root, snapshot):
def test_company_custom_post_route(app, client, specs_root):
spec_router = SpecRouter(specs_root / "definitions")

@spec_router.post("/Company/BasicInfo")
Expand All @@ -76,7 +76,7 @@ def weather_metric(request):
assert resp.json() == company_basic_info_resp


def test_default_post_handler(app, client, specs_root, snapshot):
def test_default_post_handler(app, client, specs_root):
spec_router = SpecRouter(specs_root / "definitions")

@spec_router.post()
Expand All @@ -89,7 +89,7 @@ def company_info(request):
assert resp.json() == company_basic_info_resp


def test_custom_route_definitions(app, client, specs_root, snapshot):
def test_custom_route_definitions(app, client, specs_root, json_snapshot):
spec_router = SpecRouter(specs_root / "definitions")

@spec_router.post("/Weather/Current/Metric")
Expand All @@ -99,7 +99,7 @@ def weather_metric(request, vendor: str, auth_header: str = Header(...)):
app.include_router(spec_router.to_fastapi_router())
resp = client.post("/Weather/Current/Metric", json={"lat": "30.5", "lon": 1.56})
assert resp.status_code == 422
snapshot.assert_match(resp.json(), "Custom route definition")
assert json_snapshot == resp.json(), "Custom route definition"


def test_response_model_is_parsed(app, client, specs_root):
Expand Down
111 changes: 9 additions & 102 deletions poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ pydantic = "^2.7.1"
isort = "^5.13.2"
pytest = "^8.2.1"
pytest-asyncio = "^0.23.7"
snapshottest = "^0.6.0"
flake8 = "^7.0.0"
mypy = "^1.10.0"
invoke = "^2.2.0"
httpx = "^0.27.0"
syrupy = "^4.6.1"

[tool.skjold]
report_only = false
Expand Down

0 comments on commit 5689838

Please sign in to comment.