airtai / faststream

FastStream is a powerful and easy-to-use Python framework for building asynchronous services interacting with event streams such as Apache Kafka, RabbitMQ, NATS and Redis.
https://faststream.airt.ai/latest/
Apache License 2.0
2.52k stars 128 forks source link

Topics with dots cause failure of Tester instantiation. #303

Closed Gerleff closed 1 year ago

Gerleff commented 1 year ago

It's related with current Tester instantiation logic, where AsyncMocks are created as named tuple If consumer has topic named "testtopic" and its prefix is "on", then AsyncMocks(on test_topic=AsyncMock()) is created But if topic named is "ecosystem.project.service.test_topic", then error is raised.

Suggestions: 1) Provide argument "alias" to FastKafka.consumes and FastKafka.produces and use alias for mocks 2) Replace dots with underscores on AsyncMocks creation 3) Use dictionary instead of named tuple (my fav)

sternakt commented 1 year ago

Hi Gerleff, We'll go with the option two now, so your tester functions for topics with dots will have them replaced by underscores.

I'll work on it now, and we'll try to release the patch today

sternakt commented 1 year ago

Should be patched now, when you define the topics names with dots in the name, the tester mirrored functions will replace the dots with underscores (and add the prefixes)

For example:

@app.consumes(topic="my.topic.with_dots")
async def consume_func(...)
    ....

with Tester(app):
    await tester.to_my_topic_with_dots(...)
    ...

You can find the patched release here: https://pypi.org/project/fastkafka/0.6.1/

Gerleff commented 1 year ago

Thanks for fix!