Skip to content

Commit

Permalink
chore(example): migrate fastapi provider example
Browse files Browse the repository at this point in the history
This migrates the Pact FastAPI provider from the old standalone
examples, and merges it with the new combined examples.

The consumer tests are executed first which publishes the contracts with
the broker. The provider test is then executed against the broker to
verify compliance with the published contracts.

This does, at this stage, create an unusual interdependence between
tests which typically should be avoided. I plan to fix this at a later
stage.

Signed-off-by: JP-Ellis <josh@jpellis.me>
  • Loading branch information
JP-Ellis committed Sep 18, 2023
1 parent b3103c1 commit 6759e83
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 241 deletions.
4 changes: 0 additions & 4 deletions examples/fastapi_provider/.flake8

This file was deleted.

9 changes: 0 additions & 9 deletions examples/fastapi_provider/requirements.txt

This file was deleted.

6 changes: 0 additions & 6 deletions examples/fastapi_provider/run_pytest.sh

This file was deleted.

25 changes: 0 additions & 25 deletions examples/fastapi_provider/src/provider.py

This file was deleted.

Empty file.
27 changes: 0 additions & 27 deletions examples/fastapi_provider/tests/conftest.py

This file was deleted.

46 changes: 0 additions & 46 deletions examples/fastapi_provider/tests/pact_provider.py

This file was deleted.

Empty file.
87 changes: 0 additions & 87 deletions examples/fastapi_provider/tests/provider/test_provider.py

This file was deleted.

37 changes: 0 additions & 37 deletions examples/fastapi_provider/verify_pact.sh

This file was deleted.

46 changes: 46 additions & 0 deletions examples/src/fastapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
FastAPI provider example.
This modules defines a simple
[provider](https://docs.pact.io/getting_started/terminology#service-provider)
with Pact. As Pact is a consumer-driven framework, the consumer defines the
contract which the provider must then satisfy.
This test assumes that the consumer has already defined a contract for the
provider to satisfy.
"""

from __future__ import annotations

from typing import Any

from fastapi import FastAPI
from fastapi.responses import JSONResponse

app = FastAPI()

"""
As this is a simple example, we'll use a simple dict to represent a database.
This would be replaced with a real database in a real application.
When testing the provider in a real application, the calls to the database
would be mocked out (e.g. with `unittest.mock.patch`).
"""
FAKE_DB: dict[int, dict[str, Any]] = {}


@app.get("/users/{uid}")
async def get_user_by_id(uid: int) -> dict[str, Any]:
"""
Fetch a user by their ID.
Args:
uid: The ID of the user to fetch
Returns:
The user data if found, HTTP 404 if not
"""
user = FAKE_DB.get(uid)
if not user:
return JSONResponse(status_code=404, content={"error": "User not found"})
return JSONResponse(status_code=200, content=user)
Loading

0 comments on commit 6759e83

Please sign in to comment.