def test_emit(self): emitter = temitter() event, expect = teventr_expect() result = emitter.emit(event) assert isinstance(result, EmittingEventContext) assert result.name == expect['name'] assert result.enter_event.name == 'enter' assert result.exit_event.name == 'exit' assert id(result.enter_event) != id(result.exit_event) assert len(emitter.transport.adapter) == 1 got = Event.from_json(emitter.transport.adapter[0]) want = (emitter.event_stack | Event(**expect)) assert got == want
def chk(expect): event = Event.from_json(emitter.transport.adapter.pop()) assert event.system == 'test.pyemit' assert event.tid == tid assert event.component == 'event_stack' assert event.operation == 'testing' assert event.name == '{}.{}'.format('base.one.two.three', expect) assert event.valid
def test__enter__as_callable(self): emitter = temitter() with emitter('called'): emitter('hello') for expect in ['called', 'called.enter', 'called.hello', 'called.exit']: e = emitter.transport.adapter.pop(0) e = Event.from_json(e) assert e.name == '{}.{}'.format('base.one.two.three', expect) assert len(emitter.transport.adapter) == 0
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 tevent_expect(**kwargs): expect = dict( tid='test.tid', # time=DEFAULT_DATETIME, system=SYSTEM, component=COMPONENT, operation=OPERATION, name='test.event') expect.update(**kwargs) evt = Event(**expect) return evt, expect
def tevent_stack(cls=EventStack): event_stack = cls() event_stack.append(Event(system='test.pyemit', tid='test.event_stack_tid')) event_stack.append(Event(component='event_stack')) event_stack.append(Event(operation='testing')) event_stack.append(Event(name='base')) event_stack.append(Event(name='one')) event_stack.append(Event(name='two')) event_stack.append(Event(name='three')) return event_stack
def test__enter__(self): emitter = temitter() with emitter: emitter('hello') assert len(emitter.transport.adapter) == 3 for expect in ['enter', 'hello', 'exit']: e = emitter.transport.adapter.pop(0) e = Event.from_json(e) assert e.name == '{}.{}'.format('base.one.two.three', expect) assert len(emitter.transport.adapter) == 0
def test_ping(self): conf.debug = True emitter = temitter() result = emitter.ping() assert len(emitter.transport.adapter) == 3 for expect in ['open', 'ping', 'close']: e = emitter.transport.adapter.pop(0) e = Event.from_json(e) assert e.name == expect assert e.tid == result assert e.system == 'test.pyemit' assert e.component == 'emitter' assert e.operation == 'ping'
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_defaults_operation(self): event_defaults = Event(operation='test_defaults_operation_getter') emitter = Emitter(defaults=event_defaults) assert event_defaults.operation == 'test_defaults_operation_getter' assert emitter.operation == 'test_defaults_operation_getter' assert event_defaults.operation == emitter.operation
def test_defaults_component(self): event_defaults = Event(component='test_component_getter') emitter = Emitter(defaults=event_defaults) assert event_defaults.component == 'test_component_getter' assert emitter.component == 'test_component_getter' assert event_defaults.component == emitter.component
def test_defaults_system(self): event_defaults = Event(system='test_defaults_system_getter') emitter = Emitter(defaults=event_defaults) assert event_defaults.system == 'test_defaults_system_getter' assert emitter.system == 'test_defaults_system_getter' assert event_defaults.system == emitter.system
def test__init__defaults(self): defaults = Event(tid='test__init__defaults') emitter = Emitter(defaults=defaults) assert emitter.top == defaults assert emitter.top.tid == 'test__init__defaults'