Skip to content

Commit e097153

Browse files
docs: update readme
1 parent fc0d91c commit e097153

File tree

1 file changed

+148
-15
lines changed

1 file changed

+148
-15
lines changed

examples/message/README.md

Lines changed: 148 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,135 @@
22

33
This is an e2e example that uses messages, including a sample implementation of a message handler.
44

5-
## Setup
5+
## Consumer
66

7-
Create your own virtualenv for this. Run
7+
A Consumer is the system that will be reading a message from a queue or some intermediary. In this example, the consumer is a Lambda function that handles the message.
8+
9+
From a Pact testing point of view, Pact takes the place of the intermediary (MQ/broker etc.) and confirms whether or not the consumer is able to handle a request.
810

9-
```bash
10-
pip install -r requirements.txt
11-
pip install ../../
12-
pytest
11+
```
12+
+-----------+ +-------------------+
13+
| (Pact) | message |(Message Consumer) |
14+
| MQ/broker |--------->|Lambda Function |
15+
| | |check valid doc |
16+
+-----------+ +-------------------+
1317
```
1418

15-
This should provide you with a relative path to pact install relatively (2 dirs up)
19+
Below is a sample message handler that only accepts that the key `documentType` would only be `microsoft-word`. If not, the message handler will throw an exception (`CustomError`)
1620

17-
Create the local broker (for demo purposes only) Open a separate terminal in the examples/broker folder and run:
18-
```bash
19-
docker-compose up
21+
```python
22+
class CustomError(Exception):
23+
def __init__(self, *args):
24+
if args:
25+
self.topic = args[0]
26+
else:
27+
self.topic = None
28+
29+
def __str__(self):
30+
if self.topic:
31+
return 'Custom Error:, {0}'.format(self.topic)
32+
33+
class MessageHandler(object):
34+
def __init__(self, event):
35+
self.pass_event(event)
36+
37+
@staticmethod
38+
def pass_event(event):
39+
if event.get('documentType') != 'microsoft-word':
40+
raise CustomError("Not correct document type")
2041
```
2142

22-
If you can open a browser to http://localhost and see the broker you have succeeded.
23-
If needed, log-in using the provided details in tests such as:
43+
Below is a snippet from a test where the message handler has no error.
44+
Since the expected event contains a key `documentType` with value `microsoft-word`, message handler does not throw an error and a pact file `f"pacts/{expected_json}"` is expected to be generated.
45+
46+
```python
47+
def test_generate_new_pact_file(pact):
48+
cleanup_json(PACT_FILE)
49+
50+
expected_event = {
51+
'documentName': 'document.doc',
52+
'creator': 'TP',
53+
'documentType': 'microsoft-word'
54+
}
55+
56+
(pact
57+
.given('A document create in Document Service')
58+
.expects_to_receive('Description')
59+
.with_content(expected_event)
60+
.with_metadata({
61+
'Content-Type': 'application/json'
62+
}))
63+
64+
with pact:
65+
# handler needs 'documentType' == 'microsoft-word'
66+
MessageHandler(expected_event)
2467

68+
progressive_delay(f"{PACT_FILE}")
69+
assert isfile(f"{PACT_FILE}") == 1
2570
```
26-
PACT_BROKER_USERNAME = "pactbroker"
27-
PACT_BROKER_PASSWORD = "pactbroker"
71+
72+
For a similar test where the event does not contain a key `documentType` with value `microsoft-word`, a `CustomError` is generated and there there is no generated json file `f"pacts/{expected_json}"`.
73+
74+
```python
75+
def test_throw_exception_handler(pact):
76+
cleanup_json(PACT_FILE)
77+
wrong_event = {
78+
'documentName': 'spreadsheet.xls',
79+
'creator': 'WI',
80+
'documentType': 'microsoft-excel'
81+
}
82+
83+
(pact
84+
.given('Another document in Document Service')
85+
.expects_to_receive('Description')
86+
.with_content(wrong_event)
87+
.with_metadata({
88+
'Content-Type': 'application/json'
89+
}))
90+
91+
with pytest.raises(CustomError):
92+
with pact:
93+
# handler needs 'documentType' == 'microsoft-word'
94+
MessageHandler(wrong_event)
95+
96+
progressive_delay(f"{PACT_FILE}")
97+
assert isfile(f"{PACT_FILE}") == 0
98+
```
99+
100+
Otherwise, no pact file is generated.
101+
102+
## Provider
103+
104+
```
105+
+-------------------+ +-----------+
106+
|(Message Provider) | message | (Pact) |
107+
|Document Upload |--------->| MQ/broker |
108+
|Service | | |
109+
+-------------------+ +-----------+
110+
```
111+
112+
## E2E Messaging
113+
114+
```
115+
+-------------------+ +-----------+ +-------------------+
116+
|(Message Provider) | message | (Pact) | message |(Message Consumer) |
117+
|Document Upload |--------->| MQ/broker |--------->|Lambda Function |
118+
|Service | | | |check valid doc |
119+
+-------------------+ +-----------+ +-------------------+
120+
```
121+
122+
Note: The current example only tests the consumer side. In the future, provider tests will also be included.
123+
124+
# Setup
125+
126+
## Virtual Environment
127+
128+
Go to the `example/message` directory Create your own virtualenv for this. Run
129+
130+
```bash
131+
pip install -r requirements.txt
132+
pip install -e ../../
133+
pytest
28134
```
29135

30136
## Message Consumer
@@ -38,5 +144,32 @@ pytest
38144
Or you can run individual tests like:
39145

40146
```bash
41-
pytest tests/consumer/test_message_consumer.py::test_generate_pact_file
147+
pytest tests/consumer/test_message_consumer.py::test_generate_new_pact_file
148+
```
149+
150+
## With Broker
151+
152+
The current consumer test can run even without a local broker,
153+
but this is added for demo purposes.
154+
155+
Open a separate terminal in the `examples/broker` folder and run:
156+
157+
```bash
158+
docker-compose up
159+
```
160+
161+
Open a browser to http://localhost and see the broker you have succeeded.
162+
If needed, log-in using the provided details in tests such as:
163+
164+
```
165+
PACT_BROKER_USERNAME = "pactbroker"
166+
PACT_BROKER_PASSWORD = "pactbroker"
167+
```
168+
169+
To get the consumer to publish a pact to broker,
170+
open a new terminal in the `examples/message` and run the following (2 is an arbitary version number). The first part makes sure that the an existing json has been generated:
171+
172+
```bash
173+
pytest tests/consumer/test_message_consumer.py::test_publish_to_broker
174+
pytest tests/consumer/test_message_consumer.py::test_publish_to_broker --publish-pact 2
42175
```

0 commit comments

Comments
 (0)