-
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.
- Loading branch information
1 parent
8d2b4d5
commit 63a55bc
Showing
6 changed files
with
101 additions
and
2 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,20 @@ | ||
name: CI | ||
on: [workflow_dispatch, pull_request, push] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: 3.9 | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
- name: Test with pytest | ||
run: | | ||
python -m pytest |
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
# faststream-gen-template | ||
Template files for creating a new FastStream project with the faststream-gen library. | ||
This project is auto generated using the faststream-gen library. |
Empty file.
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,31 @@ | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from faststream import FastStream, Logger | ||
from faststream.kafka import KafkaBroker | ||
|
||
|
||
class CourseUpdates(BaseModel): | ||
course_name: str = Field(..., examples=[ | ||
"Biology"], description="Course example") | ||
new_content: Optional[str] = Field( | ||
default=None, examples=["New content"], description="Content example" | ||
) | ||
|
||
|
||
broker = KafkaBroker("localhost:9092") | ||
app = FastStream(broker) | ||
|
||
|
||
@broker.publisher("notify_updates") | ||
@broker.subscriber("course_updates") | ||
async def on_course_update(msg: CourseUpdates, logger: Logger) -> CourseUpdates: | ||
logger.info(msg) | ||
|
||
if msg.new_content: | ||
logger.info(f"Course has new content {msg.new_content=}") | ||
msg = CourseUpdates( | ||
course_name=("Updated: " + msg.course_name), new_content=msg.new_content | ||
) | ||
return msg |
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 @@ | ||
faststream[kafka, testing]==0.0.1.dev20230912 |
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,48 @@ | ||
import pytest | ||
|
||
from faststream.kafka import TestKafkaBroker | ||
|
||
from app.application import CourseUpdates, broker, on_course_update | ||
|
||
|
||
@broker.subscriber("notify_updates") | ||
async def on_notify_update(msg: CourseUpdates): | ||
pass | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_app_without_new_content(): | ||
async with TestKafkaBroker(broker): | ||
await broker.publish(CourseUpdates(course_name="Biology"), "course_updates") | ||
on_course_update.mock.assert_called_with( | ||
dict(CourseUpdates(course_name="Biology")) | ||
) | ||
on_notify_update.mock.assert_called_with( | ||
dict(CourseUpdates(course_name="Biology")) | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_app_with_new_content(): | ||
async with TestKafkaBroker(broker): | ||
await broker.publish( | ||
CourseUpdates( | ||
course_name="Biology", new_content="We have additional classes..." | ||
), | ||
"course_updates", | ||
) | ||
on_course_update.mock.assert_called_with( | ||
dict( | ||
CourseUpdates( | ||
course_name="Biology", new_content="We have additional classes..." | ||
) | ||
) | ||
) | ||
on_notify_update.mock.assert_called_with( | ||
dict( | ||
CourseUpdates( | ||
course_name="Updated: Biology", | ||
new_content="We have additional classes...", | ||
) | ||
) | ||
) |