'While testing my Websocket Consumer a model object is not created (Django Channels)

I'm new in Django Channels and I'm trying to build a simple chat app. But when I'm trying to test my async Websocket Consumer I run into the following exception: chat.models.RoomModel.DoesNotExist: RoomModel matching query does not exist.

It seems like the test room is not created.

test.py file is the following:

class ChatTest(TestCase):

    @sync_to_async
    def set_data(self):
        room = RoomModel.objects.create_room('Room1', 'room_password_123')
        room_slug = room.slug
        user = User.objects.create_user(username='User1', password='user_password_123')
        print(RoomModel.objects.all()) # querySet here contains the created room
        return room_slug, user

    async def test_my_consumer(self):
        room_slug, user = await self.set_data()
        application = URLRouter([
            re_path(r'ws/chat/(?P<room_name>\w+)/$', ChatConsumer.as_asgi()),
        ])
        communicator = WebsocketCommunicator(application, f'/ws/chat/{room_slug}/')
        communicator.scope['user'] = user
        connected, subprotocol = await communicator.connect()
        self.assertTrue(connected)
        await communicator.send_json_to({'message': 'hello'})
        response = await communicator.receive_json_from()
        self.assertEqual('hello', response)
        await communicator.disconnect()

My consumer.py file is the following:

class ChatConsumer(AsyncWebsocketConsumer):

    async def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = f'chat_{self.room_name}'
        await self.channel_layer.group_add(self.room_group_name, self.channel_name)
        await self.accept()

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

    async def receive(self, text_data):
        self.message = json.loads(text_data).get('message')
        data = {'type': 'chat_message'}
        data.update(await self.create_message())
        await self.channel_layer.group_send(self.room_group_name, data)

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

    @database_sync_to_async
    def create_message(self):
        print(RoomModel.objects.all()) # qyerySet turns out to be empty here
        room = RoomModel.objects.get(slug=self.room_name)
        msg = room.messagemodel_set.create(text=self.message, user_name=self.scope['user'])
        return {
            'message': msg.text,
            'user': self.scope['user'].username,
            'timestamp': msg.timestamp.strftime("%d/%m/%Y, %H:%M")
        }

I would be grateful for any help.



Solution 1:[1]

When you try to run room = RoomModel.objects.create_room('Room1', 'room_password_123') you are trying to commit a transaction.

And this cannot happen until the end of the test because TestCase wraps each test within a transaction. It waits until the end of the test to create this object.

And since you are awaiting set_data, the flow goes on to execute the rest of the test, i.e., it reaches the call to create_message where it tries to get the RoomModel object, which will be not present in the db yet since the transaction has not been committed.

Solution 2:[2]

I had a similar problem that I solved using this link https://github.com/django/channels/issues/1110. To summarize, you have to change TestCase to TransactionTestCase

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 shailysangwan
Solution 2 Jojo