def test_allowed_fire(self): app = App('test_allowed_fire', config) def fire(self, names): names.append('fire') event = Event('display', names=names, name='B') self.fire(event) foo_handler = Handler(foo) boo_handler = Handler(boo) eoo_handler = Handler(eoo) fire_handler = Handler(fire, ['display']) app.on('show', foo_handler) app.on('show', boo_handler, True) app.on('show', fire_handler, True) app.on('display', eoo_handler) names = list() show_event = Event('show', names=names, name='A') runtime_tree = app.fire(show_event, False) self.assertListEqual(names, ['foo-A', 'boo-A', 'fire', 'eoo-B']) self.assertEqual(runtime_tree.event_count, 2) self.assertEqual(runtime_tree.handler_runtime_count, 4) self.assertEqual(runtime_tree.exception_count, 1) self.assertNotEqual(runtime_tree.begin_time, None) self.assertNotEqual(runtime_tree.end_time, None) self.assertNotEqual(runtime_tree.time_cost, -1)
def test_start_and_stop(self): app = App('test_start_and_stop', config) app.start() foo_handler = Handler(foo) boo_handler = Handler(boo) app.on('show', foo_handler) app.on('show', boo_handler, True) names = list() show_event = Event('show', names=names, name='background') app.fire(show_event, False) time.sleep(2) app.stop() self.assertListEqual(names, ['foo-background', 'boo-background'])
def test_no_emittion(self): class AEvent(Event): pass class BEvent(Event): pass def foo(context, event): return NoEmittion(BEvent, 'test reason') handler = Handler(AEvent, foo, derivative_events=[BEvent]) handler_runtime = handler.handle(None, AEvent()) self.assertEqual(handler_runtime.why_no_emittion(BEvent), 'test reason')
def test_on_and_find(self): app = App('test_on_and_find', config) handlers = app.find_handlers('show') self.assertListEqual(handlers, list()) foo_handler = Handler(foo) boo_handler = Handler(boo) app.on('show', foo_handler) app.on('show', boo_handler, True) handlers = app.find_handlers('show') self.assertListEqual(handlers, [foo_handler, boo_handler]) self.assertListEqual(app._App__global.event_handler_map['show'], [foo_handler]) self.assertListEqual(app._App__local.event_handler_map['show'], [boo_handler])
def test_handle_unexcepted_event(self): names = [] class AEvent(Event): name = Field(str, 'earo') class BEvent(Event): name = Field(str, 'bbq') def foo(context, event): names.append(event.name) handler = Handler(AEvent, foo) with self.assertRaises(TypeError): handler.handle(None, BEvent())
def test_time_cost(self): handler = Handler(foo) handler_runtime = HandlerRuntime(handler, None) self.assertLess(handler_runtime.time_cost, 0) handler_runtime.record_begin_time() self.assertLess(handler_runtime.time_cost, 0) handler_runtime.record_end_time() self.assertGreater(handler_runtime.time_cost, 0)
def test_save_and_find(self): storage = RuntimeTreeStorage(db_path) app = App('test_save_and_find') foo_handler = Handler(foo) app.on('show', foo_handler) show_event = Event('show', name='A') runtime_tree = app.fire(show_event, False) storage.save(runtime_tree) self.assertEqual(runtime_tree.dict, storage.find(runtime_tree.id))
def test_succeeded(self): handler = Handler(foo) handler_runtime = HandlerRuntime(handler, None) self.assertFalse(handler_runtime.succeeded) handler_runtime.record_begin_time() self.assertFalse(handler_runtime.succeeded) handler_runtime.record_end_time() self.assertTrue(handler_runtime.succeeded) handler_runtime.record_exception(Exception()) self.assertFalse(handler_runtime.succeeded)
def test_handle_excepted_event(self): names = [] class AEvent(Event): name = Field(str, 'earo') def foo(context, event): names.append(event.name) handler = Handler(AEvent, foo) handler_runtime = handler.handle(None, AEvent()) self.assertEqual(names, ['earo']) self.assertIsNotNone(handler_runtime.begin_time) self.assertIsNotNone(handler_runtime.end_time) self.assertIsNone(handler_runtime.exception) self.assertTrue(handler_runtime.succeeded) self.assertGreaterEqual(handler_runtime.time_cost, 0)
def test_pickle(self): app = App('test_pickle', config) def fire(self): names.append('fire') event = Event('display', names=names, name='B') self.fire(event) foo_handler = Handler(foo) boo_handler = Handler(boo) eoo_handler = Handler(eoo) fire_handler = Handler(fire, ['display']) app.on('show', foo_handler) app.on('show', boo_handler, True) app.on('show', fire_handler, True) app.on('display', eoo_handler) names = list() show_event = Event('show', names=names, name='A') runtime_tree = app.fire(show_event, False) self.assertDictEqual(RuntimeTree.loads(runtime_tree.dumps()), runtime_tree.dict)
def test_handler_decorator(self): app = App('test_handler_decorator', config) @app.handler('show', ['display'], True) def koo(self, names, name): names.append('koo-%s' % (name, )) event = Event('display', names=names, name='B') self.fire(event) foo_handler = Handler(foo) app.on('display', foo_handler) names = list() show_event = Event('show', names=names, name='A') runtime_tree = app.fire(show_event, False) self.assertListEqual(names, ['koo-A', 'foo-B']) self.assertEqual(runtime_tree.event_count, 2) self.assertEqual(runtime_tree.handler_runtime_count, 2) self.assertEqual(runtime_tree.exception_count, 0) self.assertNotEqual(runtime_tree.begin_time, None) self.assertNotEqual(runtime_tree.end_time, None) self.assertNotEqual(runtime_tree.time_cost, -1)
def test_handle_excepted_event(self): handler = Handler(AEvent, foo) event = AEvent() handler_runtime = HandlerRuntime(handler, event) handler.handle(event, handler_runtime) self.assertEqual(['earo'], names)
def test_handler_name(self): handler = Handler(foo) handler_runtime = HandlerRuntime(handler, None) self.assertEqual('__main__.foo', handler.name)
def test_handler_name(self): handler = Handler(AEvent, foo) self.assertEqual('__main__.foo', handler.name)
def test_handle_unexcepted_event(self): handler = Handler(AEvent, foo) event = BEvent() handler_runtime = HandlerRuntime(handler, event) with self.assertRaises(TypeError): handler.handle(event, handler_runtime)
def test_handle_excepted_event(self): handler = Handler(AEvent, foo) event = AEvent() handler_runtime = HandlerRuntime(handler, event) handler.handle(event, handler_runtime) self.assertEqual(["earo"], names)