1- """pact test for user service client """
1+ """pact test for a message consumer """
22
33import logging
44import pytest
1414logging .basicConfig (level = logging .INFO )
1515
1616PACT_BROKER_URL = "http://localhost"
17- PACT_FILE = "userserviceclient-userservice.json"
1817PACT_BROKER_USERNAME = "pactbroker"
1918PACT_BROKER_PASSWORD = "pactbroker"
2019PACT_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' )
2827def 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
4748def 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
6465def 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