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

chore(examples):- show matchers in use on consumer/provider side for MessagePact #364

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: adds matchers to content
MdotMertens committed Oct 17, 2022
commit 36781d38f4746e2ebe95cd468e367be60bb03037
7 changes: 5 additions & 2 deletions pact/message_pact.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

from .broker import Broker
from .constants import MESSAGE_PATH
from .matchers import from_term
from .matchers import Term, from_term, get_generated_values


class MessagePact(Broker):
@@ -137,7 +137,10 @@ def with_content(self, contents):
:rtype: Pact
"""
self._insert_message_if_complete()
self._messages[0]['contents'] = from_term(contents)
if any(isinstance(value, Term) for value in contents.values()):
self._messages[0]['contents'] = get_generated_values(contents)
else:
self._messages[0]['contents'] = from_term(contents)
return self

def expects_to_receive(self, description):
31 changes: 31 additions & 0 deletions tests/test_message_pact.py
Original file line number Diff line number Diff line change
@@ -82,6 +82,37 @@ def test_definition_sparse(self):

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

def test_definition_with_matchers_in_content(self):
target = MessagePact(self.consumer, self.provider)
(
target
.given('there is an alligator named John')
.expects_to_receive('an alligator message')
.with_content({'name': 'John', 'document_name': 'sample_document.doc', 'document_style': Term('prose|docs', 'prose')})
.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'}])

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

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

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)
(