-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow functional arguments for provider callbacks and message producers #890
Conversation
@valkolovos Here's the PR I have been talking about. Keen to have you give it a go and tell me what you think 🚀 |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #890 +/- ##
====================================
+ Coverage 59% 78% +18%
====================================
Files 30 31 +1
Lines 3373 3665 +292
====================================
+ Hits 2023 2859 +836
+ Misses 1350 806 -544
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Pact makes use of additional HTTP endpoints to be able to communicate. In particular: - A callback endpoint is required in order to setup/teardown the provider states; and, - HTTP is used as the transport mechanism for message interactions as the specific protocol is abstracted away. This commit adds the foundational HTTP servers for both use cases. Signed-off-by: JP-Ellis <[email protected]>
With the message relay server added, this commit modifies the verifier to allow for a handler function to be used to handle messages. Note that this does _not_ modify the tests yet (this will be done in another commit). BREAKING CHANGE: The provider name must be given as an argument of the `Verifier` constructor, instead of the first argument of the `set_info` method. BREAKING CHANGE: The `set_info` verifier method is removed, with `add_transport` needing to be used. Signed-off-by: JP-Ellis <[email protected]>
When testing a provider, it is very common that the provider state needs to be set up. This commit allows for functions to be provided instead of needing to configure a HTTP handler for the callback. BREAKING CHANGE: `set_state` has been renamed to `state_handler`. If using a URL still, the `body` keyword argument is now a _required_ parameter. Signed-off-by: JP-Ellis <[email protected]>
The initial implentation required the end user to provide a function; however, this has been expanded to also allow for mapping of message names to either functions, or static message definitions. Furthermore, the function signature has been tweaked. It now expects a dictionary, with the `Message` typed dictionary being provided for convenience. As part of this commit, the MessageRelay has been renamed to MessageProducer to more correctly reflect its purpose. BREAKING CHANGE: `message_handler` signature has been changed and expanded. Signed-off-by: JP-Ellis <[email protected]>
Signed-off-by: JP-Ellis <[email protected]>
With functions being able to be provided directly to the Verifier, this greatly simplifies the examples as we no longer need custom HTTP servers to wrap a function. Signed-off-by: JP-Ellis <[email protected]>
Signed-off-by: JP-Ellis <[email protected]>
Signed-off-by: JP-Ellis <[email protected]>
Signed-off-by: JP-Ellis <[email protected]>
Signed-off-by: JP-Ellis <[email protected]>
ebadf95
to
3c4359b
Compare
The `typing.Self` annotation was only introduced in Python 3.11, and therefore we have to rely on the typing extensions for versions 3.9 and 3.10. There are also issues with TypeAliases and the use of the `|` operator. Signed-off-by: JP-Ellis <[email protected]>
f9f3e8f
to
5a0d3d8
Compare
Unfortunately, CI is flaky and this is difficult to replicate locally. Signed-off-by: JP-Ellis <[email protected]>
The `SIGINT` signal is _not_ supported on Windows, so it is replaced with the cross-platform `process.terminate` method. Signed-off-by: JP-Ellis <[email protected]>
📝 Summary
Allows to functions to be passed in to handle provider states and to produce messages.
Still outstanding at the time of writing this PR is fully updated documentation.
As part of these changes,
🚨 Breaking Changes
This is all within the
pact.v3
module which is not yet stable. The breaking changes are as follows:Verifier.set_info
has been removed. Its functionality has been split across two functions:Verifier.__init__
which takes two arguments now: the provider name and the host against which to run the provider tests.Verifier.add_transport
which behaves as before but now needs to be called at least once before executing.verify()
Verifier.set_state
has been replaced byVerifier.state_handler
. This more correctly reflects the intent of the method.🔥 Motivation
Previously, if one wanted to have a function be called to set the provider in a specific state, the end-user implementing the test suite would need to write a minimal HTTP server to effectively wrap this function.
This capability is still present, but it is now possible to pass in a function directly. Pact Python internally handles the HTTP server wrapper so the end user need not worry about that.
Similarly, the message producer required a HTTP wrapper to trigger the message generation. This has been made automatic with the end-user only requiring to define a function to handle that.
🔨 Test Plan
All existing tests have been adapted to make use of this feature.
🔗 Related issues/PRs