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

If the object (message) is changed in consumes/produces functions, tester doesn't work properly. #60

Open
rjambrecic opened this issue Jul 26, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@rjambrecic
Copy link
Contributor

e.g.

class Currency(BaseModel):
    currency:` str = Field(..., description="Currency")

@app.consumes(prefix="on", topic="store_product")
async def on_store_product(msg: Currency):
    msg.currency = "EUR"
    
async with Tester(app).using_inmemory_broker() as tester:
    await tester.to_store_product(Currency(currency="HRK"))
    await app.awaited_mocks.on_store_product.assert_called_with(
        Currency(currency="HRK"),
        timeout=5
    )

assertion would fail:
AssertionError: expected call not found. Expected: mock(Currency(currency='HRK')) Actual: mock(Currency(currency='EUR'))

Quick fix: add at the beginning of each consumes/produces:
msg_copy = msg.model_copy()
and use the copy in the rest of the function...

Good fix: add msg.model_copy() to the Tester implementation

@davorrunje
Copy link
Contributor

Why do you think creating a copy is a good idea? It is clearly overhead and not needed in most use cases. If needed, it is easy to create a copy in the body of the function.

@kumaranvpl kumaranvpl transferred this issue from airtai/faststream Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants