Skip to content

Commit

Permalink
Apply optional value and tests for Request
Browse files Browse the repository at this point in the history
  • Loading branch information
sdjmchattie committed May 10, 2024
1 parent d2ff8dc commit 0bc1122
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
59 changes: 59 additions & 0 deletions tests/messages/properties/complex/test_request.py
Original file line number Diff line number Diff line change
@@ -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
23 changes: 4 additions & 19 deletions tol_lab_share/messages/properties/complex/request.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
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,
REQUEST_LIBRARY_TYPE,
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

Expand All @@ -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)

0 comments on commit 0bc1122

Please sign in to comment.