def test_operation(self): emitter = Emitter() assert emitter.operation == '' val = 'test_set_operation' emitter.operation = val emitter.top.operation = val assert emitter.operation == val
def test_fields(self): emitter = Emitter() assert not len(emitter.fields) val = {'f1': 'one', 'f2': 'two'} emitter.fields = val assert emitter.fields == val assert emitter.top.fields == val
def test_component(self): emitter = Emitter() assert emitter.component == '' val = 'test_set_component' emitter.component = val emitter.top.component = val assert emitter.component == val
def test_tid(self): emitter = Emitter() assert emitter.tid == '' val = 'test_set_tid' emitter.tid = val assert emitter.tid == val assert emitter.top.tid == val
def test_data(self): emitter = Emitter() assert not len(emitter.data) val = {'f1': 'one', 'f2': 'two'} emitter.data = val assert emitter.data == val assert emitter.top.data == val
def test_system(self): emitter = Emitter() assert emitter.system == '' val = 'test_set_system' emitter.system = val emitter.top.system = val assert emitter.system == val
def test_replay(self): emitter = Emitter() assert emitter.replay == '' val = 'test_set_replay' emitter.replay = val assert emitter.replay == val assert emitter.top.replay == val
def test_open(self): emitter = Emitter() result = emitter.open('test_open') assert isinstance(result, EmittingEventContext) assert result.name == 'test_open' assert result.enter_event.name == 'open' assert result.exit_event.name == 'close' assert id(result.enter_event) != id(result.exit_event)
def test_enter(self): emitter = Emitter() result = emitter.enter('test_enter') assert isinstance(result, EmittingEventContext) assert result.name == 'test_enter' assert result.enter_event.name == 'enter' assert result.exit_event.name == 'exit' assert id(result.enter_event) != id(result.exit_event)
def test_time(self): emitter = Emitter() assert isinstance(emitter.time, datetime) val = datetime.utcnow() emitter.time = val assert emitter.time == val assert emitter.top.time == val
def test_name(self): emitter = Emitter() assert emitter.name == '' val = 'test_set_name' emitter.name = val emitter.top.name = val assert emitter.name == val
def test_tags(self): emitter = Emitter() assert not len(emitter.tags) vals = ['tag_1', 'tag_2', 'tag_3'] emitter.tags = vals for val in vals: assert val in emitter.tags assert val in emitter.top.tags
def test_instance_defaults(self): assert Emitter().event_stack_class == event.EventStack assert Emitter().event_class == event.Event assert Emitter().adapter_class == adapters.Adapter assert Emitter().transport_class == transports.Transport assert Emitter.event_stack_class == event.EventStack assert Emitter.event_class == event.Event assert Emitter.adapter_class == adapters.Adapter assert Emitter.transport_class == transports.Transport
def test_emit_throws(self): restore = conf.debug try: conf.debug = True emitter = Emitter() with pytest.raises(ValueError) as excinfo: emitter.emit('foo') assert '`operation` must not be empty' == str(excinfo.value) finally: conf.debug = restore
def test_setters_are_event_top(self): event_stack = tevent_stack() emitter = Emitter(event_stack=event_stack) emitter.system = 'test_setters_are_event_top' assert emitter.system == 'test_setters_are_event_top' assert emitter.event_stack.top.system == 'test_setters_are_event_top' assert emitter.event_stack.bot.system == 'test.pyemit' top = emitter.event_stack.pop() assert top.system == 'test_setters_are_event_top' assert emitter.system == '' assert emitter.event_stack.top.system == '' assert emitter.event_stack.bot.system == 'test.pyemit'
def test__init__event_class(self): class TClass(event.Event): pass emitter = Emitter(event_class=TClass) assert Emitter.event_class == event.Event assert emitter.event_class == TClass assert isinstance(emitter.event_stack.top, TClass)
def test__init__adapter_class(self): class TClass(adapters.StdoutAdapter): pass emitter = Emitter(adapter_class=TClass) assert Emitter.adapter_class == adapters.Adapter assert emitter.adapter_class == TClass assert isinstance(emitter.transport.adapter, TClass)
def test__init__transport_class(self): class TClass(transports.Transport): pass emitter = Emitter(transport_class=TClass) assert Emitter.transport_class == transports.Transport assert emitter.transport_class == TClass assert isinstance(emitter.transport, TClass)
def test__init__event_stack_class(self): class TClass(event.EventStack): pass emitter = Emitter(event_stack_class=TClass) assert Emitter.event_stack_class == event.EventStack assert emitter.event_stack_class == TClass
def event_from_callback(cls, event, **kwargs): events = [] def cb(message): events.append(message) adapter = adapters.ListAdapter() transport = transports.Transport( adapter, worker_class=transports.Worker) emitter = Emitter(transport=transport, callbacks=[cb], **kwargs) emitter.emit(event) assert len(events) == 1 event = events.pop() assert isinstance(event, Event) return event
def test__init__kwargs(self): emitter = Emitter( tid='t_tid', system='t_system', component='t_component', operation='t_operation') assert emitter.top.tid == 't_tid' assert emitter.top.system == 't_system' assert emitter.top.component == 't_component' assert emitter.top.operation == 't_operation'
def test__init__event_stack_and_defaults_and_kwargs(self): system = 'test__init__event_stack_and_defaults_and_kwargs_system' component = 'test__init__event_stack_and_defaults_and_kwargs_component' event_stack = event.EventStack() defaults = Event(system=system) emitter = Emitter(event_stack=event_stack, defaults=defaults, component=component) assert emitter.event_stack == event_stack assert emitter.event_stack.top.system == system assert emitter.event_stack.top.system == system assert emitter.event_stack.top.component == component
def temitter(**kwargs): if not ('adapter' in kwargs): kwargs['adapter'] = ListAdapter() if not ('transport' in kwargs): if not ('worker_class' in kwargs): kwargs['worker_class'] = Worker kwargs['transport'] = Transport( kwargs['adapter'], worker_class=kwargs['worker_class']) del kwargs['adapter'] del kwargs['worker_class'] if not ('event_stack' in kwargs): kwargs['event_stack'] = tevent_stack() return Emitter(**kwargs)
def test__init__transport_and_adapter(self): restore = conf.debug try: conf.debug = False adapter = adapters.StdoutAdapter() adapter_kwarg = adapters.StdoutAdapter() transport = transports.Transport(adapter) emitter = Emitter(adapter=adapter_kwarg, transport=transport) assert emitter.transport == transport assert emitter.transport.adapter == adapter finally: conf.debug = restore
def test__enter__without_valid_ctx(self): restore = conf.debug try: conf.debug = True emitter = Emitter() with pytest.raises(ValueError) as excinfo: with emitter: pass assert '`operation` must not be empty' == str(excinfo.value) finally: conf.debug = restore
def test__init__kwargs_and_named(self): event_stack = event.EventStack() emitter = Emitter( event_stack=event_stack, tid='t_tid', system='t_system', component='t_component', operation='t_operation') assert emitter.top.tid == 't_tid' assert emitter.top.system == 't_system' assert emitter.top.component == 't_component' assert emitter.top.operation == 't_operation' assert emitter.event_stack == event_stack
def test_defaults(self): event_stack = tevent_stack() emitter = Emitter(event_stack=event_stack) event = Event(system='test_defaults') assert event.system == 'test_defaults' assert emitter.system == '' assert emitter.top.system == '' assert emitter.to_event.system == 'test.pyemit' # Defaults should set the values of the top most ctx emitter.defaults(event) assert event.system == 'test_defaults' assert emitter.system == 'test_defaults' assert emitter.top.system == 'test_defaults' assert emitter.to_event.system == 'test_defaults' event_stack.pop() assert event.system == 'test_defaults' assert emitter.system == '' assert emitter.top.system == '' assert emitter.to_event.system == 'test.pyemit'
def test__init__transport_and_adapter_debug(self): restore = conf.debug try: conf.debug = True adapter = adapters.StdoutAdapter() adapter_kwarg = adapters.StdoutAdapter() transport = transports.Transport(adapter) expect_msg = '`adapter` argument is discarded when `transport`.' expect_msg += ' Disable debug mode to remove this assertion.' with pytest.raises(AssertionError) as excinfo: Emitter(adapter=adapter_kwarg, transport=transport) assert expect_msg == str(excinfo.value) finally: conf.debug = restore
def test_callbacks(self): events = [] def cb(message): events.append(message) adapter = adapters.ListAdapter() transport = transports.Transport(adapter) emitter = Emitter(transport=transport, callbacks=[cb]) event_sent = tevent() emitter(event_sent) assert len(events) == 1 event = events.pop() assert isinstance(event, Event) assert event_sent == event
def test__init__transport(self): adapter = adapters.StdoutAdapter() transport = transports.Transport(adapter) emitter = Emitter(transport=transport) assert emitter.transport == transport assert emitter.transport.adapter == adapter