def test_wildcard_handler(): core.cleanup_handlers() @eventlib.handler('stuff.*') def do_nothing(data): pass core.HANDLER_REGISTRY.should.have.length_of(1) core.HANDLER_REGISTRY['stuff.*'].should.be.equals([do_nothing])
def test_external_handler(): core.cleanup_handlers() @eventlib.external_handler('stuff.Klass') def handle_stuff(self): pass core.EXTERNAL_HANDLER_REGISTRY.should.have.length_of(1) core.EXTERNAL_HANDLER_REGISTRY['stuff.Klass'].should.be.equals([handle_stuff])
def test_log_when_debug_is_false(conf, datetime, find_event, tasks): conf.getsetting.return_value = False core.cleanup_handlers() datetime.now.return_value = 'tea time' eventlib.log('app.Event') tasks.process_task.delay.assert_called_once_with('app.Event', ejson.dumps({ '__ip_address__': '0.0.0.0', '__datetime__': 'tea time', }))
def test_handler_registry(): core.cleanup_handlers() @eventlib.handler('stuff.Klass') def do_nothing(data): return len(data) core.HANDLER_REGISTRY.should.have.length_of(1) core.HANDLER_REGISTRY['stuff.Klass'].should.be.equals([do_nothing])
def test_process_external(find_event): core.cleanup_handlers() handler = Mock() eventlib.external_handler('app.Event')(handler) data = {'file': '/etc/passwd', 'server': 'yipster'} core.process_external('app.Event', data) handler.assert_called_once_with(data)
def test_handler_with_methods(): core.cleanup_handlers() class MyEvent(eventlib.BaseEvent): @eventlib.handler def handle_stuff(self): pass core.HANDLER_REGISTRY.should.have.length_of(1) core.HANDLER_REGISTRY[MyEvent].should.be.equals([MyEvent.handle_stuff])
def test_external_handler(): core.cleanup_handlers() @eventlib.external_handler('stuff.Klass') def handle_stuff(self): pass core.EXTERNAL_HANDLER_REGISTRY.should.have.length_of(1) core.EXTERNAL_HANDLER_REGISTRY['stuff.Klass'].should.be.equals( [handle_stuff])
def test_process_raises_the_exception_when_debugging(settings, find_event): core.cleanup_handlers() settings.DEBUG = True handler_fail = Mock() handler_fail.side_effect = ValueError('P0wned!!!') eventlib.handler('myapp.CoolEvent')(handler_fail) name, data = 'myapp.CoolEvent', ejson.dumps({'a': 1}) core.process.when.called_with(name, data).should.throw( ValueError, 'P0wned!!!')
def test_process_raises_the_exception_when_debugging(settings, find_event): core.cleanup_handlers() settings.DEBUG = True handler_fail = Mock() handler_fail.side_effect = ValueError('P0wned!!!') eventlib.handler('myapp.CoolEvent')(handler_fail) name, data = 'myapp.CoolEvent', ejson.dumps({'a': 1}) core.process.when.called_with(name, data).should.throw(ValueError, 'P0wned!!!')
def test_process_data_clean_raise_errors(logger, find_event): core.cleanup_handlers() class MyEvent(eventlib.BaseEvent): def clean(self): raise exceptions.ValidationError('Owned!!11') data = {'name': 'Lincoln', 'answer': 42} find_event.return_value = MyEvent core.process.when.called_with('stuff', ejson.dumps(data)).should.throw( exceptions.ValidationError)
def test_handler_with_methods(): core.cleanup_handlers() class MyEvent(eventlib.BaseEvent): @eventlib.handler def handle_stuff(self): pass core.HANDLER_REGISTRY.should.have.length_of(1) core.HANDLER_REGISTRY['tests.MyEvent'].should.be.equals([MyEvent.handle_stuff])
def test_log_when_debug_is_false(datetime, find_event, tasks): conf.DEBUG = False core.cleanup_handlers() datetime.now.return_value = 'tea time' eventlib.log('app.Event') tasks.process_task.delay.assert_called_once_with( 'app.Event', ejson.dumps({ '__ip_address__': '0.0.0.0', '__datetime__': 'tea time', }))
def test_process_data_clean_raise_errors(logger, find_event): core.cleanup_handlers() class MyEvent(eventlib.BaseEvent): def clean(self): raise exceptions.ValidationError('Owned!!11') data = {'name': 'Lincoln', 'answer': 42} find_event.return_value = MyEvent core.process.when.called_with( 'stuff', ejson.dumps(data) ).should.throw(exceptions.ValidationError)
def test_process(find_event): core.cleanup_handlers() handler = Mock() eventlib.handler('app.Event')(handler) handler2 = Mock() eventlib.handler('app.Event')(handler2) data = {'file': '/etc/passwd', 'server': 'yipster'} core.process('app.Event', ejson.dumps(data)) handler.assert_called_once_with(data) handler2.assert_called_once_with(data)
def test_log(find_event, process, datetime): conf.DEBUG = True core.cleanup_handlers() datetime.now.return_value = 'tea time' event_cls = Mock() find_event.return_value = event_cls data = {'name': 'Event System', 'code': 42} eventlib.log('app.Event', data) find_event.assert_called_once_with('app.Event') event_cls.assert_called_once_with('app.Event', data) event_cls.return_value.validate.assert_called_once() process.assert_called_once_with('app.Event', ejson.dumps(data))
def test_log(conf, find_event, process, datetime): conf.getsetting.return_value = True core.cleanup_handlers() datetime.now.return_value = 'tea time' event_cls = Mock() find_event.return_value = event_cls data = {'name': 'Event System', 'code': 42} eventlib.log('app.Event', data) find_event.assert_called_once_with('app.Event') event_cls.assert_called_once_with('app.Event', data) event_cls.return_value.validate.assert_called_once() process.assert_called_once_with('app.Event', ejson.dumps(data))
def test_process_data_clean(logger, find_event): core.cleanup_handlers() class MyEvent(eventlib.BaseEvent): def clean(self): raise exceptions.ValidationError('Owned!!11') data = {'name': 'Lincoln', 'answer': 42} find_event.return_value = MyEvent core.process('stuff', ejson.dumps(data)) logger.warning.assert_called_once_with( 'The event system just got an exception while cleaning data ' "for the event 'stuff'\n" "data: {\"answer\": 42, \"name\": \"Lincoln\"}\n" "exc: Owned!!11")
def test_find_handlers_with_mixed_objects_and_strings(find_event): core.cleanup_handlers() class MyEvent(eventlib.BaseEvent): @eventlib.handler def handle_stuff(self): pass find_event.return_value = MyEvent @eventlib.handler('stuff.MyEvent') def do_nothing(data): return len(data) core.find_handlers('stuff.MyEvent').should.be.equals( [MyEvent.handle_stuff, do_nothing])
def test_find_handlers_with_mixed_objects_and_strings(find_event): core.cleanup_handlers() class MyEvent(eventlib.BaseEvent): @eventlib.handler def handle_stuff(self): pass find_event.return_value = MyEvent @eventlib.handler('tests.MyEvent') def do_nothing(data): return len(data) core.find_handlers('tests.MyEvent').should.be.equals([ MyEvent.handle_stuff, do_nothing ])
def test_find_handlers(find_event): core.cleanup_handlers() @eventlib.handler('app.Event') def stuff(data): return 0 core.find_handlers('app.Event').should.be.equals([stuff]) @eventlib.handler('app.Event') def other_stuff(data): return 1 core.find_handlers('app.Event').should.be.equals([stuff, other_stuff]) @eventlib.handler('other_app.Event2') def more_stuff(data): return 2 core.find_handlers('app.Event').should.be.equals([stuff, other_stuff]) core.find_handlers('other_app.Event2').should.be.equals([more_stuff]) core.find_handlers({'not': 'a string'}).should.be.equals([]) @eventlib.handler('app.*') def even_more_stuff(data): return 2 core.find_handlers('app.Event').should.be.equals( [stuff, other_stuff, even_more_stuff]) @eventlib.external_handler('app2.Event') def still_more_stuff(data): return 3 @eventlib.external_handler('app2.*') def another_method(data): return 4 core.find_external_handlers('app2.Event').should.be.equals( [still_more_stuff, another_method]) core.find_handlers('dont.Exist').should.be.equal([]) core.find_external_handlers('dont.Exist').should.be.equal([])
def test_process_fails_gracefully(settings, logger, find_event): core.cleanup_handlers() settings.DEBUG = False handler_fail = Mock() handler_fail.side_effect = ValueError('P0wned!!!') eventlib.handler('myapp.CoolEvent')(handler_fail) handler = Mock() eventlib.handler('myapp.CoolEvent')(handler) data = {'a': 1} event = 'myapp.CoolEvent' core.process(event, ejson.dumps(data)) logger.warning.assert_called_once_with( 'One of the handlers for the event "myapp.CoolEvent" has ' 'failed with the following exception: P0wned!!!') handler.assert_called_once_with(data)
def test_find_handlers(find_event): core.cleanup_handlers() @eventlib.handler('app.Event') def stuff(data): return 0 core.find_handlers('app.Event').should.be.equals([stuff]) @eventlib.handler('app.Event') def other_stuff(data): return 1 core.find_handlers('app.Event').should.be.equals([stuff, other_stuff]) @eventlib.handler('app.Event2') def more_stuff(data): return 2 core.find_handlers('app.Event').should.be.equals([stuff, other_stuff]) core.find_handlers('app.Event2').should.be.equals([more_stuff]) core.find_handlers('dont.Exist').should.be.equal([])
def test_find_handlers(find_event): core.cleanup_handlers() @eventlib.handler('app.Event') def stuff(data): return 0 core.find_handlers('app.Event').should.be.equals([stuff]) @eventlib.handler('app.Event') def other_stuff(data): return 1 core.find_handlers('app.Event').should.be.equals([stuff, other_stuff]) @eventlib.handler('other_app.Event2') def more_stuff(data): return 2 core.find_handlers('app.Event').should.be.equals([stuff, other_stuff]) core.find_handlers('other_app.Event2').should.be.equals([more_stuff]) core.find_handlers({'not': 'a string'}).should.be.equals([]) @eventlib.handler('app.*') def even_more_stuff(data): return 2 core.find_handlers('app.Event').should.be.equals([stuff, other_stuff, even_more_stuff]) @eventlib.external_handler('app2.Event') def still_more_stuff(data): return 3 @eventlib.external_handler('app2.*') def another_method(data): return 4 core.find_external_handlers('app2.Event').should.be.equals([still_more_stuff, another_method]) core.find_handlers('dont.Exist').should.be.equal([]) core.find_external_handlers('dont.Exist').should.be.equal([])
def test_handler_registry_cleanup(): core.cleanup_handlers() core.HANDLER_REGISTRY.should.have.length_of(0) @eventlib.handler('stuff.Klass') def do_nothing(data): return -1 @eventlib.handler('stuff.Blah') def do_another_nothing(data): return 0 @eventlib.external_handler('stuff.Foobar') def do_more_nothing(data): return 0 @eventlib.external_handler('stuff.Foobaz') def do_a_lot_more_nothing(data): return 0 core.HANDLER_REGISTRY.should.have.length_of(2) core.HANDLER_REGISTRY['stuff.Klass'].should.be.equals([do_nothing]) core.HANDLER_REGISTRY['stuff.Blah'].should.be.equals([do_another_nothing]) core.EXTERNAL_HANDLER_REGISTRY.should.have.length_of(2) core.EXTERNAL_HANDLER_REGISTRY['stuff.Foobar'].should.be.equals( [do_more_nothing]) core.EXTERNAL_HANDLER_REGISTRY['stuff.Foobaz'].should.be.equals( [do_a_lot_more_nothing]) core.cleanup_handlers('stuff.Klass') dict(core.HANDLER_REGISTRY).should.be.equals({ 'stuff.Blah': [do_another_nothing], }) core.cleanup_handlers('stuff.Foobaz') dict(core.EXTERNAL_HANDLER_REGISTRY).should.be.equals({ 'stuff.Foobar': [do_more_nothing], }) core.cleanup_handlers() core.HANDLER_REGISTRY.should.have.length_of(0) core.EXTERNAL_HANDLER_REGISTRY.should.have.length_of(0)
def test_handler_registry_cleanup(): core.cleanup_handlers() core.HANDLER_REGISTRY.should.have.length_of(0) @eventlib.handler('stuff.Klass') def do_nothing(data): return -1 @eventlib.handler('stuff.Blah') def do_another_nothing(data): return 0 @eventlib.external_handler('stuff.Foobar') def do_more_nothing(data): return 0 @eventlib.external_handler('stuff.Foobaz') def do_a_lot_more_nothing(data): return 0 core.HANDLER_REGISTRY.should.have.length_of(2) core.HANDLER_REGISTRY['stuff.Klass'].should.be.equals([do_nothing]) core.HANDLER_REGISTRY['stuff.Blah'].should.be.equals([do_another_nothing]) core.EXTERNAL_HANDLER_REGISTRY.should.have.length_of(2) core.EXTERNAL_HANDLER_REGISTRY['stuff.Foobar'].should.be.equals([do_more_nothing]) core.EXTERNAL_HANDLER_REGISTRY['stuff.Foobaz'].should.be.equals([do_a_lot_more_nothing]) core.cleanup_handlers('stuff.Klass') dict(core.HANDLER_REGISTRY).should.be.equals({ 'stuff.Blah': [do_another_nothing], }) core.cleanup_handlers('stuff.Foobaz') dict(core.EXTERNAL_HANDLER_REGISTRY).should.be.equals({ 'stuff.Foobar': [do_more_nothing], }) core.cleanup_handlers() core.HANDLER_REGISTRY.should.have.length_of(0) core.EXTERNAL_HANDLER_REGISTRY.should.have.length_of(0)
def test_handler_registry_cleanup(): core.cleanup_handlers() core.HANDLER_REGISTRY.should.have.length_of(0) @eventlib.handler('stuff.Klass') def do_nothing(data): return -1 @eventlib.handler('stuff.Blah') def do_another_nothing(data): return 0 core.HANDLER_REGISTRY.should.have.length_of(2) core.HANDLER_REGISTRY['stuff.Klass'].should.be.equals([do_nothing]) core.HANDLER_REGISTRY['stuff.Blah'].should.be.equals([do_another_nothing]) core.cleanup_handlers('stuff.Klass') dict(core.HANDLER_REGISTRY).should.be.equals( {'stuff.Blah': [do_another_nothing]}) core.cleanup_handlers() core.HANDLER_REGISTRY.should.have.length_of(0)