Flask¶
Flask provider example.
This modules defines a simple provider which will be tested with Pact in the provider test. As Pact is a consumer-driven framework, the consumer defines the contract which the provider must then satisfy.
The provider is the application which receives requests from another service (the consumer) and returns a response. In this example, we have a simple endpoint which returns a user's information from a (fake) database.
This also showcases how Pact tests differ from merely testing adherence to an OpenAPI specification. The Pact tests are more concerned with the practical use of the API, rather than the formally defined specification. The User class defined here has additional fields which are not used by the consumer. Should the provider later decide to add or remove fields, Pact's consumer-driven testing will provide feedback on whether the consumer is compatible with the provider's changes.
Note that the code in this module is agnostic of Pact (i.e., this would be your
production code). The pact-python
dependency only appears in the tests. This
is because the consumer is not concerned with Pact, only the tests are.
Attributes¶
FAKE_DB: dict[int, User] = {}
module-attribute
¶
app = Flask(__name__)
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
User(id: int, name: str, created_on: datetime, email: str | None, ip_address: str | None, hobbies: list[str], admin: bool)
dataclass
¶
User data class.
Attributes¶
admin: bool
instance-attribute
¶
created_on: datetime
instance-attribute
¶
email: str | None
instance-attribute
¶
hobbies: list[str]
instance-attribute
¶
id: int
instance-attribute
¶
ip_address: str | None
instance-attribute
¶
name: str
instance-attribute
¶
Functions¶
dict() -> dict[str, Any]
¶
Return the user's data as a dict.
Source code in examples/src/flask.py
Functions¶
create_user() -> Response
¶
Source code in examples/src/flask.py
delete_user(uid: int) -> tuple[str | Response, int]
¶
get_user_by_id(uid: int) -> Response | tuple[Response, int]
¶
Fetch a user by their ID.
PARAMETER | DESCRIPTION |
---|---|
uid
|
The ID of the user to fetch
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Response | tuple[Response, int]
|
The user data if found, HTTP 404 if not |