Skip to content

Commit 0c5f3ab

Browse files
committed
chore: update messaging examples
- dont strip matchers from pact content - show examples of matchers in consumer message test - show examples matchers.get_generated_values() to reify message of pact matchers for passing to handler under test
1 parent 3221e2d commit 0c5f3ab

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

examples/message/README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ def test_generate_new_pact_file(pact):
4848
cleanup_json(PACT_FILE)
4949

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

5656
(pact
@@ -63,7 +63,9 @@ def test_generate_new_pact_file(pact):
6363

6464
with pact:
6565
# handler needs 'documentType' == 'microsoft-word'
66-
MessageHandler(expected_event)
66+
# call matchers.get_generated_values(expected_event) to
67+
# reify/strip the expected_event of the matchers used
68+
MessageHandler(matchers.get_generated_values(expected_event))
6769

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

9193
with pytest.raises(CustomError):
9294
with pact:
93-
# handler needs 'documentType' == 'microsoft-word'
94-
MessageHandler(wrong_event)
95+
# handler needs 'documentType' == 'microsoft-word
96+
MessageHandler(matchers.get_generated_values(wrong_event))
9597

9698
progressive_delay(f"{PACT_FILE}")
9799
assert isfile(f"{PACT_FILE}") == 0

examples/message/tests/consumer/test_message_consumer.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from os.path import isfile
99

1010
from pact import MessageConsumer, Provider
11+
from pact import matchers
12+
from pact.matchers import Like, Term
1113
from src.message_handler import MessageHandler, CustomError
1214

1315
log = logging.getLogger(__name__)
@@ -93,7 +95,7 @@ def test_throw_exception_handler(pact_no_publish):
9395
with pytest.raises(CustomError):
9496
with pact_no_publish:
9597
# handler needs "documentType" == "microsoft-word"
96-
MessageHandler(wrong_event)
98+
MessageHandler(matchers.get_generated_values(wrong_event))
9799

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

105107
expected_event = {
106108
"event": "ObjectCreated:Put",
107-
"documentName": "document.doc",
108-
"creator": "TP",
109-
"documentType": "microsoft-word"
109+
"documentName": Term("^.*\\.(doc|docx)$",'document.doc'),
110+
"creator": Like("TP"),
111+
"documentType": "microsoft-word",
110112
}
111113

112114
(pact_no_publish
@@ -118,7 +120,7 @@ def test_put_file(pact_no_publish):
118120
}))
119121

120122
with pact_no_publish:
121-
MessageHandler(expected_event)
123+
MessageHandler(matchers.get_generated_values(expected_event))
122124

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

137139
expected_event = {
138140
"event": "ObjectCreated:Delete",
139-
"documentName": "document.doc",
140-
"creator": "TP",
141-
"documentType": "microsoft-word"
141+
"documentName": Term("^.*\\.(doc|docx)$",'document.doc'),
142+
"creator": Like("TP"),
143+
"documentType": "microsoft-word",
142144
}
143145

144146
(pact
@@ -150,7 +152,10 @@ def test_publish_to_broker(pact):
150152
}))
151153

152154
with pact:
153-
MessageHandler(expected_event)
155+
# call matchers.get_generated_values(expected_event) to
156+
# reify/strip the expected_event of the matchers used
157+
# for Pact
158+
MessageHandler(matchers.get_generated_values(expected_event))
154159

155160
progressive_delay(f"{PACT_FILE}")
156161
assert isfile(f"{PACT_FILE}") == 1

examples/message/tests/provider/test_message_provider.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def document_created_handler():
2222
return {
2323
"event": "ObjectCreated:Put",
2424
"documentName": "document.doc",
25-
"creator": "TP",
25+
"creator": "PF",
2626
"documentType": "microsoft-word"
2727
}
2828

pact/message_pact.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,7 @@ def with_content(self, contents):
146146
:rtype: Pact
147147
"""
148148
self._insert_message_if_complete()
149-
if any(isinstance(value, Term) for value in contents.values()):
150-
self._messages[0]['contents'] = get_generated_values(contents)
151-
else:
152-
self._messages[0]['contents'] = from_term(contents)
149+
self._messages[0]['contents'] = from_term(contents)
153150
return self
154151

155152
def expects_to_receive(self, description):

tests/test_message_pact.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,11 @@ def test_definition_with_matchers_in_content(self):
158158

159159
self.assertEqual(
160160
target._messages[0]['contents'],
161-
{'name': 'John', 'document_name': 'sample_document.doc', 'document_style': 'prose'})
161+
{'name': 'John', 'document_name': 'sample_document.doc', 'document_style': {'data': {'generate': 'prose',
162+
'matcher': {'json_class': 'Regexp',
163+
'o': 0,
164+
's': 'prose|docs'}},
165+
'json_class': 'Pact::Term'}})
162166

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

0 commit comments

Comments
 (0)