From a363b843720cd2c1c5b84992bcc2cb8eafc1393d Mon Sep 17 00:00:00 2001 From: Marina Gourtovaia Date: Fri, 24 Apr 2026 13:29:55 +0100 Subject: [PATCH] Added a function for validating runfolder name --- CHANGELOG | 12 +++++++++-- src/npgtracking/db/retrieval.py | 22 +++++++++++++++++++++ tests/{test_schema.py => test_retrieval.py} | 19 +++++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) rename tests/{test_schema.py => test_retrieval.py} (83%) diff --git a/CHANGELOG b/CHANGELOG index f325039..18709fd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,22 +5,30 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +## [1.0.0] - 2026-04-24 + ### Changed * Upgraded MySQL version for GitHub CI to 8.4 * Updated the ORM to reflect [changes](https://github.com/wtsi-npg/npg_tracking/pull/969) to the production database. * Regenerated the ORM with the current (4.0.3) version of `sqlacodegen`. +* Renamed `tests/test_schema.py` to `tests/test_retrieval.py` to reflect the + module name that is being tested. + +### Added + +* `validate_runfolder` function to npgtracking.db.retrieval ## [0.2.0] - 2025-12-12 ### Added -* get_run_by_id function added to retrieval module +* `get_run_by_id` function added to retrieval module ### Fixed -* run.batch_id is now a varchar +* `run.batch_id` is now a varchar ## [0.1.1] - 2025-12-11 diff --git a/src/npgtracking/db/retrieval.py b/src/npgtracking/db/retrieval.py index e424122..0ab8cdb 100644 --- a/src/npgtracking/db/retrieval.py +++ b/src/npgtracking/db/retrieval.py @@ -107,3 +107,25 @@ def get_run_by_id( raise ValueError("Can't get one run without an argument") result = session.execute(statement).scalar_one_or_none() return result + + +def validate_runfolder(session: Session, id_run: int, runfolder_name: str) -> bool: + """Validates the runfolder name against the run ID. + + Errors if run with the given ID does not exist. + + Args: + session : + Database session + id_run : + Tracking run ID + runfolder_name : + Runfolder name + Returns: + `True` if the runfolder name and run id belong to the same run, `False` + otherwise. + """ + run = session.execute(select(Run).where(Run.id_run == id_run)).scalar() + if not run: + raise ValueError(f"Run with ID {id_run} does not exist.") + return run.folder_name == runfolder_name diff --git a/tests/test_schema.py b/tests/test_retrieval.py similarity index 83% rename from tests/test_schema.py rename to tests/test_retrieval.py index 3ed450e..52d01f6 100644 --- a/tests/test_schema.py +++ b/tests/test_retrieval.py @@ -21,10 +21,11 @@ from npgtracking.db.retrieval import ( get_run_by_id, get_runs_by_currentstatus, + validate_runfolder, ) -@m.describe("SchemaModel") +@m.describe("Data retrieval from the tracking database") class TestSchemaModel(object): @m.context("When retrieving run records from tracking DB") @m.context("When there are no runs having a current status and manufacturer name") @@ -102,3 +103,19 @@ def test_run_by_id(self, tracking_session): run = get_run_by_id(session=tracking_session, id_run=12345) assert run is None + + @m.context("When runfolder name and run ID are correct") + @m.it("Validator returns True, otherwise False") + def test_validate_runfolder(self, tracking_session): + assert ( + validate_runfolder(tracking_session, 51533, "430591-20251204_1628") is True + ) + assert ( + validate_runfolder(tracking_session, 51533, "430591-20251204_XXXX") is False + ) + + @m.context("When run ID is invalid") + @m.it("An error is raised") + def test_validate_runfolder_error(self, tracking_session): + with raises(ValueError, match="Run with ID 1 does not exist"): + validate_runfolder(tracking_session, 1, "430591-20251204_1628")