diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fbcc7a9..fb8ab80 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,12 +14,12 @@ jobs: - macos-13 - macos-14 project: - - rust - - dotnet - - go - - java - - js - # - python + # - rust + # - dotnet + # - go + # - java + # - js + - python fail-fast: false runs-on: ${{ matrix.os }} defaults: @@ -65,23 +65,25 @@ jobs: if: ${{ matrix.project == 'python' }} with: python-version: '3.11.5' - - name: setup pyenv - if: ${{ matrix.project == 'python' }} - uses: "gabrielfalcao/pyenv-action@v18" - with: - default: '3.11.5' - command: pip install -U pip - name: install (consumer) run: make install if: ${{matrix.project == 'js'||matrix.project == 'go'||matrix.project == 'python'}} working-directory: consumer-${{matrix.project}}-kafka - name: pact test (consumer) - run: make test + run: | + if [ "${{ matrix.project }}" == "python" ]; then + source $(poetry env info --path)/bin/activate + fi + make test working-directory: consumer-${{matrix.project}}-kafka - name: install (provider) run: make install if: ${{matrix.project == 'js'||matrix.project == 'go'||matrix.project == 'python'}} working-directory: provider-${{matrix.project}}-kafka - name: pact verification (provider) - run: make test + run: | + if [ "${{ matrix.project }}" == "python" ]; then + source $(poetry env info --path)/bin/activate + fi + make test working-directory: provider-${{matrix.project}}-kafka diff --git a/consumer-python-kafka/Makefile b/consumer-python-kafka/Makefile index 2c2ff51..0cdb9e1 100644 --- a/consumer-python-kafka/Makefile +++ b/consumer-python-kafka/Makefile @@ -113,12 +113,10 @@ sgr0 := $(shell tput sgr0) red := $(shell tput setaf 1) green := $(shell tput setaf 2) -install: - pip install poetry - make venv - make deps +install: deps deps: + pip3 install poetry poetry install venv: diff --git a/consumer-python-kafka/tests/unit/product_service_pact_test.py b/consumer-python-kafka/tests/unit/product_service_pact_test.py index 074bb76..25b5dca 100644 --- a/consumer-python-kafka/tests/unit/product_service_pact_test.py +++ b/consumer-python-kafka/tests/unit/product_service_pact_test.py @@ -69,6 +69,6 @@ def test_receive_a_product_update(pact, handler, verifier): .upon_receiving("a product event update", "Async") .with_body(event, "application/json") - .with_metadata({"topic": "products"}) + .with_metadata({"kafka_topic": "products"}) ) pact.verify(verifier, "Async") \ No newline at end of file diff --git a/provider-python-kafka/Makefile b/provider-python-kafka/Makefile index 8334c1f..851ec27 100644 --- a/provider-python-kafka/Makefile +++ b/provider-python-kafka/Makefile @@ -36,7 +36,7 @@ publish_pacts: ## ===================== test: - python3 -m pytest + python3 -m pytest -s ## ===================== ## Deploy tasks @@ -80,7 +80,10 @@ sgr0 := $(shell tput sgr0) red := $(shell tput setaf 1) green := $(shell tput setaf 2) +install: deps + deps: + pip3 install poetry poetry install venv: @@ -104,4 +107,4 @@ venv: pyenv local ${PROJECT} run: - python3 run.py \ No newline at end of file + python3 src/server.py \ No newline at end of file diff --git a/provider-python-kafka/src/server.py b/provider-python-kafka/src/server.py index 7b65145..34897fd 100644 --- a/provider-python-kafka/src/server.py +++ b/provider-python-kafka/src/server.py @@ -11,9 +11,11 @@ 'acks': 'all' } -producer = Producer(config) + class ProductRepository: + + PRODUCT_TOPIC = 'products' @staticmethod def create_product(data): if "id" not in data: @@ -33,6 +35,13 @@ def delete_product(data): @staticmethod def _send_event(event_type, data): + msg = ProductRepository.produce_event(event_type, data) + producer.produce(msg[0],value=msg[1].encode('utf-8')) + producer.poll(10000) + producer.flush() + + @staticmethod + def produce_event(event_type, data): product_event = { 'event': event_type, 'type': data["type"], @@ -40,9 +49,7 @@ def _send_event(event_type, data): 'version': data["version"], 'name': data["name"] } - producer.produce('products', value=json.dumps(data).encode('utf-8')) - producer.poll(10000) - producer.flush() + return (ProductRepository.PRODUCT_TOPIC, json.dumps(product_event)) class ProductController: @staticmethod @@ -81,4 +88,5 @@ def delete_product(product_id): return '', 200 if __name__ == '__main__': + producer = Producer(config) app.run(port=8081, debug=True) \ No newline at end of file diff --git a/provider-python-kafka/tests/message_product_pact_test.py b/provider-python-kafka/tests/message_product_pact_test.py index 8caf3fc..7ff1113 100644 --- a/provider-python-kafka/tests/message_product_pact_test.py +++ b/provider-python-kafka/tests/message_product_pact_test.py @@ -30,24 +30,23 @@ def message_producer_function() -> tuple[str, str, str]: producer = ProductRepository() - # TODO - The demo provider_server mirrors the same bug in pact-python v2 # incorrect mapping of states, rather than description assert CURRENT_STATE is not None, "Message Description is not set" function_name = responses.get(CURRENT_STATE, {}).get("function_name") assert function_name is not None, "Function name could not be found" if function_name == "product_event_update": - updated_product = producer.create_product({"event": "UPDATED", "name": "Some Product", "type": "Product Range"}) + updated_product = producer.produce_event("UPDATED",{"name": "Some Product", "type": "Product Range","version":"v1", "id":"123"}) return ( - json.dumps(updated_product), + updated_product[1], "application/json", - '{"topic":"products"}' + f'{{"kafka_topic":"{updated_product[0]}"}}' ) return ( - '{"event":"UPDATED","id": "some-uuid-1234-5678", "name": "Some Product","type": "Product Range"}', - "application/json", - '{"topic":"products"}' + nil, + nil, + nil )