Skip to content

Commit

Permalink
Created an extendable declarative base class
Browse files Browse the repository at this point in the history
... for mlwh ORM classes so that common methods can be
implemented.

Customised __repr__ method for one of db classes.
  • Loading branch information
mgcam committed Jun 11, 2024
1 parent 5c77509 commit fbff9a7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
36 changes: 33 additions & 3 deletions lang_qc/db/mlwh_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,30 @@
from sqlalchemy.dialects.mysql import SMALLINT as mysqlSMALLINT
from sqlalchemy.dialects.mysql import TINYINT as mysqlTINYINT
from sqlalchemy.dialects.mysql import VARCHAR as mysqlVARCHAR
from sqlalchemy.orm import declarative_base, relationship
from sqlalchemy.orm import DeclarativeBase, relationship

Base = declarative_base()

class Base(DeclarativeBase):
"""
A base class for declarative class definitions for the ml warehouse database.
"""

def get_row_description(self, fields: list[str]) -> str:
"""
Returns a printable representation of the database table row. Interprets
a list of strings given as the `fields` argument as a list of column
names. Combines the name of the class, names of the given columns
and respective values into a row description. The columns for which
the row has a NULL value are omitted from the description.
"""

pairs = []
for name in fields:
value = self.__getattribute__(name)
if value is not None:
pairs.append(f"{name}={value}")
description = ", ".join(pairs)
return f"{self.__module__}.{self.__class__.__name__}: {description}"


class Sample(Base):
Expand Down Expand Up @@ -538,7 +559,16 @@ class PacBioRunWellMetrics(Base):
"PacBioProductMetrics", back_populates="pac_bio_run_well_metrics"
)

def get_experiment_info(self):
"""Custom or customised methods are added below"""

def __repr__(self):
"""Returns a printable representation of the database row"""

return self.get_row_description(
["pac_bio_run_name", "well_label", "plate_number", "id_pac_bio_product"]
)

def get_experiment_info(self) -> list[PacBioRun]:
"""Returns a list of PacBioRun mlwh database rows.
Returns LIMS information about the PacBio experiment
Expand Down
24 changes: 24 additions & 0 deletions tests/test_mlwh_db_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from sqlalchemy import select

from lang_qc.db.mlwh_schema import PacBioRunWellMetrics

"""Tests for custom and customised ORM methods"""


def test_pac_bio_well_metrics_repr(mlwhdb_test_session, mlwhdb_load_runs):
id1 = "cf18bd66e0f0895ea728c1d08103c62d3de8a57a5f879cee45f7b0acc028aa61"
id2 = "513c674f489b106c6af716dd0d210826ff03b7648d50888839c3722ca1b10dbf"
data = {
id1: f"pac_bio_run_name=TRACTION-RUN-92, well_label=A1, id_pac_bio_product={id1}",
id2: f"pac_bio_run_name=TRACTION-RUN-1140, well_label=A1, plate_number=2, id_pac_bio_product={id2}",
}

for id in data.keys():
query = select(PacBioRunWellMetrics).where(
PacBioRunWellMetrics.id_pac_bio_product == id
)
db_row = mlwhdb_test_session.execute(query).scalar_one()
assert (
db_row.__repr__()
== "lang_qc.db.mlwh_schema.PacBioRunWellMetrics: " + data[id]
)

0 comments on commit fbff9a7

Please sign in to comment.