Skip to content

Commit 5ed73db

Browse files
test: consider publish to broker with no pact_dir argument
1 parent e097153 commit 5ed73db

File tree

3 files changed

+63
-22
lines changed

3 files changed

+63
-22
lines changed

examples/message/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ Otherwise, no pact file is generated.
101101

102102
## Provider
103103

104+
Note: The current example only tests the consumer side.
105+
In the future, provider tests will also be included.
106+
104107
```
105108
+-------------------+ +-----------+
106109
|(Message Provider) | message | (Pact) |
@@ -111,6 +114,9 @@ Otherwise, no pact file is generated.
111114

112115
## E2E Messaging
113116

117+
Note: The current example only tests the consumer side.
118+
In the future, provider tests will also be included.
119+
114120
```
115121
+-------------------+ +-----------+ +-------------------+
116122
|(Message Provider) | message | (Pact) | message |(Message Consumer) |
@@ -119,8 +125,6 @@ Otherwise, no pact file is generated.
119125
+-------------------+ +-----------+ +-------------------+
120126
```
121127

122-
Note: The current example only tests the consumer side. In the future, provider tests will also be included.
123-
124128
# Setup
125129

126130
## Virtual Environment

examples/message/run_pytest.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -o pipefail
3+
4+
pytest
5+
6+
# publish to broker assuming broker is active
7+
# pytest tests/consumer/test_message_consumer.py::test_publish_to_broker --publish-pact 2

examples/message/tests/consumer/test_message_consumer.py

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""pact test for user service client"""
1+
"""pact test for a message consumer"""
22

33
import logging
44
import pytest
@@ -14,35 +14,36 @@
1414
logging.basicConfig(level=logging.INFO)
1515

1616
PACT_BROKER_URL = "http://localhost"
17-
PACT_FILE = "userserviceclient-userservice.json"
1817
PACT_BROKER_USERNAME = "pactbroker"
1918
PACT_BROKER_PASSWORD = "pactbroker"
2019
PACT_DIR = 'pacts'
2120

22-
consumer_name = 'DetectContentLambda'
23-
provider_name = 'ContentProvider'
24-
expected_json = (f"{consumer_name.lower().replace(' ', '_')}_message-"
25-
+ f"{provider_name.lower().replace(' ', '_')}_message.json")
21+
CONSUMER_NAME = 'DetectContentLambda'
22+
PROVIDER_NAME = 'ContentProvider'
23+
PACT_FILE = (f"{CONSUMER_NAME.lower().replace(' ', '_')}_message-"
24+
+ f"{PROVIDER_NAME.lower().replace(' ', '_')}_message.json")
2625

2726
@pytest.fixture(scope='session')
2827
def pact(request):
2928
version = request.config.getoption('--publish-pact')
3029
publish = True if version else False
3130

32-
pact = MessageConsumer(consumer_name, version=version).has_pact_with(
33-
Provider(provider_name),
34-
pact_dir=PACT_DIR, publish_to_broker=publish, broker_base_url=PACT_BROKER_URL,
31+
pact = MessageConsumer(CONSUMER_NAME, version=version).has_pact_with(
32+
Provider(PROVIDER_NAME),
33+
publish_to_broker=publish, broker_base_url=PACT_BROKER_URL,
3534
broker_username=PACT_BROKER_USERNAME, broker_password=PACT_BROKER_PASSWORD)
3635

36+
# current pact does not consider the PACT_DIR argument, assumes none
3737
yield pact
3838

3939

40-
def cleanup_json(expected_json):
40+
def cleanup_json(file):
4141
"""
4242
Remove existing json file before test if any
4343
"""
44-
if (isfile(f"pacts/{expected_json}")):
45-
remove(f"pacts/{expected_json}")
44+
if (isfile(f"{file}")):
45+
remove(f"{file}")
46+
4647

4748
def progressive_delay(file, time_to_wait=10, second_interval=0.5, verbose=False):
4849
"""
@@ -62,8 +63,7 @@ def progressive_delay(file, time_to_wait=10, second_interval=0.5, verbose=False)
6263

6364

6465
def test_throw_exception_handler(pact):
65-
cleanup_json(expected_json)
66-
66+
cleanup_json(PACT_FILE)
6767
wrong_event = {
6868
'documentName': 'spreadsheet.xls',
6969
'creator': 'WI',
@@ -83,12 +83,12 @@ def test_throw_exception_handler(pact):
8383
# handler needs 'documentType' == 'microsoft-word'
8484
MessageHandler(wrong_event)
8585

86-
progressive_delay(f"pacts/{expected_json}")
87-
assert isfile(f"pacts/{expected_json}") == 0
86+
progressive_delay(f"{PACT_FILE}")
87+
assert isfile(f"{PACT_FILE}") == 0
8888

8989

90-
def test_generate_pact_file(pact):
91-
cleanup_json(expected_json)
90+
def test_generate_new_pact_file(pact):
91+
cleanup_json(PACT_FILE)
9292

9393
expected_event = {
9494
'documentName': 'document.doc',
@@ -108,5 +108,35 @@ def test_generate_pact_file(pact):
108108
# handler needs 'documentType' == 'microsoft-word'
109109
MessageHandler(expected_event)
110110

111-
progressive_delay(f"pacts/{expected_json}")
112-
assert isfile(f"pacts/{expected_json}") == 1
111+
progressive_delay(f"{PACT_FILE}")
112+
assert isfile(f"{PACT_FILE}") == 1
113+
114+
115+
def test_publish_to_broker(pact):
116+
"""
117+
This test does not clean-up previously generated pact.
118+
Sample execution where 2 is an arbitrary version:
119+
120+
`pytest tests/consumer/test_message_consumer.py::test_publish_pact_to_broker`
121+
122+
`pytest tests/consumer/test_message_consumer.py::test_publish_pact_to_broker --publish-pact 2`
123+
"""
124+
expected_event = {
125+
'documentName': 'document.doc',
126+
'creator': 'TP',
127+
'documentType': 'microsoft-word'
128+
}
129+
130+
(pact
131+
.given('A document create in Document Service with broker')
132+
.expects_to_receive('Description with broker')
133+
.with_content(expected_event)
134+
.with_metadata({
135+
'Content-Type': 'application/json'
136+
}))
137+
138+
with pact:
139+
MessageHandler(expected_event)
140+
141+
progressive_delay(f"{PACT_FILE}")
142+
assert isfile(f"{PACT_FILE}") == 1

0 commit comments

Comments
 (0)