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
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 8 additions & 6 deletions examples/message/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def test_generate_new_pact_file(pact):
cleanup_json(PACT_FILE)

expected_event = {
'documentName': 'document.doc',
'creator': 'TP',
'documentType': 'microsoft-word'
"documentName": Term("^.*\\.(doc|docx)$",'document.doc'),
"creator": Like("TP"),
"documentType": "microsoft-word",
}

(pact
Expand All @@ -63,7 +63,9 @@ def test_generate_new_pact_file(pact):

with pact:
# handler needs 'documentType' == 'microsoft-word'
MessageHandler(expected_event)
# call matchers.get_generated_values(expected_event) to
# reify/strip the expected_event of the matchers used
MessageHandler(matchers.get_generated_values(expected_event))

progressive_delay(f"{PACT_FILE}")
assert isfile(f"{PACT_FILE}") == 1
Expand All @@ -90,8 +92,8 @@ def test_throw_exception_handler(pact):

with pytest.raises(CustomError):
with pact:
# handler needs 'documentType' == 'microsoft-word'
MessageHandler(wrong_event)
# handler needs 'documentType' == 'microsoft-word
MessageHandler(matchers.get_generated_values(wrong_event))

progressive_delay(f"{PACT_FILE}")
assert isfile(f"{PACT_FILE}") == 0
Expand Down
20 changes: 11 additions & 9 deletions examples/message/tests/consumer/test_message_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from os.path import isfile

from pact import MessageConsumer, Provider
from pact import matchers
from pact.matchers import Like, Term
from src.message_handler import MessageHandler, CustomError

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -93,7 +95,7 @@ def test_throw_exception_handler(pact_no_publish):
with pytest.raises(CustomError):
with pact_no_publish:
# handler needs "documentType" == "microsoft-word"
MessageHandler(wrong_event)
MessageHandler(matchers.get_generated_values(wrong_event))

progressive_delay(f"{PACT_FILE}")
assert isfile(f"{PACT_FILE}") == 0
Expand All @@ -104,9 +106,9 @@ def test_put_file(pact_no_publish):

expected_event = {
"event": "ObjectCreated:Put",
"documentName": "document.doc",
"creator": "TP",
"documentType": "microsoft-word"
"documentName": Term("^.*\\.(doc|docx)$", 'document.doc'),
"creator": Like("TP"),
"documentType": "microsoft-word",
}

(pact_no_publish
Expand All @@ -118,7 +120,7 @@ def test_put_file(pact_no_publish):
}))

with pact_no_publish:
MessageHandler(expected_event)
MessageHandler(matchers.get_generated_values(expected_event))

progressive_delay(f"{PACT_FILE}")
assert isfile(f"{PACT_FILE}") == 1
Expand All @@ -136,9 +138,9 @@ def test_publish_to_broker(pact):

expected_event = {
"event": "ObjectCreated:Delete",
"documentName": "document.doc",
"creator": "TP",
"documentType": "microsoft-word"
"documentName": Term("^.*\\.(doc|docx)$", 'document.doc'),
"creator": Like("TP"),
"documentType": "microsoft-word",
}

(pact
Expand All @@ -150,7 +152,7 @@ def test_publish_to_broker(pact):
}))

with pact:
MessageHandler(expected_event)
MessageHandler(matchers.get_generated_values(expected_event))

progressive_delay(f"{PACT_FILE}")
assert isfile(f"{PACT_FILE}") == 1
2 changes: 1 addition & 1 deletion examples/message/tests/provider/test_message_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def document_created_handler():
return {
"event": "ObjectCreated:Put",
"documentName": "document.doc",
"creator": "TP",
"creator": "PF",
"documentType": "microsoft-word"
}

Expand Down
36 changes: 36 additions & 0 deletions tests/test_message_pact.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,42 @@ def test_definition_multiple_provider_states(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':
{'data': {'generate': 'prose',
'matcher': {'json_class': 'Regexp',
'o': 0,
's': 'prose|docs'}},
'json_class': 'Pact::Term'}})

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
Loading