From c13e329208dbc74860b6d6d80fb808d6129e0c4f Mon Sep 17 00:00:00 2001 From: Chris Berkhout Date: Thu, 29 Jul 2021 17:04:32 +0200 Subject: [PATCH] Test exceptions handler. --- tests/pricehist/test_exceptions.py | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/pricehist/test_exceptions.py diff --git a/tests/pricehist/test_exceptions.py b/tests/pricehist/test_exceptions.py new file mode 100644 index 0000000..9837d03 --- /dev/null +++ b/tests/pricehist/test_exceptions.py @@ -0,0 +1,43 @@ +import logging +import sys + +import pytest + +from pricehist import exceptions + + +def test_handler_logs_debug_information(caplog): + with caplog.at_level(logging.DEBUG): + try: + with exceptions.handler(): + raise exceptions.RequestError("Some message") + except SystemExit: + pass + + assert caplog.records[0].levelname == "DEBUG" + assert "exception encountered" in caplog.records[0].message + assert caplog.records[0].exc_info + + +def test_handler_exits_nonzero(caplog): + with pytest.raises(SystemExit) as e: + with exceptions.handler(): + raise exceptions.RequestError("Some message") + + assert e.value.code == 1 + + +def test_handler_logs_critical_information(caplog): + with caplog.at_level(logging.CRITICAL): + try: + with exceptions.handler(): + raise exceptions.RequestError("Some message") + except SystemExit: + pass + + assert any( + [ + "CRITICAL" == r.levelname and "Some message" in r.message + for r in caplog.records + ] + )