async def test_rpc_amqp_client_returns_an_error(event_loop):
    app = Application(config=FakeConfig(), loop=event_loop)
    register_worker = FakeRegisterMicroserviceWorker(app)
    extension = AmqpExtension(app)
    extension.register_worker(register_worker)

    await extension.init(event_loop)

    client = RpcAmqpClient(app=app,
                           routing_key=REQUEST_QUEUE,
                           request_exchange=REQUEST_EXCHANGE,
                           response_queue='',
                           response_exchange=RESPONSE_EXCHANGE_NAME)
    response = await client.send(payload={})

    assert Response.ERROR_FIELD_NAME in response.keys()
    assert Response.ERROR_TYPE_FIELD_NAME in response[
        Response.ERROR_FIELD_NAME].keys()
    assert response[Response.ERROR_FIELD_NAME][
        Response.ERROR_TYPE_FIELD_NAME] == VALIDATION_ERROR  # NOQA

    assert Response.ERROR_DETAILS_FIELD_NAME in response[
        Response.ERROR_FIELD_NAME].keys()
    assert response[Response.ERROR_FIELD_NAME][
        Response.ERROR_DETAILS_FIELD_NAME] == VALIDATION_ERROR_DECR  # NOQA

    assert Response.EVENT_FIELD_NAME in response.keys()
    assert response[Response.EVENT_FIELD_NAME] is None

    await extension.deinit(event_loop)
async def test_rpc_amqp_client_returns_ok_with_custom_event_loop(event_loop):
    app = Application(config=FakeConfig(), loop=event_loop)
    register_worker = FakeRegisterMicroserviceWorker(app)
    extension = AmqpExtension(app)
    extension.register_worker(register_worker)

    await extension.init(event_loop)

    client = RpcAmqpClient(app=app,
                           routing_key=REQUEST_QUEUE,
                           request_exchange=REQUEST_EXCHANGE,
                           response_queue='',
                           response_exchange=RESPONSE_EXCHANGE_NAME,
                           loop=event_loop)
    response = await client.send(payload={
        'name': 'microservice',
        'version': '1.0.0'
    })

    assert Response.CONTENT_FIELD_NAME in response.keys()
    assert response[Response.CONTENT_FIELD_NAME] == 'OK'

    assert Response.EVENT_FIELD_NAME in response.keys()
    assert response[Response.EVENT_FIELD_NAME] is None

    await extension.deinit(event_loop)
def test_constructor_with_init():
    app = Application()
    extension = CustomExtension(app)

    assert 'extensions' in app.__dict__
    assert isinstance(app.extensions, dict)
    assert len(app.extensions.keys()) == 1
    assert extension.extension_name in app.extensions.keys()
    assert app.extensions[extension.extension_name] == extension
Exemple #4
0
async def test_extension_without_workers(event_loop):
    app = Application()
    extension = AmqpExtension(app)

    await extension.init(event_loop)
    assert 'extensions' in app.__dict__
    assert isinstance(app.extensions, dict)
    assert len(app.extensions.keys()) == 1
    assert extension.extension_name in app.extensions.keys()
    assert app.extensions[extension.extension_name] == extension
    assert len(extension.active_tasks) == 0

    await asyncio.sleep(0.1)

    await extension.deinit(event_loop)
    assert 'extensions' in app.__dict__
    assert extension.extension_name not in app.extensions.keys()
    assert getattr(app, AmqpExtension.app_attribute) is None
    assert len(extension.active_tasks) == 0
async def test_microservice_has_been_registered_successfully(event_loop):
    app = Application(config=FakeConfig(), loop=event_loop)
    register_worker = FakeRegisterMicroserviceWorker(app)
    extension = AmqpExtension(app)
    extension.register_worker(register_worker)

    await extension.init(event_loop)

    instance = BaseRegisterWorker(app)
    instance.REQUEST_QUEUE_NAME = REQUEST_QUEUE
    instance.REQUEST_EXCHANGE_NAME = REQUEST_EXCHANGE
    instance.RESPONSE_EXCHANGE_NAME = RESPONSE_EXCHANGE_NAME

    with mock.patch(
            target=
            'sage_utils.amqp.workers.BaseRegisterWorker.get_microservice_data',
            side_effect=valid_microservice_data):
        result = await instance.run()
        assert result is None

    await extension.deinit(event_loop)
Exemple #6
0
async def test_extension_init_with_workers(event_loop):
    app = Application(config=FakeConfig())
    amqp_worker = CustomWorker(app)
    extension = AmqpExtension(app)
    extension.register_worker(amqp_worker)

    await extension.init(event_loop)
    assert 'extensions' in app.__dict__
    assert isinstance(app.extensions, dict)
    assert len(app.extensions.keys()) == 1
    assert extension.extension_name in app.extensions.keys()
    assert app.extensions[extension.extension_name] == extension
    assert len(extension.active_tasks) == 1

    await asyncio.sleep(0.1)

    await extension.deinit(event_loop)
    assert 'extensions' in app.__dict__
    assert extension.extension_name not in app.extensions.keys()
    assert getattr(app, AmqpExtension.app_attribute) is None
    assert len(extension.active_tasks) == 1
async def test_register_worker_raises_an_error(event_loop):
    app = Application(config=FakeConfig(), loop=event_loop)
    register_worker = FakeRegisterMicroserviceWorker(app)
    extension = AmqpExtension(app)
    extension.register_worker(register_worker)

    await extension.init(event_loop)

    instance = BaseRegisterWorker(app)
    instance.REQUEST_QUEUE_NAME = 'invalid.queue.name'
    instance.REQUEST_EXCHANGE_NAME = 'some.request.exchange'
    instance.RESPONSE_EXCHANGE_NAME = 'some.response.exchange'
    instance.RETRY_TIMEOUT = 0.1
    instance.MAX_RETRIES = 1

    with mock.patch(
            target=
            'sage_utils.amqp.workers.BaseRegisterWorker.get_microservice_data',
            side_effect=valid_microservice_data):
        with pytest.raises(ConnectionError):
            await instance.run()

    await extension.deinit(event_loop)
def test_default_implementation_raises_an_error_when_getting_data():
    with pytest.raises(NotImplementedError):
        app = Application(config=FakeConfig())
        instance = BaseRegisterWorker(app)
        instance.get_microservice_data(app)
async def test_deinit_returns_none_by_default(event_loop):
    app = Application()
    extension = CustomExtension(app)

    result = await extension.deinit(event_loop)
    assert result is None
def test_get_from_app_config_returns_none():
    app = Application(config=FakeConfig())
    extension = CustomExtension(app)

    assert extension.get_from_app_config(app, 'UNKNOWN_KEY') is None
def test_get_from_app_config_returns_value():
    app = Application(config=FakeConfig())
    extension = CustomExtension(app)

    assert extension.get_from_app_config(app, 'key') == 'value'