Skip to content

Commit

Permalink
First (failing) pass at tests for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
danpalmer committed Apr 24, 2018
1 parent 40b0ca5 commit 651db1e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
7 changes: 7 additions & 0 deletions plugins/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Pytest conftest for plugin tests."""

from routemaster.conftest import app

__all__ = (
'app',
)
75 changes: 75 additions & 0 deletions plugins/tests/test_logging_plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from typing import Any, Dict, Type, Tuple, Iterable

import pytest
import requests
from flask import Flask
from routemaster_sentry import SentryLogger
from routemaster_prometheus import PrometheusLogger

from routemaster.logging import BaseLogger, SplitLogger

SENTRY_KWARGS = {'dsn': 'https://xxxxxxx:xxxxxxx@sentry.io/xxxxxxx'}
PROMETHEUS_KWARGS = {'path': '/metrics'}

TEST_CASES: Iterable[Tuple[Type[BaseLogger], Dict[str, Any]]] = [
(SentryLogger, SENTRY_KWARGS),
(PrometheusLogger, PROMETHEUS_KWARGS),
(PrometheusLogger, {}),
(SplitLogger, {'loggers': [
SentryLogger(None, **SENTRY_KWARGS),
PrometheusLogger(None, **PROMETHEUS_KWARGS),
]}),
]


@pytest.mark.parametrize('klass, kwargs', TEST_CASES)
def test_logger(app, klass, kwargs):
logger = klass(app.config, **kwargs)
state_machine = app.config.state_machines['test_machine']
state = state_machine.states[0]
feed_url = 'https://localhost'

server = Flask('test_server')
logger.init_flask(server)

with logger.process_cron(state_machine, state, 'test_cron'):
pass

with pytest.raises(RuntimeError):
with logger.process_cron(state_machine, state, 'test_cron'):
raise RuntimeError("Error must propagate")

with logger.process_feed(state_machine, state, feed_url):
pass

with pytest.raises(RuntimeError):
with logger.process_feed(state_machine, state, feed_url):
raise RuntimeError("Error must propagate")

with logger.process_webhook(state_machine, state):
pass

with pytest.raises(RuntimeError):
with logger.process_webhook(state_machine, state):
raise RuntimeError("Error must propagate")

with logger.process_request({}):
pass

with pytest.raises(RuntimeError):
with logger.process_request({}):
raise RuntimeError("Error must propagate")

logger.debug("test")
logger.info("test")
logger.warning("test")
logger.error("test")
logger.critical("test")
logger.exception("test")

with pytest.raises(AttributeError):
logger.non_existent_logging_fn("test")

response = requests.Response()
logger.webhook_response(state_machine, state, response)
logger.feed_response(state_machine, state, feed_url, response)

0 comments on commit 651db1e

Please sign in to comment.