Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing and add blueprint for converting errors #3476

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion cg/services/order_validation_service/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from typing import Callable

from cg.services.order_validation_service.models.errors import CaseSampleError, OrderError
from pydantic import ValidationError

from cg.services.order_validation_service.models.errors import (
CaseSampleError,
OrderError,
ValidationErrors,
)
from cg.services.order_validation_service.models.order import Order
from cg.store.store import Store

Expand All @@ -21,3 +27,7 @@ def apply_case_sample_validation(
rule_errors: list[CaseSampleError] = rule(case=case, store=store)
errors.extend(rule_errors)
return errors


def convert_errors(pydantic_errors: list[ValidationError]) -> ValidationErrors:
pass
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from typing import Tuple
from cg.services.order_validation_service.models.errors import OrderError

from pydantic import ValidationError

from cg.services.order_validation_service.models.errors import ValidationErrors
from cg.services.order_validation_service.utils import convert_errors
from cg.services.order_validation_service.workflows.tomte.models.order import TomteOrder


class TomteFieldValidator:

def validate(self, order_json: str) -> Tuple[TomteOrder, list[OrderError]]:
def validate(self, order_json: str) -> Tuple[TomteOrder, ValidationErrors]:
islean marked this conversation as resolved.
Show resolved Hide resolved
try:
return TomteOrder.model_validate_json(order_json)
except ValueError:
# TODO: map all Pydantic errors to ValidationError
pass
return TomteOrder.model_validate_json(order_json), ValidationErrors()
except ValidationError as error:
order_errors: ValidationErrors = convert_errors(error.errors())
return order_errors
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from cg.models.orders.sample_base import ContainerEnum
from cg.services.order_validation_service.models.errors import OccupiedWellError, OrderError
from cg.services.order_validation_service.workflows.tomte.models.order import TomteOrder
from cg.services.order_validation_service.workflows.tomte.models.sample import TomteSample
from cg.services.order_validation_service.models.errors import OccupiedWellError
from cg.services.order_validation_service.workflows.tomte.models.case import TomteCase
from cg.services.order_validation_service.workflows.tomte.models.order import TomteOrder
from cg.services.order_validation_service.workflows.tomte.models.sample import (
TomteSample,
)


def validate_wells_contain_at_most_one_sample(order: TomteOrder) -> list[OccupiedWellError]:
Expand Down Expand Up @@ -44,8 +46,8 @@ def _get_excess_samples(
return colliding_samples


def _get_sample_well_map(plate_samples_with_cases: list[tuple[TomteSample, str]]):
sample_well_map: dict[str, tuple[TomteSample, TomteCase]] = {}
def _get_sample_well_map(plate_samples_with_cases: list[tuple[TomteSample, TomteCase]]):
sample_well_map: dict[str, list[tuple[TomteSample, TomteCase]]] = {}
for sample, case in plate_samples_with_cases:
if sample.well_position not in sample_well_map:
sample_well_map[sample.well_position] = []
Expand Down
11 changes: 8 additions & 3 deletions tests/services/order_validation_service/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import pytest

from cg.constants.constants import GenomeVersion, Workflow
from cg.models.orders.sample_base import ContainerEnum, SexEnum, StatusEnum
from cg.services.order_validation_service.workflows.tomte.constants import TomteDeliveryType
from cg.services.order_validation_service.workflows.tomte.constants import (
TomteDeliveryType,
)
from cg.services.order_validation_service.workflows.tomte.models.case import TomteCase
from cg.services.order_validation_service.workflows.tomte.models.order import TomteOrder
from cg.services.order_validation_service.workflows.tomte.models.sample import TomteSample
from cg.services.order_validation_service.workflows.tomte.models.sample import (
TomteSample,
)


def create_sample(id: int) -> TomteSample:
Expand All @@ -18,7 +23,7 @@ def create_sample(id: int) -> TomteSample:
source="source",
status=StatusEnum.affected,
subject_id="subject1",
well_position=f"A{id}",
well_position=f"A:{id}",
)


Expand Down
Loading