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 multiple provider states and additional state parameters for Message Pact #323

Closed
nsfrias opened this issue Nov 26, 2022 · 0 comments

Comments

@nsfrias
Copy link
Contributor

nsfrias commented Nov 26, 2022

Currently for Message Pacts it is only possible to define a single provider state as a string.
Check the method that sets the state:

def given(self, provider_states):
"""
Define the provider state for this pact.
When the provider verifies this contract, they will use this field to
setup pre-defined data that will satisfy the response expectations.
:param provider_state: The short sentence that is unique to describe
the provider state for this contract.
:type provider_state: basestring
:rtype: Pact
"""
self._insert_message_if_complete()
state = [{"name": "{}".format(provider_states)}]
self._messages[0]['providerStates'] = state
return self

But often enough there is the need to define multiple provider states and also to be able to pass additional arguments to set the state (besides the provider state name).

To this end I put together this pull request: #322

This change allows multiple providers states to be defined and also additional parameters to be passed for each state, for example:

(
            target
            .given('there is an alligator named John',
                params={'color':'green', 'weight_kg': 130, 'lenght_m': 1.95}
            )
            .given('there is an spider named Jack',
                params={'color':'mostly black', 'weight_kg': 0.009, 'lenght_m': 0.05}
            )
            .expects_to_receive('an alligator message')
            .with_content({'name': 'John', 'document_name': 'sample_document.doc'})
            .with_metadata({'contentType': 'application/json',
                            'source': 'legacy_api',
                            'some-header': Term('\\d+-\\d+-\\d+T\\d+:\\d+:\\d+', '2022-02-15T20:16:01')})
        )

It is also backwards compatible, in that it is possible to just pass a single state name.

@nsfrias nsfrias closed this as completed Feb 20, 2023
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

No branches or pull requests

1 participant