예제 #1
0
def test_app__before_run__argument_no_callable__raises_AppError(
        invalid_callable_argument):
    handler = mock.Mock()

    app = App(handler)

    with pytest.raises(AppError) as e:
        app.before_run(invalid_callable_argument)
    assert f'{invalid_callable_argument} is not callable' == str(e.value)
예제 #2
0
def test_app__remove_client__client_is_not_registered__raises_AppError():
    handler = mock.Mock()

    app = App(handler)

    client = PubSubClient(broker_handler=mock.Mock(), sm_service=mock.Mock())

    with pytest.raises(AppError) as e:
        app.remove_client(client)
    assert f"PubSubClient {client} was not found" == str(e.value)
예제 #3
0
def test_app__remove_client__client_is_registered_and_removed_properly():
    handler = mock.Mock()

    app = App(handler)

    client = PubSubClient(broker_handler=mock.Mock(), sm_service=mock.Mock())

    app.clients.append(client)

    app.remove_client(client)

    assert [] == app.clients
예제 #4
0
def test_app__register_client__client_class_is_not_of_type_Client__raises_pubsubclienterror(
):
    handler = mock.Mock()

    app = App(handler)

    class FakeClient:
        pass

    with pytest.raises(PubSubClientError) as e:
        app.register_client('username', 'password', client_class=FakeClient)
    assert "client_class should be PubSubClient or should inherit from PubSubClient" == str(
        e.value)
예제 #5
0
def test_app__register_client__client_is_not_valid__raises_pubsubclienterror():
    handler = mock.Mock()

    app = App(handler)
    app.config = {'SUBSCRIPTION-MANAGER': {}}

    class TestClient(PubSubClient):
        def is_valid(self):
            return False

    TestClient.create = Mock(
        return_value=TestClient(broker_handler=Mock(), sm_service=Mock()))

    with pytest.raises(PubSubClientError) as e:
        app.register_client('username', 'password', client_class=TestClient)

    assert "User 'username' is not valid" == str(e.value)
예제 #6
0
def test_app_register_client__client_class_is_valid__client_is_properly_registered(
):
    handler = mock.Mock()

    app = App(handler)
    app.config = {'SUBSCRIPTION-MANAGER': {}}

    class CustomPubSubClient(PubSubClient):
        pass

    client = CustomPubSubClient(broker_handler=mock.Mock(),
                                sm_service=mock.Mock())

    CustomPubSubClient.create = mock.Mock(return_value=client)

    app.register_client('username', 'password', CustomPubSubClient)

    assert client in app.clients
예제 #7
0
def test_app__create_from_config():
    config = {
        'BROKER': {
            'host': 'host'
        },
    }

    with mock.patch('swim_pubsub.core.utils.yaml_file_to_dict',
                    return_value=config):
        app = App._create_from_config('config_file',
                                      broker_handler_class=BrokerHandler)

        assert isinstance(app._handler, BrokerHandler)
        assert config == app.config
예제 #8
0
def test_app__before_run__appends_callables():
    handler = mock.Mock()

    app = App(handler)

    @app.before_run
    def callable1():
        pass

    @app.before_run
    def callable2():
        pass

    action_names = [c.__name__ for c in app._before_run_actions]

    assert 2 == len(action_names)
    assert ['callable1', 'callable2'] == action_names
예제 #9
0
def test_app__run__before_run_actions_run_before_app_run(mock_protoncontainer):
    handler = mock.Mock()

    app = App(handler)

    callable1 = mock.Mock()
    callable2 = mock.Mock()

    app.before_run(callable1)
    app.before_run(callable2)

    app.run()

    assert callable1.called
    assert callable2.called