Test 01 Provider Fastapi¶
Test the FastAPI provider with Pact.
This module tests the FastAPI provider defined in src/fastapi.py
against the
mock consumer. The mock consumer is set up by Pact and will replay the requests
defined by the consumers. Pact will then validate that the provider responds
with the expected responses.
The provider will be expected to be in a given state in order to respond to
certain requests. For example, when fetching a user's information, the provider
will need to have a user with the given ID in the database. In order to avoid
side effects, the provider's database calls are mocked out using functionalities
from unittest.mock
.
In order to set the provider into the correct state, this test module defines an
additional endpoint on the provider, in this case /_pact/provider_states
.
Calls to this endpoint mock the relevant database calls to set the provider into
the correct state.
A good resource for understanding the provider tests is the Pact Provider Test section of the Pact documentation.
Attributes¶
PROVIDER_URL = URL('http://localhost:8080')
module-attribute
¶
Classes¶
ProviderState
¶
Functions¶
mock_delete_request_to_delete_user() -> None
¶
Mock the database for the delete request to delete a user.
Source code in examples/tests/test_01_provider_fastapi.py
mock_pact_provider_states(state: ProviderState) -> dict[str, Optional[str]]
async
¶
Define the provider state.
For Pact to be able to correctly tests compliance with the contract, the internal state of the provider needs to be set up correctly. Naively, this would be achieved by setting up the database with the correct data for the test, but this can be slow and error-prone. Instead this is best achieved by mocking the relevant calls to the database so as to avoid any side effects.
For Pact to be able to correctly get the provider into the correct state, this function is used to define an additional endpoint on the provider. This endpoint is called by Pact before each test to ensure that the provider is in the correct state.
Source code in examples/tests/test_01_provider_fastapi.py
mock_post_request_to_create_user() -> None
¶
Mock the database for the post request to create a user.
Source code in examples/tests/test_01_provider_fastapi.py
mock_user_123_doesnt_exist() -> None
¶
Mock the database for the user 123 doesn't exist state.
Source code in examples/tests/test_01_provider_fastapi.py
mock_user_123_exists() -> None
¶
Mock the database for the user 123 exists state.
You may notice that the return value here differs from the consumer's expected response. This is because the consumer's expected response is guided by what the consumer uses.
By using consumer-driven contracts and testing the provider against the consumer's contract, we can ensure that the provider is what the consumer needs. This allows the provider to safely evolve their API (by both adding and removing fields) without fear of breaking the interactions with the consumers.
Source code in examples/tests/test_01_provider_fastapi.py
run_server() -> None
¶
Run the FastAPI server.
This function is required to run the FastAPI server in a separate process. A
lambda cannot be used as the target of a multiprocessing.Process
as it
cannot be pickled.
Source code in examples/tests/test_01_provider_fastapi.py
test_against_broker(broker: URL, verifier: Verifier) -> None
¶
Test the provider against the broker.
The broker will be used to retrieve the contract, and the provider will be tested against the contract.
As Pact is a consumer-driven, the provider is tested against the contract defined by the consumer. The consumer defines the expected request to and response from the provider.
For an example of the consumer's contract, see the consumer's tests.
Source code in examples/tests/test_01_provider_fastapi.py
verifier() -> Generator[Verifier, Any, None]
¶
Set up the Pact verifier.