You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is an e2e example that uses messages, including a sample implementation of a message handler.
4
4
5
-
## Setup
5
+
## Consumer
6
6
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.
8
10
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
+
+-----------+ +-------------------+
13
17
```
14
18
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`)
16
20
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
+
classCustomError(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
+
ifself.topic:
31
+
return'Custom Error:, {0}'.format(self.topic)
32
+
33
+
classMessageHandler(object):
34
+
def__init__(self, event):
35
+
self.pass_event(event)
36
+
37
+
@staticmethod
38
+
defpass_event(event):
39
+
if event.get('documentType') !='microsoft-word':
40
+
raise CustomError("Not correct document type")
20
41
```
21
42
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.
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}"`.
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:
0 commit comments