Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion examples/broker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3'
version: '3.9'

services:
# A PostgreSQL database for the Broker to store Pacts and verification results
Expand Down Expand Up @@ -32,6 +32,13 @@ services:
PACT_BROKER_DATABASE_NAME: postgres
PACT_BROKER_BASIC_AUTH_USERNAME: pactbroker
PACT_BROKER_BASIC_AUTH_PASSWORD: pactbroker
# The Pact Broker provides a healthcheck endpoint which we will use to wait
# for it to become available before starting up
healthcheck:
test: [ "CMD", "wget", "-q", "--tries=1", "--spider", "http://pactbroker:pactbroker@localhost:9292/diagnostic/status/heartbeat" ]
interval: 1s
timeout: 2s
retries: 5

# An NGINX reverse proxy in front of the Broker on port 8443, to be able to
# terminate with SSL
Expand All @@ -44,3 +51,7 @@ services:
- ./ssl:/etc/nginx/ssl
ports:
- "8443:443"
restart: always
depends_on:
broker_app:
condition: service_healthy
7 changes: 7 additions & 0 deletions pact/pact.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .broker import Broker
from .constants import MOCK_SERVICE_PATH
from .matchers import from_term
from .verify_wrapper import PactException


class Pact(Broker):
Expand Down Expand Up @@ -165,6 +166,12 @@ def given(self, provider_state):
def setup(self):
"""Configure the Mock Service to ready it for a test."""
try:
# First, check that the interactions are all complete
for interaction in self._interactions:
missing_fields = [f for f in self.MANDATORY_FIELDS if f not in interaction]
if missing_fields:
raise PactException(f"Interaction incomplete, missing field(s): {', '.join(missing_fields)}")

interactions_uri = f"{self.uri}/interactions"
resp = requests.delete(
interactions_uri, headers=self.HEADERS, verify=False
Expand Down
20 changes: 20 additions & 0 deletions tests/test_pact.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pact.constants import MOCK_SERVICE_PATH
from pact.pact import Pact, FromTerms, Request, Response
from pact import pact as pact
from pact.verify_wrapper import PactException


class PactTestCase(TestCase):
Expand Down Expand Up @@ -559,6 +560,25 @@ def test_context_raises_error(self):
self.assertFalse(self.mock_verify.called)


class PactContextManagerSetupTestCase(PactTestCase):
def test_definition_without_description(self):
# Description (populated from "given") is listed in the MANDATORY_FIELDS.
# Make sure if it isn't there that an exception is raised
pact = Pact(self.consumer, self.provider)
(pact.given("A request without a description")
.with_request('GET', '/path')
.will_respond_with(200, body='success'))

self.assertEqual(len(pact._interactions), 1)

self.assertTrue('description' not in pact._interactions[0])

# By using "with", __enter__ will call the setup method that will verify if this is present
with self.assertRaises(PactException):
with pact:
pact.verify()


class FromTermsTestCase(TestCase):
def test_json(self):
with self.assertRaises(NotImplementedError):
Expand Down