Exemple #1
0
    async def test_app_events(self):
        app = Application()

        async def on_finalize():
            pass

        app.on("finalize", on_finalize)
        assert len(app._event_handlers["finalize"]) == 1
Exemple #2
0
    async def test_app_finalize_event(self):
        app = Application()

        class CallTracker:
            def __init__(self):
                self.called = False

            async def on_finalize(self):
                self.called = True

        tracker = CallTracker()
        app.on("finalize", tracker.on_finalize)
        await app.finalize()

        assert tracker.called is True
Exemple #3
0
    def test_mount_router(self):
        app = Application()

        router = kafkaesk.Router()

        @router.schema("Foo", streams=["foo.bar"])
        class Foo(pydantic.BaseModel):
            bar: str

        @router.subscribe("foo.bar", group="test_group")
        async def consume(data: Foo, schema, record):
            ...

        app.mount(router)

        assert app.subscriptions == router.subscriptions
        assert app.schemas == router.schemas
        assert app.event_handlers == router.event_handlers
Exemple #4
0
async def test_record_metric_on_publish():
    """
    this test is acting funny on github action...
    """
    with patch("kafkaesk.app.PUBLISHED_MESSAGES") as published_metric, patch(
            "kafkaesk.app.PUBLISHED_MESSAGES_TIME"
    ) as published_metric_time, patch(
            "kafkaesk.metrics.PUBLISH_MESSAGES") as publish_metric, patch(
                "kafkaesk.metrics.PUBLISH_MESSAGES_TIME"
            ) as publish_metric_time:
        app = Application()

        async def _fake_publish(*args, **kwargs):
            async def _publish():
                return record_factory()

            return asyncio.create_task(_publish())

        producer = AsyncMock()
        producer.send.side_effect = _fake_publish
        app._get_producer = AsyncMock(return_value=producer)
        app._topic_mng = MagicMock()
        app._topic_mng.get_topic_id.return_value = "foobar"

        await (await app.raw_publish("foo", b"data"))

        published_metric.labels.assert_called_with(stream_id="foobar",
                                                   partition=0,
                                                   error="none")
        published_metric.labels(stream_id="foobar", partition=0,
                                error="none").inc.assert_called_once()
        published_metric_time.labels.assert_called_with(stream_id="foobar")
        published_metric_time.labels(
            stream_id="foobar").observe.assert_called_once()

        publish_metric.labels.assert_called_with(stream_id="foobar",
                                                 error="none")
        publish_metric.labels(stream_id="foobar",
                              error="none").inc.assert_called_once()
        publish_metric_time.labels.assert_called_with(stream_id="foobar")
        publish_metric_time.labels(
            stream_id="foobar").observe.assert_called_once()
Exemple #5
0
async def test_record_metric_error():
    """
    this test is acting funny on github action...
    """
    with patch("kafkaesk.metrics.PUBLISH_MESSAGES") as publish_metric, patch(
            "kafkaesk.metrics.PUBLISH_MESSAGES_TIME") as publish_metric_time:
        app = Application()

        producer = AsyncMock()
        producer.send.side_effect = Exception
        app._get_producer = AsyncMock(return_value=producer)
        app._topic_mng = MagicMock()
        app._topic_mng.get_topic_id.return_value = "foobar"

        with pytest.raises(Exception):
            await app.raw_publish("foo", b"data")

        publish_metric.labels.assert_called_with(stream_id="foobar",
                                                 error="exception")
        publish_metric.labels(stream_id="foobar",
                              error="none").inc.assert_called_once()
        publish_metric_time.labels.assert_called_with(stream_id="foobar")
        publish_metric_time.labels(
            stream_id="foobar").observe.assert_called_once()
Exemple #6
0
def test_swallows_schema_conflict():
    app = Application()
    handler.PydanticKafkaeskHandler(app, "stream", MagicMock())
    handler.PydanticKafkaeskHandler(app, "stream", MagicMock())
Exemple #7
0
        tracer._scope_manager = ContextVarsScopeManager()

        tracer.scope_manager.activate("foobar", True)

        with patch.object(tracer, "inject") as mock:
            await app.raw_publish("foobar", b"foobar")
            mock.assert_called_once()


class TestSchemaRegistration:
    def test_schema_registration_repr(self):
        reg = SchemaRegistration(id="id", version=1, model=None)
        assert repr(reg) == "<SchemaRegistration id, version: 1 >"


test_app = Application()


def app_callable():
    return test_app


class TestRun:
    def test_run(self):
        rapp = AsyncMock()
        with patch("kafkaesk.app.run_app", rapp), patch("kafkaesk.app.cli_parser") as cli_parser:
            args = Mock()
            args.app = "tests.unit.test_app:test_app"
            args.kafka_servers = "foo,bar"
            args.kafka_settings = json.dumps({"foo": "bar"})
            args.topic_prefix = "prefix"