Skip to content

Commit

Permalink
ci(python): test python project in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Oct 22, 2024
1 parent a3b495d commit 799172c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 32 deletions.
30 changes: 16 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
6 changes: 2 additions & 4 deletions consumer-python-kafka/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
7 changes: 5 additions & 2 deletions provider-python-kafka/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ publish_pacts:
## =====================

test:
python3 -m pytest
python3 -m pytest -s

## =====================
## Deploy tasks
Expand Down Expand Up @@ -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:
Expand All @@ -104,4 +107,4 @@ venv:
pyenv local ${PROJECT}

run:
python3 run.py
python3 src/server.py
16 changes: 12 additions & 4 deletions provider-python-kafka/src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
'acks': 'all'
}

producer = Producer(config)


class ProductRepository:

PRODUCT_TOPIC = 'products'
@staticmethod
def create_product(data):
if "id" not in data:
Expand All @@ -33,16 +35,21 @@ 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"],
'id': data["id"],
'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
Expand Down Expand Up @@ -81,4 +88,5 @@ def delete_product(product_id):
return '', 200

if __name__ == '__main__':
producer = Producer(config)
app.run(port=8081, debug=True)
13 changes: 6 additions & 7 deletions provider-python-kafka/tests/message_product_pact_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down

0 comments on commit 799172c

Please sign in to comment.