-
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
63a55bc
commit 4eed0a0
Showing
2 changed files
with
0 additions
and
79 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 |
---|---|---|
@@ -1,31 +0,0 @@ | ||
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 |
---|---|---|
@@ -1,48 +0,0 @@ | ||
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...", | ||
) | ||
) | ||
) | ||