-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(example): migrate fastapi provider example
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
Showing
12 changed files
with
172 additions
and
241 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.