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

feat: Enhance provider states for pact-message #322

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 15 additions & 6 deletions pact/message_pact.py
Expand Up @@ -98,22 +98,31 @@ def __init__(
self._messages = []
self._message_process = None

def given(self, provider_states):
def given(self, name, params=None):
"""
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
:param name: The short sentence that is unique to describe the provider
state for this contract.
:type name: basestring
:param params: Additional arguments necessary to set the provider state
:type params: dict
:rtype: Pact
"""
self._insert_message_if_complete()

state = [{"name": "{}".format(provider_states)}]
self._messages[0]['providerStates'] = state
provider_state = {'name': "{}".format(name)}
if params:
provider_state['params'] = params

if 'providerStates' not in self._messages[0]:
self._messages[0]['providerStates'] = [provider_state]
else:
self._messages[0]['providerStates'].append(provider_state)

return self

def with_metadata(self, metadata):
Expand Down
52 changes: 52 additions & 0 deletions tests/test_message_pact.py
Expand Up @@ -82,6 +82,58 @@ def test_definition_sparse(self):

self.assertTrue(target._messages[0]['metaData']['some-header'], 'Pact::Term')

def test_definition_multiple_provider_states(self):
target = MessagePact(self.consumer, self.provider)
(
target
.given('there is an alligator named John',
params={'color': 'green', 'weight_kg': 130, 'length_m': 1.95})
.given('there is an spider named Jack',
params={'color': 'mostly black', 'weight_kg': 0.009, 'length_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')})
)

self.assertEqual(len(target._messages), 1)

self.assertEqual(
target._messages[0]['providerStates'],
[
{
'name': 'there is an alligator named John',
'params': {
'color': 'green',
'weight_kg': 130,
'length_m': 1.95
}
},
{
'name': 'there is an spider named Jack',
'params': {
'color': 'mostly black',
'weight_kg': 0.009,
'length_m': 0.05
}
}
]
)

self.assertEqual(
target._messages[0]['description'],
'an alligator message')

self.assertEqual(
target._messages[0]['contents'],
{'name': 'John', 'document_name': 'sample_document.doc'})

self.assertTrue({'contentType': 'application/json', 'source': 'legacy_api'}.items()
<= target._messages[0]['metaData'].items())

self.assertTrue(target._messages[0]['metaData']['some-header'], 'Pact::Term')

def test_insert_new_message_once_required_attributes_provided(self):
target = MessagePact(self.consumer, self.provider)
(
Expand Down