-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Dockerfile and add app & tests for it
- Loading branch information
1 parent
fba149b
commit 8d6db66
Showing
4 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |