Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add TestClient class #28

Merged
merged 4 commits into from
Apr 17, 2023
Merged

✨ Add TestClient class #28

merged 4 commits into from
Apr 17, 2023

Conversation

gabrielmbmb
Copy link
Owner

@gabrielmbmb gabrielmbmb commented Apr 17, 2023

I've added a TestClient class that allows building unit tests for consumers functions:

import pytest

from kaflow import FromValue, Kaflow, Message
from kaflow.testclient import TestClient

app = Kaflow(client_id="test", brokers=["localhost:9092"])


class RandomDependency:
    def __init__(self, random: str = "random") -> None:
        self.random = random


@app.consume(topic="test_topic", sink_topics=["test_topic_2"])
async def consume_test_topic(
    value: FromValue[bytes], dependency: RandomDependency
) -> Message:
    print("Value", value)
    print("Override dependency:", dependency.random)
    return Message(value=b"Hello Unit Test!")


@pytest.fixture
def test_client() -> TestClient:
    client = TestClient(app=app)

    with app.dependency_overrides:
        app.dependency_overrides[RandomDependency] = lambda: RandomDependency(
            random="not so random"
        )
        yield client


def test_consume_test_topic(test_client: TestClient) -> None:
    message = test_client.publish(
        topic="test_topic",
        value=b"Hello World!",
    )
    assert message.value == b"Hello Unit Test!"

@gabrielmbmb gabrielmbmb merged commit ef9f8a7 into main Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant