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 8d2b4d5 commit 63a55bc
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 2 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
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
3 changes: 1 addition & 2 deletions README.md
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 added app/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions app/application.py
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
faststream[kafka, testing]==0.0.1.dev20230912
48 changes: 48 additions & 0 deletions tests/test_application.py
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...",
)
)
)

0 comments on commit 63a55bc

Please sign in to comment.