Skip to content

Commit

Permalink
coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mochic committed Jul 17, 2024
1 parent 51ce1d2 commit 21183bd
Show file tree
Hide file tree
Showing 12 changed files with 471 additions and 225 deletions.
12 changes: 8 additions & 4 deletions src/aind_slims_api/behavior_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from pydantic import Field

from aind_slims_api.core import SlimsBaseModel, SlimsClient, SLIMSTABLES
from aind_slims_api.core import SlimsBaseModel, SlimsClient
from aind_slims_api.mouse import SlimsMouseContent
from aind_slims_api.instrument import SlimsInstrument
from aind_slims_api.user import SlimsUser
Expand All @@ -30,7 +30,11 @@ class SlimsBehaviorSessionContentEvent(SlimsBaseModel):

pk: int | None = Field(default=None, alias="cnvn_pk")
mouse_pk: int | None = Field(
default=None, alias="cnvn_fk_content"
default=None,
alias="cnvn_fk_content",
description=(
"The primary key of the mouse associated with this behavior session."
),
) # used as reference to mouse
notes: str | None = Field(default=None, alias="cnvn_cf_notes")
task_stage: str | None = Field(default=None, alias="cnvn_cf_taskStage")
Expand All @@ -47,8 +51,8 @@ class SlimsBehaviorSessionContentEvent(SlimsBaseModel):
default=None, alias="cnvn_cf_softwareVersion")
date: datetime | None = Field(default=None, alias="cnvn_cf_scheduledDate")
cnvn_fk_contentEventType: int = 10 # pk of Behavior Session ContentEvent
_slims_table: SLIMSTABLES = "ContentEvent"
_base_fetch_filters: ClassVar[dict[str, int | str]] = {
_slims_table = "ContentEvent"
_base_fetch_filters: ClassVar[dict[str, str]] = {
"cnvt_name": "Behavior Session",
}

Expand Down
16 changes: 8 additions & 8 deletions src/aind_slims_api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class MyModel(SlimsBaseModel):

pk: Optional[int] = None
json_entity: Optional[dict] = None
_slims_table: SLIMSTABLES
_base_fetch_filters: ClassVar[dict[str, str | int]] = {} # use for fetch_models, fetch_model
_slims_table: ClassVar[SLIMSTABLES]
_base_fetch_filters: ClassVar[dict[str, str]] = {} # use for fetch_models, fetch_model

@field_validator("*", mode="before")
def _validate(cls, value, info: ValidationInfo):
Expand Down Expand Up @@ -141,11 +141,11 @@ def _serialize(self, field, info):
# TODO: Add Table - need Record.json_entity['tableName']


class Attachment(SlimsBaseModel):
class SlimsAttachment(SlimsBaseModel):

pk: int = Field(..., alias="attm_pk")
name: str = Field(..., alias="attm_name")
_slims_table: SLIMSTABLES = "Attachment"
_slims_table = "Attachment"


SlimsBaseModelTypeVar = TypeVar("SlimsBaseModelTypeVar", bound=SlimsBaseModel)
Expand Down Expand Up @@ -281,7 +281,7 @@ def fetch_models(
]
logger.debug("Resolved sort: %s", resolved_sort)
response = self.fetch(
model._slims_table.default, # TODO: consider changing fetch method
model._slims_table, # TODO: consider changing fetch method
*args,
sort=resolved_sort,
start=start,
Expand Down Expand Up @@ -321,15 +321,15 @@ def fetch_model(
def fetch_attachments(
self,
record: SlimsBaseModel,
) -> list[Attachment]:
) -> list[SlimsAttachment]:
return self._validate_models(
Attachment,
SlimsAttachment,
self.db.slims_api.get_entities(
f"attachment/{record._slims_table}/{record.pk}"
)
)

def fetch_attachment_content(self, attachment: Attachment) -> Response:
def fetch_attachment_content(self, attachment: SlimsAttachment) -> Response:
return self.db.slims_api.get(f"repo/{attachment.pk}")

@lru_cache(maxsize=None)
Expand Down
4 changes: 2 additions & 2 deletions src/aind_slims_api/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pydantic import Field

from aind_slims_api.core import SlimsBaseModel, SLIMSTABLES
from aind_slims_api.core import SlimsBaseModel

logger = logging.getLogger()

Expand All @@ -25,7 +25,7 @@ class SlimsInstrument(SlimsBaseModel):
description="The name of the instrument",
)
pk: int = Field(..., alias="nstr_pk")
_slims_table: SLIMSTABLES = "Instrument"
_slims_table = "Instrument"

# todo add more useful fields

Expand Down
39 changes: 4 additions & 35 deletions src/aind_slims_api/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import logging
from typing import Annotated, ClassVar

from pydantic import Field, BeforeValidator, ValidationError
from pydantic import Field, BeforeValidator

from aind_slims_api.core import SlimsBaseModel, SlimsClient, UnitSpec, SLIMSTABLES
from aind_slims_api.core import SlimsBaseModel, UnitSpec

logger = logging.getLogger()

Expand All @@ -30,8 +30,8 @@ class SlimsMouseContent(SlimsBaseModel):
barcode: str = Field(..., alias="cntn_barCode")
pk: int = Field(..., alias="cntn_pk")

_slims_table: SLIMSTABLES = "Content"
_base_fetch_filters: ClassVar[dict[str, int | str]] = {
_slims_table = "Content"
_base_fetch_filters: ClassVar[dict[str, str]] = {
"cntp_name": "Mouse",
}

Expand All @@ -52,37 +52,6 @@ class SlimsMouseContent(SlimsBaseModel):
# cntn_cf_parentBarcode: SlimsColumn


def fetch_mouse_content(
client: SlimsClient,
mouse_name: str,
) -> SlimsMouseContent | dict | None:
"""Fetches mouse information for a mouse with labtracks id {mouse_name}"""
mice = client.fetch(
"Content",
cntp_name="Mouse",
cntn_barCode=mouse_name,
)

if len(mice) > 0:
mouse_details = mice[0]
if len(mice) > 1:
logger.warning(
f"Warning, Multiple mice in SLIMS with barcode "
f"{mouse_name}, using pk={mouse_details.cntn_pk.value}"
)
else:
logger.warning("Warning, Mouse not in SLIMS")
return None

try:
mouse = SlimsMouseContent.model_validate(mouse_details)
except ValidationError as e:
logger.error(f"SLIMS data validation failed, {repr(e)}")
return mouse_details.json_entity

return mouse


if __name__ == "__main__":
from aind_slims_api import testmod

Expand Down
4 changes: 2 additions & 2 deletions src/aind_slims_api/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pydantic import Field

from aind_slims_api.core import SlimsBaseModel, SLIMSTABLES
from aind_slims_api.core import SlimsBaseModel

logger = logging.getLogger()

Expand All @@ -17,4 +17,4 @@ class SlimsUnit(SlimsBaseModel):
abbreviation: Optional[str] = Field("", alias="unit_abbreviation")
pk: int = Field(..., alias="unit_pk")

_slims_table: SLIMSTABLES = "Unit"
_slims_table = "Unit"
6 changes: 3 additions & 3 deletions src/aind_slims_api/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pydantic import Field

from aind_slims_api.core import SlimsBaseModel, SLIMSTABLES
from aind_slims_api.core import SlimsBaseModel


# TODO: Tighten this up once users are more commonly used
Expand All @@ -12,7 +12,7 @@ class SlimsUser(SlimsBaseModel):
>>> from aind_slims_api.core import SlimsClient
>>> client = SlimsClient()
>>> user = client.fetch_model(SlimsUser, "LisaK")
>>> user = client.fetch_model(SlimsUser, username="LKim")
"""

username: str = Field(..., alias="user_userName")
Expand All @@ -22,7 +22,7 @@ class SlimsUser(SlimsBaseModel):
email: Optional[str] = Field("", alias="user_email")
pk: int = Field(..., alias="user_pk")

_slims_table: SLIMSTABLES = "User"
_slims_table = "User"


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 21183bd

Please sign in to comment.