def test_magic_methods(): bck = DictBackend() evt1 = Event(GenericEventType(), dict(foo='bar')) evt2 = Event(MetricEventType(), dict(bar='baz')) bck.create(evt1) bck.create(evt2) assert len(bck) == 2 assert evt1 in bck assert evt2 in bck for event_id in bck: assert isinstance(bck[event_id], Event) keys = bck.keys() assert len(keys) == 2 assert evt1.id in keys assert evt2.id in keys values = bck.values() assert len(values) == 2 assert evt1 in values assert evt2 in values bck.clear() assert len(bck) == 0
def test_remove_all(): reg = EventTypesRegistry() reg.register(GenericEventType()) reg.register(MetricEventType()) reg.register(DataOperationEventType()) assert reg.count() == 3 reg.remove_all() assert reg.count() == 0
def test_publish_on_topic(): reg = TopicsRegistry() t = Topic('test', eventtypes=[GenericEventType(), MetricEventType()]) evt = Event(MetricEventType()) # failures with pytest.raises(AssertionError): reg.publish_on_topic(None, t) with pytest.raises(AssertionError): reg.publish_on_topic(123, t) with pytest.raises(AssertionError): reg.publish_on_topic(evt, None) with pytest.raises(AssertionError): reg.publish_on_topic(evt, 123) # using a databox class to keep state and make assertions class Box: def __init__(self): self.called = False def mark_called(self): self.called = True # publish an event on a topic that doesn't have its eventtype registered t = Topic('aaa', eventtypes=[GenericEventType()]) evt = Event(MetricEventType()) box = Box() r = ReactorFactory.call_function(box, 'mark_called') reg.register(t) reg.on_topic_run(t, r) reg.publish_on_topic(evt, t) assert not box.called # publish an event on a topic that has its eventtype registered reg = TopicsRegistry() t = Topic('aaa', eventtypes=[MetricEventType()]) box = Box() r = ReactorFactory.call_function(box, 'mark_called') reg.register(t) reg.on_topic_run(t, r) evt = Event(MetricEventType()) reg.publish_on_topic(evt, t) assert box.called
def test_get_bag(): reg = ReactorsRegistry() et = MetricEventType() # with a concrete instance result = reg.get_bag(et) assert isinstance(result, ReactorsBag) reg.get_or_create_bag(et) result = reg.get_bag(MetricEventType) # with a type object assert isinstance(result, ReactorsBag) assert result == reg.registered_types[type(et)]
def test_add(): bag = EventTypesBag() assert len(bag.types) == 0 ets = [GenericEventType(), MetricEventType()] bag.add(ets) assert len(bag.types) == 2 bag.add([GenericEventType()]) assert len(bag.types) == 2 bag.add([DataOperationEventType()]) assert len(bag.types) == 3
def test_persist(): # persist events brq = Baroque() cfg['eventtypes']['ignore_unregistered'] = True cfg['eventtypes']['pre_registered'] = [ 'baroque.defaults.eventtypes.GenericEventType' ] cfg['topics']['register_on_binding'] = True cfg['events']['persist'] = True brq.config = cfg evt1 = Event(GenericEventType()) assert len(brq._persistance_backend) == 0 brq.publish(evt1) assert len(brq._persistance_backend) == 1 assert evt1 in brq._persistance_backend evt2 = Event(GenericEventType()) t = brq.topics.new('test-topic', eventtypes=[MetricEventType(), GenericEventType()]) assert len(brq._persistance_backend) == 1 brq.publish_on_topic(evt2, t) assert len(brq._persistance_backend) == 2 assert evt2 in brq._persistance_backend # do not persist events brq = Baroque() cfg['events']['persist'] = False brq.config = cfg evt3 = Event(GenericEventType()) assert len(brq._persistance_backend) == 0 brq.publish(evt3) assert len(brq._persistance_backend) == 0 evt4 = Event(GenericEventType()) t = brq.topics.new('test-topic', eventtypes=[MetricEventType(), GenericEventType()]) assert len(brq._persistance_backend) == 0 brq.publish_on_topic(evt4, t) assert len(brq._persistance_backend) == 0
def test_register(): reg = EventTypesRegistry() assert reg.count() == 0 reg.register(GenericEventType()) # with a concrete instance assert reg.count() == 1 reg.register(GenericEventType) # with a type object assert reg.count() == 1 reg.register(MetricEventType()) assert reg.count() == 2 with pytest.raises(AssertionError): reg.register('not-a-reactor') pytest.fail()
def test_on_topic_run(): brq = Baroque() t1 = Topic('test-topic1', eventtypes=[MetricEventType(), GenericEventType()]) assert len(brq.topics.topics) == 0 brq.on_topic_run(t1, ReactorFactory.stdout()) assert len(brq.topics.topics) == 1 assert len(brq.topics.topics[t1]) == 1 # one more reactor on the same topic brq.on_topic_run(t1, ReactorFactory.stdout()) assert len(brq.topics.topics) == 1 assert len(brq.topics.topics[t1]) == 2 # let's register another topic t2 = Topic('test-topic2', eventtypes=[MetricEventType()]) brq.on_topic_run(t2, ReactorFactory.stdout()) assert len(brq.topics.topics) == 2 assert len(brq.topics.topics[t1]) == 2 assert len(brq.topics.topics[t2]) == 1
def test_get_or_create_bag_when_bag_already_exists(): reg = ReactorsRegistry() # with concrete instances et1 = MetricEventType() et2 = GenericEventType() reg.get_or_create_bag(et1) assert len(reg.registered_types) == 1 assert type(et1) in reg.registered_types assert type(et2) not in reg.registered_types result = reg.get_or_create_bag(et2) assert type(et2) in reg.registered_types assert len(reg.registered_types) == 2 assert isinstance(result, ReactorsBag) assert result == reg.registered_types[type(et2)]
def test_publish_on_topic(): brq = Baroque() t = brq.topics.new('test-topic1', eventtypes=[MetricEventType(), GenericEventType()]) evt = Event(MetricEventType()) # trying to publish events to an unregistered topic with pytest.raises(UnregisteredTopicError): brq.publish_on_topic(evt, Topic('unregistered')) pytest.fail() # using a box to keep state and make assertions box = Box() brq.on_topic_run(t, ReactorFactory.call_function(box, 'mark_called')) assert not box.called assert brq.events.count_all() == 0 brq.publish_on_topic(evt, t) assert box.called assert brq.events.count_all() == 1 assert evt.status == EventStatus.PUBLISHED
def test_remove_all(): reg = ReactorsRegistry() et1 = MetricEventType() et2 = GenericEventType() et3 = DataOperationEventType() reg.get_or_create_bag(et1) reg.get_or_create_bag(et2) reg.get_or_create_bag(et3) reg.get_jolly_bag().run(ReactorFactory.stdout()) assert len(reg.registered_types) == 3 assert len(reg.jolly_bag) == 1 reg.remove_all() assert len(reg.registered_types) == 0 assert len(reg.jolly_bag) == 0
def test_new(): r = TopicsRegistry() topic = r.new('test', eventtypes=[GenericEventType(), MetricEventType()], description='this is a test topic', owner='me', tags=['aaa', 'bbb', 'ccc']) assert isinstance(topic, Topic) assert topic.name == 'test' assert isinstance(topic.eventtypes, EventTypesBag) assert topic.description == 'this is a test topic' assert topic.owner == 'me' assert 'aaa' in topic.tags assert 'bbb' in topic.tags assert 'ccc' in topic.tags assert topic in r
def test_remove(): reg = EventTypesRegistry() reg.register(GenericEventType()) reg.register(MetricEventType()) reg.register(DataOperationEventType()) assert reg.count() == 3 reg.remove(DataOperationEventType()) # with a concrete instance assert reg.count() == 2 assert DataOperationEventType() not in reg.registered_types reg.remove(GenericEventType) # with a type object assert reg.count() == 1 assert DataOperationEventType() not in reg.registered_types with pytest.raises(AssertionError): reg.remove(123) pytest.fail()
def test_delete(): bck = DictBackend() evt1 = Event(GenericEventType(), dict(foo='bar')) evt2 = Event(MetricEventType(), dict(bar='baz')) bck.create(evt1) bck.create(evt2) assert len(bck._db) == 2 # deleting nothing bck.delete(None) assert len(bck._db) == 2 # deleting an actual event bck.delete(evt1.id) assert len(bck._db) == 1 result = bck.read(evt2.id) assert result == evt2
def test_events_counting(): eventtype1 = GenericEventType() eventtype2 =MetricEventType() c = EventCounter() assert c.count_all() == 0 assert c.count(eventtype1) == 0 assert c.count(eventtype2) == 0 c.increment_counting(Event(eventtype1)) assert c.count_all() == 1 assert c.count(eventtype1) == 1 assert c.count(eventtype2) == 0 c.increment_counting(Event(eventtype2)) assert c.count_all() == 2 assert c.count(eventtype1) == 1 assert c.count(eventtype2) == 1
def test_validate_schema(): cfg['eventtypes']['pre_registered'] = [ 'baroque.defaults.eventtypes.MetricEventType' ] brq = Baroque() brq.config = cfg et = MetricEventType() event = Event(et, payload=dict(a=1, b=2)) # do not validate cfg['events']['validate_schema'] = False brq.config = cfg try: brq.publish(event) except InvalidEventSchemaError: pytest.fail() # do validate cfg['events']['validate_schema'] = True with pytest.raises(InvalidEventSchemaError): brq.publish(event)
def test_validate_event_schema(): brq = Baroque() et = MetricEventType() event_valid = Event(et, payload={ 'metric': 'temperature', 'value': 56.7793, 'timestamp': '2017-02-15T13:56:09Z' }) event_invalid = Event(et, payload={'metric': 'temperature'}) # Publish invalid event with pytest.raises(InvalidEventSchemaError): brq.publish(event_invalid) pytest.fail() # Publish valid event try: brq.publish(event_valid) except InvalidEventSchemaError: pytest.fail()
def test_to(): reg = ReactorsRegistry() et = MetricEventType() reg.get_or_create_bag(et) result = reg.to(et) assert result == reg.get_bag(et)
def test_magic_contains(): reg = EventTypesRegistry() reg.register(GenericEventType()) assert GenericEventType() in reg assert MetricEventType() not in reg