def test_remote_event_no_event(self): registry = Registry(self.app) event = registry.remote_event('app', 'event') expected_event = Event.remote_instance('app', 'event') self.assertEqual(expected_event, event) self.assertIs(self.app, event.app) self.assertTrue(event.is_remote)
def test_remote_event(self): registry = Registry(self.app) existing_event = Event.remote_instance('app', 'event') registry.events.append(existing_event) event = registry.remote_event('app', 'event') self.assertEqual(existing_event, event) self.assertTrue(event.is_remote)
class BackendTestCase(TestCase): def setUp(self): self.app = App() self.registry = Registry(self.app) self.namespaces = ['app_1', 'app_2'] self.local_events_from_backend = [] self.remote_events_from_backend = [] self.deleted_events = [] self.created_events = [] self.created_tasks = [] self.removed_tasks = [] self.updated_tasks = [] test_case = self class TestBackend(Backend): def get_local_namespaces(self): return test_case.namespaces def get_task_namespace(self, task): return task.name.split('.')[0] def fetch_events_for_namespaces(self, app_names): return test_case.local_events_from_backend def fetch_events(self, events): return [ event for event in test_case.local_events_from_backend + test_case.remote_events_from_backend if event in events ] def should_update_event(self, event): return True def delete_events(self, events): for event in events: test_case.deleted_events.append(event) def create_events(self, events): for event in events: test_case.created_events.append(event) def create_tasks(self, event, tasks): for task in tasks: test_case.created_tasks.append((event, task)) def remove_tasks(self, event, tasks): for task in tasks: test_case.removed_tasks.append((event, task)) def update_tasks(self, event, tasks): for task in tasks: test_case.updated_tasks.append((event, task)) self.backend_cls = TestBackend def test_update_local_event(self): local_event = self.registry.create_local_event('app_1', 'event_1') local_event.add_task(Task.remote_instance('task_1', use_routes=False)) local_event_from_backend = Event.local_instance('app_1', 'event_1') self.local_events_from_backend.append(local_event_from_backend) local_event_from_backend.add_task( Task.remote_instance('task_2', use_routes=False)) backend = self.backend_cls(self.registry) backend.update_local_event(local_event) self.assertEqual(1, len(self.registry.events)) self.assertEqual(local_event, self.registry.events[0]) self.assertEqual([ Task.remote_instance('task_1', use_routes=False), Task.remote_instance('task_2', use_routes=False) ], self.registry.events[0].tasks) def test_update_local_event_no_additional_remote_tasks(self): local_event = self.registry.create_local_event('app_1', 'event_1') local_event.add_task(Task.remote_instance('task_1', use_routes=False)) local_event_from_backend = Event.local_instance('app_1', 'event_1') self.local_events_from_backend.append(local_event_from_backend) backend = self.backend_cls(self.registry) backend.update_local_event(local_event) self.assertEqual(1, len(self.registry.events)) self.assertEqual(local_event, self.registry.events[0]) self.assertEqual([Task.remote_instance('task_1', use_routes=False)], self.registry.events[0].tasks) def test_update_local_event_no_event_from_backend(self): local_event = self.registry.create_local_event('app_1', 'event_1') local_event.add_task(Task.remote_instance('task_1', use_routes=False)) backend = self.backend_cls(self.registry) backend.update_local_event(local_event) self.assertEqual(1, len(self.registry.events)) self.assertEqual(local_event, self.registry.events[0]) self.assertEqual([Task.remote_instance('task_1', use_routes=False)], self.registry.events[0].tasks) def test_sync_local_events_create_event(self): local_event = self.registry.create_local_event('app_1', 'event_1') local_event.add_task(Task.local_instance('task_1', use_routes=False)) backend = self.backend_cls(self.registry) backend.sync_local_events() self.assertEqual(1, len(self.created_events)) created_event = self.created_events[0] self.assertEqual(local_event, created_event) self.assertEqual(0, len(self.created_tasks)) self.assertEqual(0, len(self.deleted_events)) self.assertEqual(0, len(self.removed_tasks)) self.assertEqual(0, len(self.updated_tasks)) def test_sync_local_events_delete_event(self): local_event = Event.local_instance('app_1', 'event_1') self.local_events_from_backend.append(local_event) local_event.add_task(Task.local_instance('task_1', use_routes=False)) backend = self.backend_cls(self.registry) backend.sync_local_events() self.assertEqual(0, len(self.created_events)) self.assertEqual(0, len(self.created_tasks)) self.assertEqual(1, len(self.deleted_events)) deleted_event = self.deleted_events[0] self.assertEqual(local_event, deleted_event) self.assertEqual(0, len(self.removed_tasks)) self.assertEqual(0, len(self.updated_tasks)) def test_sync_remote_events_create_task(self): remote_event = self.registry.remote_event('app_3', 'event_3') self.remote_events_from_backend.append(remote_event) local_task = remote_event.add_task( Task.local_instance('task_1', use_routes=False)) backend = self.backend_cls(self.registry) backend.sync_remote_events() self.assertEqual(0, len(self.created_events)) self.assertEqual(1, len(self.created_tasks)) event, created_task = self.created_tasks[0] self.assertEqual(remote_event, event) self.assertEqual(local_task, created_task) self.assertEqual(0, len(self.deleted_events)) self.assertEqual(0, len(self.removed_tasks)) self.assertEqual(0, len(self.updated_tasks)) def test_sync_remote_events_remove_task(self): remote_event = self.registry.remote_event('app_3', 'event_3') remote_event_from_backend = Event.remote_instance('app_3', 'event_3') self.remote_events_from_backend.append(remote_event_from_backend) local_task_from_backend = remote_event_from_backend.add_task( Task.local_instance('app_1.task_1', use_routes=False)) backend = self.backend_cls(self.registry) backend.sync_remote_events() self.assertEqual(0, len(self.created_events)) self.assertEqual(0, len(self.created_tasks)) self.assertEqual(0, len(self.deleted_events)) self.assertEqual(1, len(self.removed_tasks)) event, removed_task = self.removed_tasks[0] self.assertEqual(remote_event, event) self.assertEqual(local_task_from_backend, removed_task) self.assertEqual(0, len(self.updated_tasks)) def test_sync_remove_events_update_task(self): remote_event = self.registry.remote_event('app_3', 'event_3') remote_event_from_backend = Event.remote_instance('app_3', 'event_3') self.remote_events_from_backend.append(remote_event_from_backend) local_task = remote_event.add_task( Task.local_instance('app_1.task_1', queue='new_queue')) remote_event_from_backend.add_task( Task.local_instance('app_1.task_1', queue='old_queue')) backend = self.backend_cls(self.registry) backend.sync_remote_events() self.assertEqual(0, len(self.created_events)) self.assertEqual(0, len(self.created_tasks)) self.assertEqual(0, len(self.deleted_events)) self.assertEqual(0, len(self.removed_tasks)) self.assertEqual(1, len(self.updated_tasks)) updated_event, updated_task = self.updated_tasks[0] self.assertEqual(remote_event, updated_event) self.assertEqual(local_task, updated_task) self.assertEqual(local_task.queue, updated_task.queue) def test_sync_remove_events_remote_event_not_found(self): self.registry.remote_event('app_3', 'event_3') backend = self.backend_cls(self.registry) backend.sync_remote_events() self.assertEqual(0, len(self.created_events)) self.assertEqual(0, len(self.created_tasks)) self.assertEqual(0, len(self.deleted_events)) self.assertEqual(0, len(self.removed_tasks)) self.assertEqual(0, len(self.updated_tasks))