Use a mixin for common test-case code. This way we can have both (#305)

a regular channels test-case, and a transaction test-case, too.
This commit is contained in:
Luke Hodkinson 2016-08-21 10:53:54 +10:00 committed by Andrew Godwin
parent 57fa3bed67
commit 6649afce8e
2 changed files with 13 additions and 5 deletions

View File

@ -1,2 +1,2 @@
from .base import ChannelTestCase, Client, apply_routes # NOQA isort:skip from .base import TransactionChannelTestCase, ChannelTestCase, Client, apply_routes # NOQA isort:skip
from .http import HttpClient # NOQA isort:skip from .http import HttpClient # NOQA isort:skip

View File

@ -5,7 +5,7 @@ import random
import string import string
from functools import wraps from functools import wraps
from django.test.testcases import TestCase from django.test.testcases import TestCase, TransactionTestCase
from .. import DEFAULT_CHANNEL_LAYER from .. import DEFAULT_CHANNEL_LAYER
from ..channel import Group from ..channel import Group
from ..routing import Router, include from ..routing import Router, include
@ -14,7 +14,7 @@ from ..message import Message
from asgiref.inmemory import ChannelLayer as InMemoryChannelLayer from asgiref.inmemory import ChannelLayer as InMemoryChannelLayer
class ChannelTestCase(TestCase): class ChannelTestCaseMixin(object):
""" """
TestCase subclass that provides easy methods for testing channels using TestCase subclass that provides easy methods for testing channels using
an in-memory backend to capture messages, and assertion methods to allow an in-memory backend to capture messages, and assertion methods to allow
@ -31,7 +31,7 @@ class ChannelTestCase(TestCase):
""" """
Initialises in memory channel layer for the duration of the test Initialises in memory channel layer for the duration of the test
""" """
super(ChannelTestCase, self)._pre_setup() super(ChannelTestCaseMixin, self)._pre_setup()
self._old_layers = {} self._old_layers = {}
for alias in self.test_channel_aliases: for alias in self.test_channel_aliases:
# Swap in an in memory layer wrapper and keep the old one around # Swap in an in memory layer wrapper and keep the old one around
@ -52,7 +52,7 @@ class ChannelTestCase(TestCase):
# Swap in an in memory layer wrapper and keep the old one around # Swap in an in memory layer wrapper and keep the old one around
channel_layers.set(alias, self._old_layers[alias]) channel_layers.set(alias, self._old_layers[alias])
del self._old_layers del self._old_layers
super(ChannelTestCase, self)._post_teardown() super(ChannelTestCaseMixin, self)._post_teardown()
def get_next_message(self, channel, alias=DEFAULT_CHANNEL_LAYER, require=False): def get_next_message(self, channel, alias=DEFAULT_CHANNEL_LAYER, require=False):
""" """
@ -70,6 +70,14 @@ class ChannelTestCase(TestCase):
return Message(content, recv_channel, channel_layers[alias]) return Message(content, recv_channel, channel_layers[alias])
class ChannelTestCase(ChannelTestCaseMixin, TestCase):
pass
class TransactionChannelTestCase(ChannelTestCaseMixin, TransactionTestCase):
pass
class Client(object): class Client(object):
""" """
Channel client abstraction that provides easy methods for testing full live cycle of message in channels Channel client abstraction that provides easy methods for testing full live cycle of message in channels