def test_with_tags():
    r = TopicsRegistry()
    t1 = Topic('test1', tags=['aaa'])
    t2 = Topic('test2', tags=['bbb', 'ccc'])
    t3 = Topic('test3', tags=['aaa', 'ccc', 'ddd'])
    r.register(t1)
    r.register(t2)
    r.register(t3)

    results = r.with_tags(['aaa'])
    assert len(results) == 2
    assert t1 in results
    assert t3 in results

    results = r.with_tags(['aaa'])
    assert len(results) == 2
    assert t1 in results
    assert t3 in results

    results = r.with_tags(['missing-tag'])
    assert len(results) == 0

    with pytest.raises(AssertionError):
        r.with_tags(None)
    with pytest.raises(AssertionError):
        r.with_tags('string')
    with pytest.raises(AssertionError):
        r.with_tags(123)
def test_remove_all():
    r = TopicsRegistry()
    t1 = Topic('test1')
    t2 = Topic('test2')
    r.register(t1)
    r.register(t2)
    assert len(r) == 2
    r.remove_all()
    assert len(r) == 0
def test_count():
    r = TopicsRegistry()
    t1 = Topic('test1')
    r.register(t1)
    assert r.count() == 1
    t2 = Topic('test2')
    t3 = Topic('test3')
    r.register(t2)
    r.register(t3)
    assert r.count() == 3
def test_remove():
    r = TopicsRegistry()
    t1 = Topic('test1')
    t2 = Topic('test2')
    r.register(t1)
    r.register(t2)
    assert len(r) == 2
    r.remove(t2)
    assert len(r) == 1
    assert t2 not in r
Beispiel #5
0
def test_constructor():
    t = Topic('test', description='this is a test topic', owner='me')
    assert t.id is not None
    assert len(t.eventtypes) == 0
    assert t.owner is not None
    assert len(t.tags) == 0
    assert t.timestamp is not None
    ets = [GenericEventType(), DataOperationEventType()]
    t = Topic('test', eventtypes=ets,
              description='this is a test topic', owner='me',
              tags=['x', 'y'])
    assert len(t.eventtypes) == 2
    assert len(t.tags) == 2
def test_register():
    r = TopicsRegistry()
    t = Topic('test')
    r.register(t)
    assert r.count() == 1

    # registering the same topic multiple times won't change it
    r.register(t)
    assert r.count() == 1

    # ... but a new one, will
    t2 = Topic('test2')
    r.register(t2)
    assert r.count() == 2
def test_magic_methods():
    r = TopicsRegistry()
    t1 = Topic('test1')
    r.register(t1)
    assert len(r) == 1

    assert t1 in r
    t2 = Topic('test2')
    assert t2 not in r

    r.register(Topic('test2'))
    r.register(Topic('test3'))
    for _ in r:
        pass
def test_with_name():
    r = TopicsRegistry()
    t1 = Topic('test1')
    t2 = Topic('test2')
    t3 = Topic('test3')
    r.register(t1)
    r.register(t2)
    r.register(t3)
    result = r.with_name('test3')
    assert result == t3

    result = r.with_name('missing-name')
    assert result is None

    with pytest.raises(AssertionError):
        r.with_name(None)
Beispiel #9
0
def test_constructor_failures():
    with pytest.raises(TypeError):
        Topic()
        pytest.fail()
    with pytest.raises(AssertionError):
        Topic(123)
        pytest.fail()
    with pytest.raises(AssertionError):
        Topic('test', tags='abc')
        pytest.fail()
    with pytest.raises(AssertionError):
        Topic('test', eventtypes='abc')
        pytest.fail()
    with pytest.raises(AssertionError):
        Topic('test', eventtypes=['abc'])
        pytest.fail()
def test_with_id():
    r = TopicsRegistry()
    t1 = Topic('test1')
    t2 = Topic('test2')
    t3 = Topic('test3')
    r.register(t1)
    r.register(t2)
    r.register(t3)
    id2 = t2.id
    result = r.with_id(id2)
    assert result == t2

    result = r.with_id('missing-id')
    assert result is None

    with pytest.raises(AssertionError):
        r.with_id(None)
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_of():
    r = TopicsRegistry()
    t1 = Topic('test1', owner='me')
    t2 = Topic('test2', owner='you')
    t3 = Topic('test3', owner='me')
    assert len(r.of('me')) == 0
    assert len(r.of('you')) == 0
    r.register(t1)
    r.register(t2)
    r.register(t3)
    my_topics = r.of('me')
    your_topics = r.of('you')
    assert len(my_topics) == 2
    assert len(your_topics) == 1
    assert t1 in my_topics
    assert t3 in my_topics
    assert t2 in your_topics

    with pytest.raises(AssertionError):
        r.of(None)
Beispiel #13
0
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
Beispiel #14
0
 def new(self, name, **kwargs):
     """Creates a new topic, adds it to the registry and returns it.
 
     Args:
         name (str): name of the new topic
         **kwargs: positional arguments for `Topic` instantiation
 
     Returns:
         :obj:`baroque.entities.topic.Topic`
 
     """
     topic = Topic(name, **kwargs)
     self.register(topic)
     return topic
Beispiel #15
0
 def new(self, name, eventtypes, **kwargs):
     """Creates a new topic, adds it to the registry and returns it.
 
     Args:
         name (str): name of the new topic
     eventtypes (collection): the :obj:`baroque.entities.eventtype.EventType` objects that characterize the new topic
         **kwargs: positional arguments for `Topic` instantiation
 
     Returns:
         :obj:`baroque.entities.topic.Topic`
 
     """
     topic = Topic(name, eventtypes, **kwargs)
     self.register(topic)
     return topic
def test_register_on_binding():
    # do register topics upon reactor binding
    cfg['topics']['register_on_binding'] = True
    brq = Baroque()
    brq.config = cfg
    t = Topic('test-topic')
    assert len(brq.topics) == 0
    brq.on_topic_run(t, ReactorFactory.stdout())
    assert len(brq.topics) == 1

    # do not register topics upon reactor binding: throw an exception
    cfg['topics']['register_on_binding'] = False
    brq = Baroque()
    brq.config = cfg
    with pytest.raises(UnregisteredTopicError):
        brq.on_topic_run(t, ReactorFactory.stdout())
def test_run():
    reg = TopicsRegistry()
    t = Topic('test-topic')
    r1 = ReactorFactory.stdout()

    # failures
    with pytest.raises(AssertionError):
        reg.on_topic_run(None, r1)
    with pytest.raises(AssertionError):
        reg.on_topic_run(123, r1)
    with pytest.raises(AssertionError):
        reg.on_topic_run(t, None)
    with pytest.raises(AssertionError):
        reg.on_topic_run(t, 123)

    # adding a reactor to a topic that is not yet registered is idempotent
    assert len(reg.topics) == 0
    reg.on_topic_run(t, r1)
    assert len(reg.topics) == 0

    # adding a reactor to a topic that is registered
    r1 = ReactorFactory.stdout()
    reg.register(t)
    reg.on_topic_run(t, r1)
    reactors = reg.topics[t]
    assert len(reactors) == 1
    assert r1 in reactors

    # adding another reactor on the same topic does not cause topic duplication
    r2 = ReactorFactory.stdout()
    reg.on_topic_run(t, r2)
    assert len(reg.topics) == 1
    reactors = reg.topics[t]
    assert len(reactors) == 2
    assert r2 in reactors

    # adding a reactor to an already registered topic
    reg = TopicsRegistry()
    reg.register(t)
    r3 = ReactorFactory.stdout()
    reg.on_topic_run(t, r3)
    assert len(reg.topics) == 1
    reactors = reg.topics[t]
    assert len(reactors) == 1
    assert r3 in reactors
Beispiel #18
0
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
Beispiel #19
0
def test_constructor_failures():
    with pytest.raises(TypeError):
        Topic()
        pytest.fail()
    with pytest.raises(AssertionError):
        Topic(123, [])
        pytest.fail()
    with pytest.raises(AssertionError):
        Topic('test', None)
        pytest.fail()
    with pytest.raises(AssertionError):
        Topic('test', 'abc')
        pytest.fail()
    with pytest.raises(AssertionError):
        Topic('test', ['abc'])
        pytest.fail()
    with pytest.raises(AssertionError):
        Topic('test', [], tags='abc')
        pytest.fail()
Beispiel #20
0
def test_print():
    print(Topic('test'))
Beispiel #21
0
def test_print():
    print(Topic('test', []))