Skip to content

Commit 1bbdd37

Browse files
nsfriasNuno Frias
andauthored
feat: Enhance provider states for pact-message (#322)
* feat: Enhance provider states for pact-message * feat: allow multiple provider states to be defined in a pact * feat: allow additional parameters to be passed in the provider state Signed-off-by: Nuno Frias <[email protected]> * feat: Enhance provider states for pact-message * feat: allow multiple provider states to be defined in a pact * feat: allow additional parameters to be passed in the provider state Signed-off-by: Nuno Frias <[email protected]> Signed-off-by: Nuno Frias <[email protected]> Co-authored-by: Nuno Frias <[email protected]>
1 parent 53ca129 commit 1bbdd37

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

pact/message_pact.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,31 @@ def __init__(
9898
self._messages = []
9999
self._message_process = None
100100

101-
def given(self, provider_states):
101+
def given(self, name, params=None):
102102
"""
103103
Define the provider state for this pact.
104104
105105
When the provider verifies this contract, they will use this field to
106106
setup pre-defined data that will satisfy the response expectations.
107107
108-
:param provider_state: The short sentence that is unique to describe
109-
the provider state for this contract.
110-
:type provider_state: basestring
108+
:param name: The short sentence that is unique to describe the provider
109+
state for this contract.
110+
:type name: basestring
111+
:param params: Additional arguments necessary to set the provider state
112+
:type params: dict
111113
:rtype: Pact
112114
"""
113115
self._insert_message_if_complete()
114116

115-
state = [{"name": "{}".format(provider_states)}]
116-
self._messages[0]['providerStates'] = state
117+
provider_state = {'name': "{}".format(name)}
118+
if params:
119+
provider_state['params'] = params
120+
121+
if 'providerStates' not in self._messages[0]:
122+
self._messages[0]['providerStates'] = [provider_state]
123+
else:
124+
self._messages[0]['providerStates'].append(provider_state)
125+
117126
return self
118127

119128
def with_metadata(self, metadata):

tests/test_message_pact.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,58 @@ def test_definition_sparse(self):
8282

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

85+
def test_definition_multiple_provider_states(self):
86+
target = MessagePact(self.consumer, self.provider)
87+
(
88+
target
89+
.given('there is an alligator named John',
90+
params={'color': 'green', 'weight_kg': 130, 'length_m': 1.95})
91+
.given('there is an spider named Jack',
92+
params={'color': 'mostly black', 'weight_kg': 0.009, 'length_m': 0.05})
93+
.expects_to_receive('an alligator message')
94+
.with_content({'name': 'John', 'document_name': 'sample_document.doc'})
95+
.with_metadata({'contentType': 'application/json',
96+
'source': 'legacy_api',
97+
'some-header': Term('\\d+-\\d+-\\d+T\\d+:\\d+:\\d+', '2022-02-15T20:16:01')})
98+
)
99+
100+
self.assertEqual(len(target._messages), 1)
101+
102+
self.assertEqual(
103+
target._messages[0]['providerStates'],
104+
[
105+
{
106+
'name': 'there is an alligator named John',
107+
'params': {
108+
'color': 'green',
109+
'weight_kg': 130,
110+
'length_m': 1.95
111+
}
112+
},
113+
{
114+
'name': 'there is an spider named Jack',
115+
'params': {
116+
'color': 'mostly black',
117+
'weight_kg': 0.009,
118+
'length_m': 0.05
119+
}
120+
}
121+
]
122+
)
123+
124+
self.assertEqual(
125+
target._messages[0]['description'],
126+
'an alligator message')
127+
128+
self.assertEqual(
129+
target._messages[0]['contents'],
130+
{'name': 'John', 'document_name': 'sample_document.doc'})
131+
132+
self.assertTrue({'contentType': 'application/json', 'source': 'legacy_api'}.items()
133+
<= target._messages[0]['metaData'].items())
134+
135+
self.assertTrue(target._messages[0]['metaData']['some-header'], 'Pact::Term')
136+
85137
def test_insert_new_message_once_required_attributes_provided(self):
86138
target = MessagePact(self.consumer, self.provider)
87139
(

0 commit comments

Comments
 (0)