Skip to content

Commit 5ed0239

Browse files
authored
Test mutant validator (#4134)
add tests for mutant validator
1 parent bb2285d commit 5ed0239

File tree

5 files changed

+106
-103
lines changed

5 files changed

+106
-103
lines changed

cg/services/orders/validation/workflows/mutant/models/sample.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class MutantSample(Sample):
3939

4040
@model_validator(mode="before")
4141
@classmethod
42-
def set_original_lab_address(cls, data: any):
42+
def set_original_lab_address(cls, data: any) -> any:
4343
if isinstance(data, dict):
4444
is_set = bool(data.get("original_lab_address"))
4545
if not is_set:
@@ -48,7 +48,7 @@ def set_original_lab_address(cls, data: any):
4848

4949
@model_validator(mode="before")
5050
@classmethod
51-
def set_region_code(cls, data: any):
51+
def set_region_code(cls, data: any) -> any:
5252
if isinstance(data, dict):
5353
is_set = bool(data.get("region_code"))
5454
if not is_set:

tests/fixtures/cgweb_orders/sarscov2.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"organism": "SARS-CoV-2",
4040
"organism_other": "",
4141
"original_lab": "Karolinska University Hospital Solna",
42-
"original_lab_address": "171 76 Solna",
42+
"original_lab_address": "",
4343
"phenotype_groups": null,
4444
"phenotype_terms": null,
4545
"pool": null,
@@ -51,7 +51,7 @@
5151
"reagent_label": null,
5252
"reference_genome": "NC_111",
5353
"region": "Stockholm",
54-
"region_code": "01",
54+
"region_code": "",
5555
"require_qc_ok": null,
5656
"rml_plate_name": null,
5757
"selection_criteria": "Allmän övervakning",

tests/services/orders/lims_service/test_order_lims_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_to_lims_sarscov2(mutant_order: MutantOrder):
164164
assert first_sample["udfs"]["lab_code"] == "SE100 Karolinska"
165165
assert first_sample["udfs"]["organism"] == "SARS-CoV-2"
166166
assert first_sample["udfs"]["original_lab"] == "Karolinska University Hospital Solna"
167-
assert first_sample["udfs"]["original_lab_address"] == "171 76 Solna"
167+
assert first_sample["udfs"]["original_lab_address"] == "171 76 Stockholm"
168168
assert first_sample["udfs"]["pre_processing_method"] == "COVIDSeq"
169169
assert first_sample["udfs"]["priority"] == "research"
170170
assert first_sample["udfs"]["reference_genome"] == "NC_111"

tests/services/orders/validation_service/test_model_validator.py

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
from cg.services.orders.validation.model_validator.model_validator import ModelValidator
44
from cg.services.orders.validation.models.order import Order
55
from cg.services.orders.validation.workflows.fluffy.models.order import FluffyOrder
6+
from cg.services.orders.validation.workflows.mutant.models.order import MutantOrder
67
from cg.services.orders.validation.workflows.rml.models.order import RmlOrder
8+
from cg.services.orders.validation.workflows.tomte.models.order import TomteOrder
79

810

911
@pytest.mark.parametrize(
@@ -21,12 +23,110 @@ def test_validate_pool_sample_default_index(
2123
model_validator: ModelValidator,
2224
request: pytest.FixtureRequest,
2325
):
26+
"""Test the default index sequence is set for a pool sample without index sequence."""
2427
# GIVEN a pool raw order with a sample without index sequence but correct index and index number
2528
raw_order: dict = request.getfixturevalue(order_fixture)
26-
assert raw_order["samples"][0]["index_sequence"] == ""
29+
assert not raw_order["samples"][0]["index_sequence"]
2730

2831
# WHEN validating the order
2932
order, _ = model_validator.validate(order=raw_order, model=order_model)
3033

3134
# THEN the index sequence should be set to the default index sequence
3235
assert order.samples[0].index_sequence == expected_index_sequence
36+
37+
38+
def test_validate_mutant_sample_gets_lab_and_region(
39+
sarscov2_order_to_submit: dict, model_validator: ModelValidator
40+
):
41+
"""Test the lab address and region code are set for a mutant sample without these fields."""
42+
# GIVEN a Mutant order with a sample without lab address and region code
43+
assert not sarscov2_order_to_submit["samples"][0]["original_lab_address"]
44+
assert not sarscov2_order_to_submit["samples"][0]["region_code"]
45+
46+
# WHEN validating the order
47+
order, _ = model_validator.validate(order=sarscov2_order_to_submit, model=MutantOrder)
48+
49+
# THEN the lab address and region code should be set
50+
assert order.samples[0].original_lab_address == "171 76 Stockholm"
51+
assert order.samples[0].region_code == "01"
52+
53+
54+
def test_order_field_error(valid_order: TomteOrder, model_validator: ModelValidator):
55+
# GIVEN a Tomte order with an order field error
56+
valid_order.name = ""
57+
raw_order: dict = valid_order.model_dump(by_alias=True)
58+
59+
# WHEN validating the order
60+
_, errors = model_validator.validate(order=raw_order, model=TomteOrder)
61+
62+
# THEN there should be an order error
63+
assert errors.order_errors
64+
65+
# THEN the error should concern the missing name
66+
assert errors.order_errors[0].field == "name"
67+
68+
69+
def test_case_field_error(valid_order: TomteOrder, model_validator: ModelValidator):
70+
# GIVEN a Tomte order with a case field error
71+
valid_order.cases[0].priority = None
72+
raw_order: dict = valid_order.model_dump()
73+
74+
# WHEN validating the order
75+
_, errors = model_validator.validate(order=raw_order, model=TomteOrder)
76+
77+
# THEN there should be a case error
78+
assert errors.case_errors
79+
80+
# THEN the error should concern the missing name
81+
assert errors.case_errors[0].field == "priority"
82+
83+
84+
def test_case_sample_field_error(valid_order: TomteOrder, model_validator: ModelValidator):
85+
86+
# GIVEN a Tomte order with a case sample error
87+
valid_order.cases[0].samples[0].well_position = 1.8
88+
raw_order: dict = valid_order.model_dump()
89+
90+
# WHEN validating the order
91+
_, errors = model_validator.validate(order=raw_order, model=TomteOrder)
92+
93+
# THEN a case sample error should be returned
94+
assert errors.case_sample_errors
95+
96+
# THEN the case sample error should concern the invalid data type
97+
assert errors.case_sample_errors[0].field == "well_position"
98+
99+
100+
def test_order_case_and_case_sample_field_error(
101+
valid_order: TomteOrder, model_validator: ModelValidator
102+
):
103+
# GIVEN a Tomte order with an order, case and case sample error
104+
valid_order.name = None
105+
valid_order.cases[0].priority = None
106+
valid_order.cases[0].samples[0].well_position = 1.8
107+
raw_order: dict = valid_order.model_dump(by_alias=True)
108+
109+
# WHEN validating the order
110+
_, errors = model_validator.validate(order=raw_order, model=TomteOrder)
111+
112+
# THEN all errors should be returned
113+
assert errors.order_errors
114+
assert errors.case_errors
115+
assert errors.case_sample_errors
116+
117+
# THEN the errors should concern the relevant fields
118+
assert errors.order_errors[0].field == "name"
119+
assert errors.case_errors[0].field == "priority"
120+
assert errors.case_sample_errors[0].field == "well_position"
121+
122+
123+
def test_null_conversion(valid_order: TomteOrder, model_validator: ModelValidator):
124+
# GIVEN a Tomte order with a sample with empty concentration
125+
valid_order.cases[0].samples[0].concentration_ng_ul = ""
126+
raw_order: dict = valid_order.model_dump(by_alias=True)
127+
128+
# WHEN validating the order
129+
order, _ = model_validator.validate(order=raw_order, model=TomteOrder)
130+
131+
# THEN the empty concentration should be converted to None
132+
assert order.cases[0].samples[0].concentration_ng_ul is None

tests/services/orders/validation_service/workflows/tomte/test_model_validation.py

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)