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

Support checking keys in tests #557

Open
davorrunje opened this issue Sep 6, 2023 · 7 comments · Fixed by #1649
Open

Support checking keys in tests #557

davorrunje opened this issue Sep 6, 2023 · 7 comments · Fixed by #1649
Assignees
Labels
Core Issues related to core FastStream functionality and affects to all brokers enhancement New feature or request
Milestone

Comments

@davorrunje
Copy link
Collaborator

davorrunje commented Sep 6, 2023

Please see the example and let me know what you think:

import pytest

from pydantic import BaseModel, Field, NonNegativeFloat

from faststream import FastStream, Logger
from faststream.kafka import KafkaBroker, TestKafkaBroker


class Data(BaseModel):
    data: NonNegativeFloat = Field(
        ..., examples=[0.5], description="Float data example"
    )


broker = KafkaBroker("localhost:9092")
app = FastStream(broker)


to_output_data = broker.publisher("output_data")


@broker.subscriber("input_data")
# we should be able to get the key (and other stuff like partion, offset, etc injected)
async def on_input_data(msg: Data, logger: Logger, key: bytes) -> None:
    logger.info(f"on_input_data({msg=})")
    await to_output_data.publish(Data(data=msg.data + 1.0), key=b"key")

@broker.subscriber("output_data")
async def on_output_data(msg: Data, logger: Logger, key: bytes) -> None:
    logger.info(f"on_output_data({msg=})")


@pytest.mark.skip("we are not checking the key")
@pytest.mark.asyncio
async def test_base_app():
    async with TestKafkaBroker(broker):
        # we should be able to publish a message with the key
        await broker.publish(Data(data=0.2), "input_data", key=b"my_key")

        # we need to check the key as well
        on_input_data.mock.assert_called_once_with(dict(Data(data=0.2)), key=b"my_key")
        to_output_data.mock.assert_called_once_with(dict(Data(data=1.2)), key=b"key")
        on_output_data.mock.assert_called_once_with(dict(Data(data=1.2)), key=b"key")
@Lancetnik
Copy link
Member

For now user is able to access any message field via the Context

In the tests case, I am not sure that is required
Will answer with examples in a few hours

@davorrunje
Copy link
Collaborator Author

it would be very useful to be able to check Context in the tests, how difficult would that be to implement?

@Lancetnik
Copy link
Member

Lancetnik commented Sep 6, 2023

We trigger mock here: https://github.com/airtai/fastkafka/blob/FastStream/faststream/broker/wrapper.py#L90
It is not a big problem to add something extra to it, but we need to create an API this extra information

@davorrunje
Copy link
Collaborator Author

This is not a priority right now, but I'll leave the issue for later.

@Lancetnik Lancetnik added the enhancement New feature or request label Sep 7, 2023
@Lancetnik Lancetnik moved this to Backlog in FastStream Sep 7, 2023
@Lancetnik
Copy link
Member

I have an idea - we can just add assert_called_* methods to the consumer itself

This way we can override any information before pass it to the inner mock object and add some extra checks

consumer.assert_called_once_with({"user": "John", "age": 25})
# instead
consumer.mock.assert_called_once_with({"user": "John", "age": 25})

This way we can make the following checks are equal:

consumer.assert_called_once_with({"user": "John", "age": 25})
consumer.assert_called_once_with(user="John", age=25)
consumer.assert_called_once_with(User(user="John", age=25))

And add any context check feature

consumer.assert_called_once_with(
    body={"user": "John", "age": 25},
    context={
        "message.key": b"key"
    }
)

@davorrunje
Copy link
Collaborator Author

not bad, I like it

@Lancetnik
Copy link
Member

Lancetnik commented Dec 15, 2023

Alternatively (and much easier) we can add assert_called_with_context method to check just a context

consumer.mock.assert_called_once_with({"user": "John", "age": 25})
consumer.assert_called_with_context({"message.key": b"key"})

@Lancetnik Lancetnik added this to the 0.6.0 milestone Mar 26, 2024
@Lancetnik Lancetnik added the Core Issues related to core FastStream functionality and affects to all brokers label May 16, 2024
@Lancetnik Lancetnik mentioned this issue Jun 9, 2024
63 tasks
@github-project-automation github-project-automation bot moved this from Waiting for merge to Done in FastStream Aug 24, 2024
@Lancetnik Lancetnik reopened this Aug 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core Issues related to core FastStream functionality and affects to all brokers enhancement New feature or request
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants