Hello Django users,
This sounds like a pretty newbish question, but I am struggling on how to write tests for Django-channels 2 websocket consumers that access the database. I am not familiar with pytest and asynchronous programming (the bread and butter of Channels testing), so while I caught up with pytest, I am still a bit lost between `async` and `await`.
Using Django/Django-REST, when I test a view method which modifies a database object for a given request, here is what I usually do:
- Assert the initial state of the object ;
- Use the test client to send the request ;
- Assert the outcome of the request ;
- Assert the final state of the object, which should be different.
All of this synchronously. With Channels, I thought I could do the same for a consumer method which modifies a database object on a given event:
- Assert the initial state of the object ;
- Use the test communicator to send the event ;
- Assert the final state of the object, which should have changed.
All of this asynchronously, as stated in the documentation, but this is a synchronous test scenario! Obviously, it does not work, as step 2. is executed with `await` and step 3. is achieved before step 2. finishes. I tried to execute step 2. synchronously using `async_to_sync` but it does not work (I can give details).
I am missing something, how can I test such a consumer method? Is this possible? The documentation does not give any hint on this…
Help would be much appreciated!
Cheers,
-- Neraste
No comments:
Post a Comment