Skip to content

Commit 5448d8c

Browse files
test: remove mock and check generated json file
1 parent abd3574 commit 5448d8c

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

examples/message/tests/consumer/test_message_consumer.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""pact test for user service client"""
22

33
import logging
4-
from unittest.mock import patch
5-
64
import pytest
7-
from pact import MessageConsumer, Provider
5+
import time
86

7+
from os import remove
8+
from os.path import isfile
9+
10+
from pact import MessageConsumer, Provider
911
from src.message_handler import MessageHandler, CustomError
1012

1113
log = logging.getLogger(__name__)
@@ -17,21 +19,35 @@
1719
PACT_BROKER_PASSWORD = "pactbroker"
1820
PACT_DIR = 'pacts'
1921

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")
2026

2127
@pytest.fixture(scope='session')
2228
def pact(request):
2329
version = request.config.getoption('--publish-pact')
2430
publish = True if version else False
2531

26-
pact = MessageConsumer('DetectContentLambda', version=version).has_pact_with(
27-
Provider('ContentProvider'),
32+
pact = MessageConsumer(consumer_name, version=version).has_pact_with(
33+
Provider(provider_name),
2834
pact_dir=PACT_DIR, publish_to_broker=publish, broker_base_url=PACT_BROKER_URL,
2935
broker_username=PACT_BROKER_USERNAME, broker_password=PACT_BROKER_PASSWORD)
3036

3137
yield pact
3238

3339

40+
def cleanup_json(expected_json):
41+
"""
42+
Remove existing json file before test if any
43+
"""
44+
if (isfile(f"pacts/{expected_json}")):
45+
remove(f"pacts/{expected_json}")
46+
47+
3448
def test_generate_pact_file(pact):
49+
cleanup_json(expected_json)
50+
3551
expected_event = {
3652
'documentName': 'document.doc',
3753
'creator': 'TP',
@@ -46,14 +62,17 @@ def test_generate_pact_file(pact):
4662
'Content-Type': 'application/json'
4763
}))
4864

49-
with patch.object(pact, 'write_to_pact_file') as mock:
50-
with pact:
51-
# handler needs 'documentType' == 'microsoft-word'
52-
MessageHandler(expected_event)
53-
mock.assert_called_once()
65+
with pact:
66+
# handler needs 'documentType' == 'microsoft-word'
67+
MessageHandler(expected_event)
68+
69+
time.sleep(1)
70+
assert isfile(f"pacts/{expected_json}") == 1
5471

5572

5673
def test_throw_exception_handler(pact):
74+
cleanup_json(expected_json)
75+
5776
wrong_event = {
5877
'documentName': 'spreadsheet.xls',
5978
'creator': 'WI',
@@ -69,8 +88,9 @@ def test_throw_exception_handler(pact):
6988
}))
7089

7190
with pytest.raises(CustomError):
72-
with patch.object(pact, 'write_to_pact_file') as mock:
73-
with pact:
74-
# handler needs 'documentType' == 'microsoft-word'
75-
MessageHandler(wrong_event)
76-
mock.assert_not_called()
91+
with pact:
92+
# handler needs 'documentType' == 'microsoft-word'
93+
MessageHandler(wrong_event)
94+
95+
time.sleep(1)
96+
assert isfile(f"pacts/{expected_json}") == 0

0 commit comments

Comments
 (0)