|
| 1 | +"""pact test for a message consumer""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import pytest |
| 5 | +import json |
| 6 | + |
| 7 | +from pact import MessageConsumer, Provider |
| 8 | +from src.kafka_consumer import send_dog_event, send_dog_event_foo, CustomError |
| 9 | + |
| 10 | +log = logging.getLogger(__name__) |
| 11 | +logging.basicConfig(level=logging.INFO) |
| 12 | + |
| 13 | +PACT_BROKER_URL = "http://localhost" |
| 14 | +PACT_BROKER_USERNAME = "pactbroker" |
| 15 | +PACT_BROKER_PASSWORD = "pactbroker" |
| 16 | +PACT_DIR = 'pacts' |
| 17 | + |
| 18 | +CONSUMER_NAME = 'AnimalConsumer' |
| 19 | +PROVIDER_NAME = 'DogProducer' |
| 20 | + |
| 21 | +PACT_FILE = (f"{PACT_DIR}/{CONSUMER_NAME.lower().replace(' ', '_')}_message-" |
| 22 | + + f"{PROVIDER_NAME.lower().replace(' ', '_')}_message.json") |
| 23 | + |
| 24 | +EXPECTED_DOG = {'type': 'dog', 'name': 'spot', 'breed': 'poodle'} |
| 25 | + |
| 26 | +@pytest.fixture(scope='session') |
| 27 | +def pact(request): |
| 28 | + version = request.config.getoption('--publish-pact') |
| 29 | + publish = True if version else False |
| 30 | + |
| 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, |
| 34 | + broker_username=PACT_BROKER_USERNAME, broker_password=PACT_BROKER_PASSWORD, pact_dir=PACT_DIR) |
| 35 | + |
| 36 | + yield pact |
| 37 | + |
| 38 | + |
| 39 | +def test_assert_verify_message(pact): |
| 40 | + |
| 41 | + (pact |
| 42 | + .given('A dog created') |
| 43 | + .expects_to_receive('Spot the poodle') |
| 44 | + .with_content(str(json.dumps(EXPECTED_DOG))) |
| 45 | + .with_metadata({ |
| 46 | + 'Content-Type': 'application/json' |
| 47 | + }) |
| 48 | + .verify(send_dog_event) |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def test_throw_exception_handler(pact): |
| 53 | + |
| 54 | + with pytest.raises(CustomError): |
| 55 | + (pact |
| 56 | + .given('A dog created') |
| 57 | + .expects_to_receive('Spot the poodle') |
| 58 | + .with_content(str(json.dumps({'type': 'cat', 'name': 'spot', 'breed': 'poodle'}))) |
| 59 | + .with_metadata({ |
| 60 | + 'Content-Type': 'application/json' |
| 61 | + }) |
| 62 | + .verify(send_dog_event)) |
| 63 | + |
| 64 | + |
| 65 | +def test_assert_dog_returned(pact): |
| 66 | + |
| 67 | + (pact |
| 68 | + .given('A dog created') |
| 69 | + .expects_to_receive('Spot the poodle') |
| 70 | + .with_content(str(json.dumps(EXPECTED_DOG))) |
| 71 | + .with_metadata({ |
| 72 | + 'Content-Type': 'application/json' |
| 73 | + }) |
| 74 | + .verify(send_dog_event) |
| 75 | + ) |
| 76 | + |
| 77 | + with pact: |
| 78 | + dog = pact.result |
| 79 | + assert dog.name == 'spot' |
| 80 | + assert dog.breed == 'poodle' |
| 81 | + |
| 82 | + |
| 83 | +def test_assert_calling_dog(pact): |
| 84 | + |
| 85 | + (pact |
| 86 | + .given('A dog created') |
| 87 | + .expects_to_receive('Spot the poodle') |
| 88 | + .with_content(str(json.dumps(EXPECTED_DOG))) |
| 89 | + .with_metadata({ |
| 90 | + 'Content-Type': 'application/json' |
| 91 | + }) |
| 92 | + ) |
| 93 | + |
| 94 | + with pact: |
| 95 | + dog = send_dog_event_foo({}, str(json.dumps(EXPECTED_DOG))) |
| 96 | + assert dog.name == 'spot' |
| 97 | + assert dog.breed == 'poodle' |
0 commit comments