Test 02 Message Consumer¶
Consumer test of example message handler using the v3 API.
This test will create a pact between the message handler and the message provider.
Attributes¶
log = logging.getLogger(__name__)
module-attribute
¶
Classes¶
Functions¶
handler() -> Handler
¶
Fixture for the Handler.
This fixture mocks the filesystem calls in the handler, so that we can verify that the handler is calling the filesystem correctly.
Source code in examples/tests/v3/test_02_message_consumer.py
pact() -> Generator[Pact, None, None]
¶
Set up Message Pact Consumer.
This fixtures sets up the Message Pact consumer and the pact it has with a provider. The consumer defines the expected messages it will receive from the provider, and the Python test suite verifies that the correct actions are taken.
The verify method takes a function as an argument. This function
will be called with one or two arguments - the value of with_body
and
the contents of with_metadata
if provided.
If the function under test does not take those parameters, you can create a wrapper function to convert the pact parameters into the values expected by your function.
For each interaction, the consumer defines the following:
(
pact = Pact("consumer name", "provider name")
processed_messages: list[MessagePact.MessagePactResult] = pact .with_specification("V3")
.upon_receiving("a request", "Async") .given("a request to write test.txt") .with_body(msg) .with_metadata({"Content-Type": "application/json"})
.verify(pact_handler)
)
Source code in examples/tests/v3/test_02_message_consumer.py
test_async_message_handler_read(pact: Pact, handler: Handler, verifier: Callable[[str | bytes | None, dict[str, Any]], None]) -> None
¶
Create a pact between the message handler and the message provider.
Source code in examples/tests/v3/test_02_message_consumer.py
test_async_message_handler_write(pact: Pact, handler: Handler, verifier: Callable[[str | bytes | None, dict[str, Any]], None]) -> None
¶
Create a pact between the message handler and the message provider.
Source code in examples/tests/v3/test_02_message_consumer.py
verifier(handler: Handler) -> Generator[Callable[[str | bytes | None, dict[str, Any]], None], Any, None]
¶
Verifier function for the Pact.
This function is passed to the verify
method of the Pact object. It is
responsible for taking in the messages (along with the context/metadata)
and ensuring that the consumer is able to process the message correctly.
In our case, we deserialize the message and pass it to the (pre-mocked) handler for processing. We then verify that the underlying filesystem calls were made as expected.