diff --git a/tests/messages/traction/test_qc_message.py b/tests/messages/traction/test_qc_message.py index 6a5a6d2d..9ff1ad2c 100644 --- a/tests/messages/traction/test_qc_message.py +++ b/tests/messages/traction/test_qc_message.py @@ -1,12 +1,18 @@ -import logging from datetime import datetime +from unittest.mock import patch import pytest +import requests import requests_mock from tol_lab_share.messages.traction.qc_message import TractionQcMessage, QcRequestSerializer, TractionQcMessageRequest +from lab_share_lib.exceptions import TransientRabbitError -logger = logging.getLogger(__name__) + +@pytest.fixture(autouse=True) +def mock_logger(): + with patch("tol_lab_share.messages.traction.qc_message.logger") as logger: + yield logger class TestTractionQcMessage: @@ -125,3 +131,30 @@ def test_qc_request_serializer_clear_empty_value_keys(self): request = TractionQcMessageRequest() serial = QcRequestSerializer(request) assert serial.clear_empty_value_keys({"asdf": "1234", "bcde": ""}) == {"asdf": "1234"} + + def test_raises_transient_error_when_traction_api_responds_502(self, valid_traction_qc_message, config): + # This happens when the Traction API is down but nginx is still up. + with requests_mock.Mocker() as m: + m.post(config.TRACTION_URL, text="Error", status_code=502) + with pytest.raises(TransientRabbitError): + valid_traction_qc_message.send(config.TRACTION_URL) + + def test_raises_transient_error_when_post_raises(self, valid_traction_qc_message, config, mock_logger): + # This happens when nginx serving Traction API is down. + cause = requests.exceptions.RequestException() + + with requests_mock.Mocker() as m: + m.post(config.TRACTION_URL, exc=cause) + with pytest.raises(TransientRabbitError) as raised: + valid_traction_qc_message.send(config.TRACTION_URL) + + assert "QcMessage" in raised.value.message + mock_logger.exception.assert_called_once_with(cause) + + def test_can_detect_other_errors_on_sent(self, valid_traction_qc_message, config): + with requests_mock.Mocker() as m: + m.post(config.TRACTION_URL, text="Error", status_code=422) + valid_traction_qc_message.send(config.TRACTION_URL) + + assert not valid_traction_qc_message.validate() + assert len(valid_traction_qc_message.errors) > 0 diff --git a/tests/messages/traction/test_reception_message.py b/tests/messages/traction/test_reception_message.py index 8b599ccc..e6dc095f 100644 --- a/tests/messages/traction/test_reception_message.py +++ b/tests/messages/traction/test_reception_message.py @@ -1,8 +1,18 @@ +from unittest.mock import patch +import pytest +import requests from tol_lab_share.messages.traction.reception_message import TractionReceptionMessage +from lab_share_lib.exceptions import TransientRabbitError from datetime import datetime import requests_mock +@pytest.fixture(autouse=True) +def mock_logger(): + with patch("tol_lab_share.messages.traction.reception_message.logger") as logger: + yield logger + + def valid_traction_message(): instance = TractionReceptionMessage() request = instance.create_request() @@ -768,7 +778,29 @@ def test_can_generate_payload_for_mix_of_plate_and_tubes(self): } } - def test_can_detect_errors_on_sent(self, config): + def test_raises_transient_error_when_traction_api_responds_502(self, config): + # This happens when the Traction API is down but nginx is still up. + vt = valid_traction_message() + + with requests_mock.Mocker() as m: + m.post(config.TRACTION_URL, text="Error", status_code=502) + with pytest.raises(TransientRabbitError): + vt.send(config.TRACTION_URL) + + def test_raises_transient_error_when_post_raises(self, config, mock_logger): + # This happens when nginx serving Traction API is down. + vt = valid_traction_message() + cause = requests.exceptions.RequestException() + + with requests_mock.Mocker() as m: + m.post(config.TRACTION_URL, exc=cause) + with pytest.raises(TransientRabbitError) as raised: + vt.send(config.TRACTION_URL) + + assert "ReceptionMessage" in raised.value.message + mock_logger.exception.assert_called_once_with(cause) + + def test_can_detect_other_errors_on_sent(self, config): vt = valid_traction_message() with requests_mock.Mocker() as m: