Skip to content

Commit

Permalink
Add initial files
Browse files Browse the repository at this point in the history
  • Loading branch information
harishmohanraj committed Sep 12, 2023
1 parent 63a55bc commit 4eed0a0
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 79 deletions.
31 changes: 0 additions & 31 deletions app/application.py
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
48 changes: 0 additions & 48 deletions tests/test_application.py
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...",
)
)
)

0 comments on commit 4eed0a0

Please sign in to comment.