Skip to content
Closed
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
16 changes: 12 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ jobs:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6
with:
python-version: '3.14'
architecture: 'x64'
python-version: '3.14'
architecture: 'x64'
- run: pip install -r requirements.txt
- name: Test pactflow-example-consumer-python against ${{ matrix.pact_provider }}
run: make test
Expand All @@ -48,7 +48,11 @@ jobs:
needs: test
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- run: docker pull pactfoundation/pact-cli:latest
- uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6
with:
python-version: '3.14'
architecture: 'x64'
- run: pip install -r requirements.txt
- name: Can I deploy?
run: GIT_BRANCH=${GIT_REF:11} make can_i_deploy

Expand All @@ -58,7 +62,11 @@ jobs:
needs: can-i-deploy
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- run: docker pull pactfoundation/pact-cli:latest
- uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6
with:
python-version: '3.14'
architecture: 'x64'
- run: pip install -r requirements.txt
- name: Deploy
run: GIT_BRANCH=${GIT_REF:11} make deploy
if: github.ref == 'refs/heads/master'
12 changes: 5 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# It's set as a secure environment variable in the build.yml file
PACTICIPANT := "pactflow-example-consumer-python"
GITHUB_WEBHOOK_UUID := "04510dc1-7f0a-4ed2-997d-114bfa86f8ad"
PACT_CLI="docker run --rm -v ${PWD}:${PWD} -e PACT_BROKER_BASE_URL -e PACT_BROKER_TOKEN -e PACT_BROKER_USERNAME -e PACT_BROKER_PASSWORD pactfoundation/pact-cli:latest"

# Only deploy from master
ifeq ($(GIT_BRANCH),master)
Expand Down Expand Up @@ -30,7 +29,7 @@ fake_ci: .env
make ci

publish_pacts: .env
@"${PACT_CLI}" publish ${PWD}/pacts --consumer-app-version ${GIT_COMMIT} --branch ${GIT_BRANCH}
pact broker publish ${PWD}/pacts --consumer-app-version ${GIT_COMMIT} --branch ${GIT_BRANCH}

## =====================
## Build/test tasks
Expand All @@ -50,7 +49,7 @@ no_deploy:

can_i_deploy: .env
echo "can_i_deploy"
@"${PACT_CLI}" broker can-i-deploy \
pact broker can-i-deploy \
--pacticipant ${PACTICIPANT} \
--version ${GIT_COMMIT} \
--to-environment production \
Expand All @@ -61,7 +60,7 @@ deploy_app:
@echo "Deploying to prod"

record_deployment: .env
@"${PACT_CLI}" broker record-deployment --pacticipant ${PACTICIPANT} --version ${GIT_COMMIT} --environment production
pact broker record-deployment --pacticipant ${PACTICIPANT} --version ${GIT_COMMIT} --environment production

## =====================
## PactFlow set up tasks
Expand All @@ -80,8 +79,7 @@ create_github_token_secret:
# so that any PRs will get a status that shows what the status of
# the pact is.
create_or_update_github_webhook:
@"${PACT_CLI}" \
broker create-or-update-webhook \
pact broker create-or-update-webhook \
'https://api.github.com/repos/pactflow/example-consumer/statuses/$${pactbroker.consumerVersionNumber}' \
--header 'Content-Type: application/json' 'Accept: application/vnd.github.v3+json' 'Authorization: token $${user.githubCommitStatusToken}' \
--request POST \
Expand Down Expand Up @@ -125,4 +123,4 @@ venv:
ln -sf ${CURDIR}/.venv ~/.pyenv/versions/${PROJECT}

@echo "\n$(green)Use it! (populate .python-version)$(sgr0)"
pyenv local ${PROJECT}
pyenv local ${PROJECT}
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Flask==3.1.3
pytest==9.0.3
requests==2.33.1
pact_python==2.3.3
pact-python==3.3.1
pact-python-cli==2.6.0.0
47 changes: 15 additions & 32 deletions tests/consumer/test_products_consumer.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,39 @@
"""pact test for product service client via ruby core"""
"""pact test for product service client"""

import logging
import os
from typing import Generator

import pytest
from pact import Consumer, Like, Provider, Term, Format
from pact import Pact, match

from src.consumer import ProductConsumer

log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
print(Format().__dict__)

PACT_MOCK_HOST = 'localhost'
PACT_MOCK_PORT = 1234
PACT_DIR = os.path.dirname(os.path.realpath(__file__))


@pytest.fixture
def consumer():
return ProductConsumer(
'http://{host}:{port}'
.format(host=PACT_MOCK_HOST, port=PACT_MOCK_PORT)
)
@pytest.fixture(scope='session')
def pact() -> Generator[Pact, None, None]:
pact = Pact('pactflow-example-consumer-python', 'pactflow-example-provider-python')
yield pact.with_specification('V4')
pact.write_file('./pacts')


@pytest.fixture(scope='session')
def pact(request):
pact = Consumer('pactflow-example-consumer-python').has_pact_with(
Provider('pactflow-example-provider-python'), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT,
pact_dir="./pacts", log_dir="./logs")
try:
print('start service')
pact.start_service()
yield pact
finally:
print('stop service')
pact.stop_service()

def test_get_product(pact, consumer):
def test_get_product(pact) -> None:
expected = {
'id': "27",
'name': 'Margharita',
'type': 'Pizza'
}

(pact
.given('a product with ID 10 exists')
.upon_receiving('a request to get a product')
.with_request('GET', '/product/10')
.will_respond_with(200, body=Like(expected)))
.given('a product with ID 10 exists')
.with_request(method='GET', path='/product/10')
.will_respond_with(200)
.with_body(match.like(expected)))

with pact:
with pact.serve() as srv:
consumer = ProductConsumer(str(srv.url))
user = consumer.get_product('10')
assert user.name == 'Margharita'
37 changes: 0 additions & 37 deletions tests/consumer/test_products_consumer_v3.py

This file was deleted.

Loading