def test_register_same_name(self): """ Trying to register a Tukio task with a name already used must raise a `ValueError` exception. """ register('my-coro-task')(other_coro) with self.assertRaisesRegex(ValueError, 'already registered'): register('my-coro-task')(other_coro)
def setUpClass(cls): # Save the initial state of `TaskRegistry` cls._backup = _save_registry() # Register all tasks used in the tests register('my-task-holder', 'do_it')(MyTaskHolder) register('my-coro-task')(my_coro_task) register('task-bad-inputs', 'dummy')(BadInitTaskHolder1) register('task-init-exc', 'dummy')(BadInitTaskHolder2) register('task-bad-coro', 'dummy')(BadExecTaskHolder1)
def test_register_basic_class(self): """ A class that does not inherit from `TaskHolder` is still a valid task holder if registered properly. """ register('basic-task', 'mycoro')(BasicTaskHolder) klass, coro_fn = TaskRegistry.get('basic-task') self.assertIs(klass, BasicTaskHolder) self.assertIs(coro_fn, BasicTaskHolder.mycoro)
def setUpClass(cls): cls._backup = _save_registry() register('my-task-holder', 'do_it')(MyTaskHolder) register('basic-holder', 'mycoro')(BasicTaskHolder) register('dummy-coro')(my_coro_task) register('bad-coro-task')(bad_coro_task) cls.loop = asyncio.get_event_loop() cls.holder = MyTaskHolder({'hello': 'world'}) cls.basic = BasicTaskHolder({'hello': 'world'})
def test_register_not_coroutine(self): """ Trying to register a function or method that is not a coroutine must raise a `TypeError` exception. """ err = 'not a coroutine function' registered = len(TaskRegistry.all()) # A regular function cannot be registered with self.assertRaisesRegex(TypeError, err): register('dummy')(my_func) # A generator cannot be registered either with self.assertRaisesRegex(TypeError, err): register('dummy')(my_gen) with self.assertRaisesRegex(TypeError, err): register('dummy')(my_gen()) # A simple method is not a valid task with self.assertRaisesRegex(TypeError, err): register('dummy-task', 'dummy')(BadInitTaskHolder3) # No new item must have been registered self.assertEqual(registered, len(TaskRegistry.all()))
def test_bad_register_class(self): """ Trying to register a class with invalid/missing parameters passed to `register()` must raise a `ValueError` or `AttributeError` exception. """ registered = len(TaskRegistry.all()) # Register a class without the name of the method to register with self.assertRaisesRegex(TypeError, 'getattr()'): register('dummy-task')(MyTaskHolder) # Register a class with an invalid `coro_name` arg with self.assertRaisesRegex(TypeError, 'getattr()'): register('dummy-task', object())(MyTaskHolder) # Register a class with a wrong method name with self.assertRaisesRegex(AttributeError, 'yolo'): register('dummy-task', 'yolo')(MyTaskHolder) # No new item must have been registered self.assertEqual(registered, len(TaskRegistry.all()))
def test_valid_registrations(self): """ Check the current registry (provisioned by registered task holder and coroutine) holds the right references. """ # A task holder is registered as a tuple made of (klass, coro_fn) register('my-task-holder', 'do_it')(MyTaskHolder) klass, coro_fn = TaskRegistry.get('my-task-holder') self.assertIs(klass, MyTaskHolder) self.assertIs(coro_fn, MyTaskHolder.do_it) self.assertEqual(MyTaskHolder.TASK_NAME, 'my-task-holder') # A coroutine is registered as a tuple made of (None, coro_fn) register('my-coro-task')(my_coro_task) klass, coro_fn = TaskRegistry.get('my-coro-task') self.assertIs(klass, None) self.assertIs(coro_fn, my_coro_task) # A coroutine can be registered sevral times with distinct names. # Note that an extra parameter passed to register will be ignored. register('other-coro-task', 'dummy')(my_coro_task) klass, coro_fn = TaskRegistry.get('other-coro-task') self.assertIs(klass, None) self.assertIs(coro_fn, my_coro_task)