Search code examples
pythondjangodjango-channelschanneldjango-unittest

How to write unittests for Consumers in django channels?


A'm beginner in django-channels. I'm use channels 2.3.1. I have following code.

main routing

websocket_urlpatterns = [
    url(r'^ws/events/(?P<room_type>[^/]+)/(?P<room_id>[^/]+)/$', consumers.Consumer, name='core-consumer'),
]

app routing

application = ProtocolTypeRouter({
    'websocket': URLRouter(core.routing.websocket_urlpatterns),
})

consumer

class Consumer(AsyncWebsocketConsumer):
    async def connect(self):
        room_type = self.scope['url_route']['kwargs']['room_type']
        room_id = self.scope['url_route']['kwargs']['room_id']
        self.room_group_name = f'{room_type}_{room_id}'

        await self.channel_layer.group_add(self.room_group_name, self.channel_name)
        await self.accept()

    async def receive(self, text_data = None, bytes_data = None):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        await self.channel_layer.group_send(self.room_group_name,
            {'type': 'send_data', 'message': message}
        )

    async def disconnect(self, close_code):
        await self.channel_layer.group_discard(self.room_group_name, self.channel_name)

    async def send_data(self, event):
        message = event['message']
        await self.send(text_data=json.dumps({
            'data': message
        }))

def send_event(channel='', message=''):
    channel_layer = get_channel_layer()
    asyncio.run(channel_layer.group_send(channel,
        {
            'type': 'send_data',
            'message': message
        }
    ))

signal

    def activate_notification(self):
        send_event(f'user_{self.id}',
                   {'message': f"Welcome back to one deeds!"})

How can I test every function inside a consumer using unittest?

I read this documentation but nothing understand(

I want to run test with command ./manage.py test

I tried testing this with WebsocketCommunicator


Solution

  • I would suggest a few things.

    1) use the channels.layers.InMemoryChannelLayer layer.

    2) use pytest-asyncio so that your test code can be async methods

    @pytest.mark.django_db(transaction=True)
    @pytest.mark.asyncio
    async def test_get():
       ....
    

    to run these test you will then use pytest

    3) use the WebsocketCommunicator check these examples.