Ejemplo n.º 1
0
def test_filter_policy_by_concept():
    class CommandMatch(ApplicationCommand):
        __concept__: str = 'ConceptMatch'
        __version__: int = 1
    class CommandNotMatch(ApplicationCommand):
        __concept__: str = 'ConceptNotMatch'
        __version__: int = 1

    command_match = CommandMatch.stamp()()
    command_not_match = CommandNotMatch.stamp()()

    subscriber = BasicSubscriber()

    bus = Bus()
    bus.attach(
        FilterPolicy(
            concepts=[CommandMatch.__concept__],
            targets=[subscriber]   
        )
    )
    
    bus.publish(command_match)
    bus.publish(command_not_match)

    assert len(subscriber) == 1
    assert subscriber[0] == command_match
Ejemplo n.º 2
0
def test_filter_policy_by_context():
    context = "ctx"

    command_match = ApplicationCommand.stamp()(
        __context__ = context,
        __version__ = 1
    )
    command_not_match = ApplicationCommand.stamp()(
        __context__ = None,
        __version__ = 1
    )

    subscriber = BasicSubscriber()

    bus = Bus()
    bus.attach(
        FilterPolicy(
            contexts=[context],
            targets=[subscriber]   
        )
    )
    
    bus.publish(command_match)
    bus.publish(command_not_match)


    assert len(subscriber) == 1
    assert subscriber[0] == command_match
Ejemplo n.º 3
0
def test_bus():
    class Subscriber(ISubscriber):
        def __route__(self, message: Message):
            self.proof_of_work(message)

        def proof_of_work(self, *args, **kwargs):
            pass

    subscriber = Subscriber()
    subscriber.proof_of_work = mock.Mock()

    command = ApplicationCommand(__timestamp__ = 0.0, __version__ = 1)

    bus = Bus()
    bus.attach(subscriber)
    bus.publish(command)

    subscriber.proof_of_work.assert_called_with(command)
Ejemplo n.º 4
0
def test_filter_policy_by_type():
    message_match = ApplicationCommand.stamp()(__version__=1)
    message_not_match = ApplicationQuery.stamp()(__version__=1)

    subscriber = BasicSubscriber()

    bus = Bus()
    bus.attach(
        FilterPolicy(
            types=[ApplicationCommand],
            targets=[subscriber]   
        )
    )
    
    bus.publish(message_match)
    bus.publish(message_not_match)

    assert len(subscriber) == 1
    assert subscriber[0] == message_match