OpenSlides/tests/utils/test_dispatch.py
Norman Jäckel 1fb1f17d15 New API for widgets using a metaclass.
It is now possible to define a default state and to submit extra stylefiles and javascript files when defining a widget in an app. This is done by a new metaclass in openslides.utils.dispatch. Also fixed some other tests.
2013-12-01 21:30:12 +01:00

81 lines
2.5 KiB
Python

# -*- coding: utf-8 -*-
from django.dispatch import Signal
from django.test.client import RequestFactory
from mock import patch
from openslides.utils.dispatch import SignalConnectMetaClass
from openslides.utils.test import TestCase
class TestBaseOne(object):
__metaclass__ = SignalConnectMetaClass
signal = Signal()
@classmethod
def get_dispatch_uid(cls):
if not cls.__name__ == 'TestBaseOne':
return 'test_vieM1eingi6luish5Sei'
class TestBaseTwo(object):
__metaclass__ = SignalConnectMetaClass
signal = Signal()
@classmethod
def get_dispatch_uid(cls):
pass
class TestSignalConnectMetaClass(TestCase):
request_factory = RequestFactory()
@patch('tests.utils.test_dispatch.TestBaseOne.signal')
def test_call_signal_send(self, mock_signal):
TestBaseOne.get_all(self.request_factory.request)
self.assertTrue(mock_signal.send.called)
@patch('tests.utils.test_dispatch.TestBaseOne.signal')
def test_call_signal_connect(self, mock_signal):
class TestChildOne(TestBaseOne):
pass
self.assertTrue(mock_signal.connect.called)
self.assertEqual(mock_signal.connect.call_args[0][0], TestChildOne)
self.assertEqual(mock_signal.connect.call_args[1], dict(dispatch_uid='test_vieM1eingi6luish5Sei'))
def test_bad_base_class(self):
def wrapper():
class BadClass1(object):
__metaclass__ = SignalConnectMetaClass
self.assertRaisesMessage(
NotImplementedError,
'Your class BadClass1 must have a get_dispatch_uid classmethod.',
wrapper)
def test_bad_base_class_without_signal(self):
def wrapper():
class BadClass2(object):
__metaclass__ = SignalConnectMetaClass
@classmethod
def get_dispatch_uid(cls):
return True
self.assertRaisesMessage(
NotImplementedError,
'Your class BadClass2 must have a signal argument, which must be a Django Signal instance.',
wrapper)
def test_receive_signal(self):
class TestChildTwo(TestBaseTwo):
def __init__(self, sender, **kwargs):
pass
@classmethod
def get_dispatch_uid(self):
return 'test_leeve5eighahT3zooxe5'
childtwo = TestBaseTwo.get_all(self.request_factory.request)[0]
self.assertEqual(type(childtwo), TestChildTwo)