Testing Channels database data missing in Consumer

38 views
Skip to first unread message

Daniel Gilge

unread,
Feb 6, 2018, 6:37:47 PM2/6/18
to Django users
Hi,

this question is related to Channels 2.0, Django 2.0, pytest 3.4, pytest-django 3.1 and pytest-asyncio 0.8.

I created a pytest:

@pytest.fixture
def db_with_obj(db):
   
factories.MyModelFactory()

@pytest.mark.asyncio
async
def test_something(db_with_obj):
   
print(models.MyModelFactory.objects.all())
    obj
= models.MyModelFactory.objects.first()
    communicator
= WebsocketCommunicator(MyConsumer, 'ws/')
    connected
, subprotocol = await communicator.connect()
   
assert connected
    await communicator
.send_to(text_data=json.dumps({
       
'id': obj.pk,
       
'content': 'my content',
   
}))
    response
= await communicator.receive_from()
   
assert response == 'my content'
    await communicator
.disconnect()

print gives me <QuerySet [<MyModel: 1>]>

Here my Consumer code:

class MyConsumer(WebsocketConsumer):
    def receive(self, text_data=None, bytes_data=None):
        print(models.MyModel.objects.all())
        # ...

print gives me <QuerySet []>

I don't know what I'm doing wrong. I didn't use both, pytest and Python async code, before. Therefore I don't know where to look for the solution, though I think it's easy to solve. Any hint appreciated.

Thanks :)

Andrew Godwin

unread,
Feb 6, 2018, 10:31:14 PM2/6/18
to django...@googlegroups.com
I'm not quite sure either. Does the test setup work if you are not in async mode?

Andrew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7e5f63f4-789c-413e-a8d2-fcf8ea62a2cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Gilge

unread,
Feb 7, 2018, 12:51:03 PM2/7/18
to Django users
Hi Andrew,

thanks for your reply.

This test is passing (using the same packages):

def test_fixture(db_with_segment, admin_client):
   
assert models.MyModel.objects.count() == 1
    response
= admin_client.get('/api/my_model/list/')
   
assert len(response.json()['results']) == 1

I tried to write a simpler async test with asyncio.wait but I didn't complete it so far.

I thought to just write ChannelsLiveServerTestCase tests but I couldn't manage to serve static files there. (Both, setting static_handler = StaticFilesHandler – what StaticLiveServerTestCase does – and running collectstatic didn't work.)

Finally I ran my development server and did some tests in the browser console. There it works. I'll continue coding using this technique to "test" my code and try to find a solution for the other (real) tests as I have time.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Andrew Godwin

unread,
Feb 7, 2018, 1:09:29 PM2/7/18
to django...@googlegroups.com
Interesting. I did note that you called ORM methods in an async context, which is not how you are supposed to do it, but I also believe that should work. Maybe try splitting the ORM methods into a separate function and awaiting it using SyncToAsync?

As for the staticfiles in live test case problem, I have opened a ticket for that: https://github.com/django/channels/issues/880

Andrew

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages