diff --git a/tests/messages/properties/complex/test_request.py b/tests/messages/properties/complex/test_request.py new file mode 100644 index 00000000..916e348e --- /dev/null +++ b/tests/messages/properties/complex/test_request.py @@ -0,0 +1,59 @@ +import pytest +from tol_lab_share import error_codes +from tol_lab_share.messages.properties.complex.request import Request +from tol_lab_share.messages.properties.simple.dict_value import Value + + +@pytest.fixture +def valid_request(request): + return Value( + { + "costCode": "CostCode789", + "libraryType": "LibraryType123", + "studyUuid": b"12345678-1234-1234-1234-1234567890ab", + **request.param, + } + ) + + +@pytest.fixture +def invalid_request(): + return Value( + { + "costCode": 1234, + "studyUuid": "12345678-1234-1234-1234-1234567890ab", + } + ) + + +def check_error_is_present(sample, error_code, field): + assert any( + [((x.type_id == error_code.type_id) and (x.field == field)) for x in sample.errors] + ), f"Error type '{error_code.type_id}' for field '{field}' not found among {sample.errors}" + + +class TestBioscanPoolXpSample: + @pytest.mark.parametrize( + "valid_request", [{}, {"genomeSize": ""}, {"genomeSize": "1,234,567,890 bp"}], indirect=True + ) + def test_validators_when_request_is_valid(self, valid_request): + print(valid_request.value) + instance = Request(valid_request) + assert instance.validate() is True + assert len(instance.errors) == 0 + + def test_validators_when_request_incomplete(self): + instance = Request(Value({})) + assert instance.validate() is False + assert len(instance.errors) > 0 + + def test_validators_when_request_is_invalid(self, invalid_request): + instance = Request(invalid_request) + assert instance.validate() is False + + check_error_is_present(instance, error_codes.ERROR_2_NOT_STRING, "cost_code") + check_error_is_present(instance, error_codes.ERROR_9_INVALID_INPUT, "library_type") + check_error_is_present(instance, error_codes.ERROR_1_UUID_NOT_BINARY, "study_uuid") + check_error_is_present(instance, error_codes.ERROR_2_UUID_NOT_RIGHT_FORMAT, "study_uuid") + + assert len(instance.errors) == 4 diff --git a/tol_lab_share/messages/properties/complex/request.py b/tol_lab_share/messages/properties/complex/request.py index 846e25f7..820aa8fb 100644 --- a/tol_lab_share/messages/properties/complex/request.py +++ b/tol_lab_share/messages/properties/complex/request.py @@ -1,5 +1,4 @@ from .uuid import Uuid -from .labware_type import LabwareType from tol_lab_share.constants.input_bioscan_pool_xp_to_traction_message import ( REQUEST_COST_CODE, REQUEST_GENOME_SIZE, @@ -7,10 +6,7 @@ REQUEST_STUDY_UUID, ) from tol_lab_share.messages.properties.simple import DictValue, StringValue -from tol_lab_share.messages.traction import TractionReceptionMessage - from tol_lab_share.messages.properties import MessageProperty -from functools import singledispatchmethod import logging @@ -23,20 +19,9 @@ class Request(MessageProperty): def __init__(self, input: MessageProperty): super().__init__(input) - self.add_property("cost_code", LabwareType(DictValue(input, REQUEST_COST_CODE))) - self.add_property("genome_size", StringValue(DictValue(input, REQUEST_GENOME_SIZE))) + self.add_property("cost_code", StringValue(DictValue(input, REQUEST_COST_CODE))) + self.add_property( + "genome_size", StringValue(DictValue(input, REQUEST_GENOME_SIZE, optional=True), optional=True) + ) self.add_property("library_type", StringValue(DictValue(input, REQUEST_LIBRARY_TYPE))) self.add_property("study_uuid", Uuid(DictValue(input, REQUEST_STUDY_UUID))) - - @singledispatchmethod - def add_to_message_property(self, message_property: MessageProperty) -> None: - super().add_to_message_property(message_property) - - @add_to_message_property.register - def _(self, message: TractionReceptionMessage) -> None: - """Adds the request information to an OutputTractionMessage. - - Args: - message (TractionReceptionMessage): The Traction reception message to add the data to. - """ - super().add_to_message_property(message)