Skip to content
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

Merged
merged 13 commits into from
Dec 30, 2024

Conversation

JP-Ellis
Copy link
Contributor

@JP-Ellis JP-Ellis commented Dec 5, 2024

📝 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 by Verifier.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

@JP-Ellis JP-Ellis self-assigned this Dec 5, 2024
@JP-Ellis
Copy link
Contributor Author

JP-Ellis commented Dec 5, 2024

@valkolovos Here's the PR I have been talking about. Keen to have you give it a go and tell me what you think 🚀

Copy link

codecov bot commented Dec 5, 2024

Codecov Report

Attention: Patch coverage is 75.15723% with 79 lines in your changes missing coverage. Please review.

Project coverage is 78%. Comparing base (3f35067) to head (038fcc3).
Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
src/pact/v3/verifier.py 58% 64 Missing ⚠️
src/pact/v3/_server.py 89% 15 Missing ⚠️
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     
Flag Coverage Δ
examples 57% <64%> (-3%) ⬇️
tests 77% <75%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

tests/v3/test_match.py Dismissed Show dismissed Hide dismissed
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]>
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]>
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]>
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]>
@JP-Ellis JP-Ellis merged commit 6c56073 into main Dec 30, 2024
36 of 37 checks passed
@JP-Ellis JP-Ellis deleted the feat/message-relay branch December 30, 2024 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Relay messages in Pact Python
2 participants