Skip to content

Commit

Permalink
Add Dockerfile and add app & tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl committed Sep 13, 2023
1 parent fba149b commit 8d6db66
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.9-slim-bullseye

SHELL ["/bin/bash", "-c"]
WORKDIR /project

ADD app /project/app
COPY requirements.txt /project/

RUN pip install --no-cache-dir -r /project/requirements.txt

CMD ["faststream", "run", "--workers", "1", "app.application:app"]
27 changes: 27 additions & 0 deletions app/application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from pydantic import BaseModel, Field

from faststream import FastStream, Logger
from faststream.kafka import KafkaBroker


class MultiplyMessage(BaseModel):
a: int = Field(..., examples=[2], description="Integer value")
b: int = Field(..., examples=[3], description="Integer value")


class Results(BaseModel):
res: int = Field(..., examples=[6], description="Result of multiplication")


broker = KafkaBroker("localhost:9092")
app = FastStream(broker)


@broker.subscriber("multiply")
@broker.publisher("results")
async def multiply_numbers(msg: MultiplyMessage, logger: Logger) -> Results:
logger.info(msg)

result = msg.a * msg.b
result_obj = Results(res=result)
return result_obj
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "app"
version = "0.0.1"
14 changes: 14 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
from faststream.kafka import TestKafkaBroker

from app.application import MultiplyMessage, Results, broker, multiply_numbers


@pytest.mark.asyncio
async def test_multiply_numbers():
async with TestKafkaBroker(broker):
@broker.subscriber("results")
async def on_result(msg: Results):
assert msg.res == 6

await broker.publish(MultiplyMessage(a=2, b=3), "multiply")

0 comments on commit 8d6db66

Please sign in to comment.