Skip to content

Commit

Permalink
Add tests for failed requests behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
sdjmchattie committed Mar 22, 2024
1 parent 9586c77 commit 683481b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
37 changes: 35 additions & 2 deletions tests/messages/traction/test_qc_message.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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
34 changes: 33 additions & 1 deletion tests/messages/traction/test_reception_message.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 683481b

Please sign in to comment.