Skip to content

Commit 47a9944

Browse files
committed
fixes
1 parent 779da45 commit 47a9944

File tree

4 files changed

+135
-79
lines changed

4 files changed

+135
-79
lines changed

cg/apps/downsample/downsample.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
from typing import List
55

66
from cg.apps.downsample.utils import case_exists_in_statusdb, sample_exists_in_statusdb
7+
from cg.apps.housekeeper.hk import HousekeeperAPI
78
from cg.constants import SequencingFileTag
89
from cg.exc import DownsampleFailedError
910
from cg.meta.meta import MetaAPI
1011
from cg.meta.workflow.downsample.downsample import DownsampleWorkflow
1112
from cg.models.cg_config import CGConfig
1213
from cg.models.downsample.downsample_data import DownsampleData
14+
from cg.store import Store
1315
from cg.store.models import Family, Sample
1416
from cg.utils.calculations import multiply_by_million
1517
from cg.utils.files import get_files_matching_pattern
@@ -28,7 +30,9 @@ def __init__(
2830
):
2931
"""Initialize the API."""
3032
super().__init__(config)
31-
self.config = config
33+
self.config: CGConfig = config
34+
self.status_db: Store = config.status_db
35+
self.hk_api: HousekeeperAPI = config.housekeeper_api
3236
self.sample_id: str = sample_id
3337
self.number_of_reads: float = number_of_reads
3438
self.case_id: str = case_id
@@ -77,7 +81,7 @@ def add_downsampled_case_to_statusdb(self) -> Family:
7781
if case_exists_in_statusdb(status_db=self.status_db, case_id=downsampled_case.internal_id):
7882
raise ValueError(f"Case {downsampled_case.internal_id} already exists in StatusDB.")
7983
if not self.dry_run:
80-
self.status_db.session.add_commit(downsampled_case)
84+
self.status_db.session.commit(downsampled_case)
8185
LOG.info(f"New down sampled case created: {downsampled_case.internal_id}")
8286
return downsampled_case
8387
return downsampled_case
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Tests for the DownsampleAPI."""
2+
from cg.apps.downsample.downsample import DownSampleAPI
3+
from cg.apps.housekeeper.hk import HousekeeperAPI
4+
from cg.models.downsample.downsample_data import DownsampleData
5+
from cg.store import Store
6+
7+
8+
def test_add_downsampled_sample_entry_to_statusdb(downsample_api: DownSampleAPI) -> None:
9+
"""Test to add the downsampled sample entry to StatusDB."""
10+
# GIVEN a DownsampleAPI
11+
12+
# WHEN adding a downsampled case to statusDB
13+
downsample_api.add_downsampled_case_to_statusdb()
14+
15+
# THEN a downsampled case is added to the store
16+
assert downsample_api.status_db.get_case_by_name(
17+
downsample_api.downsample_data.downsampled_case.name
18+
)

tests/conftest.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
FlowCellSampleBcl2Fastq,
1919
FlowCellSampleBCLConvert,
2020
)
21+
from cg.apps.downsample.downsample import DownSampleAPI
2122
from cg.apps.gens import GensAPI
2223
from cg.apps.gt import GenotypeAPI
2324
from cg.apps.hermes.hermes_api import HermesApi
@@ -3259,3 +3260,113 @@ def flow_cell_encryption_api(
32593260
)
32603261
flow_cell_encryption_api.slurm_api.set_dry_run(dry_run=True)
32613262
return flow_cell_encryption_api
3263+
3264+
3265+
# Downsample
3266+
@pytest.fixture()
3267+
def store_with_case_and_sample_with_reads(
3268+
store: Store,
3269+
helpers: StoreHelpers,
3270+
downsample_case_internal_id: str,
3271+
downsample_sample_internal_id_1: str,
3272+
downsample_sample_internal_id_2: str,
3273+
) -> Store:
3274+
"""Return a store with a case and a sample with reads."""
3275+
case: Family = helpers.add_case(store=store, internal_id=downsample_case_internal_id)
3276+
3277+
for sample_internal_id in [downsample_sample_internal_id_1, downsample_sample_internal_id_2]:
3278+
helpers.add_sample(
3279+
store=store,
3280+
internal_id=sample_internal_id,
3281+
customer_id=case.customer_id,
3282+
reads=100_000_000,
3283+
)
3284+
sample: Sample = store.get_sample_by_internal_id(internal_id=sample_internal_id)
3285+
helpers.add_relationship(store=store, case=case, sample=sample)
3286+
3287+
return store
3288+
3289+
3290+
@pytest.fixture()
3291+
def downsample_case_internal_id() -> str:
3292+
"""Return a case internal id."""
3293+
return "supersonicturtle"
3294+
3295+
3296+
@pytest.fixture()
3297+
def downsample_sample_internal_id_1() -> str:
3298+
"""Return a sample internal id."""
3299+
return "ACC12345675213"
3300+
3301+
3302+
@pytest.fixture()
3303+
def downsample_sample_internal_id_2() -> str:
3304+
"""Return a sample internal id."""
3305+
return "ACC12345684213"
3306+
3307+
3308+
@pytest.fixture()
3309+
def number_of_reads_in_millions() -> int:
3310+
"""Return a number of reads in millions."""
3311+
return 50
3312+
3313+
3314+
@pytest.fixture()
3315+
def downsample_hk_api(
3316+
real_housekeeper_api: HousekeeperAPI,
3317+
fastq_file: Path,
3318+
downsample_sample_internal_id_1: str,
3319+
downsample_sample_internal_id_2: str,
3320+
timestamp_yesterday: str,
3321+
helpers: StoreHelpers,
3322+
tmp_path_factory,
3323+
) -> HousekeeperAPI:
3324+
"""Return a Housekeeper API with a real database."""
3325+
for sample_internal_id in [downsample_sample_internal_id_1, downsample_sample_internal_id_2]:
3326+
tmp_fastq_file = tmp_path_factory.mktemp(f"{sample_internal_id}.fastq.gz")
3327+
downsample_bundle: dict = {
3328+
"name": sample_internal_id,
3329+
"created": timestamp_yesterday,
3330+
"expires": timestamp_yesterday,
3331+
"files": [
3332+
{
3333+
"path": tmp_fastq_file.as_posix(),
3334+
"archive": False,
3335+
"tags": [SequencingFileTag.FASTQ, sample_internal_id],
3336+
}
3337+
],
3338+
}
3339+
helpers.ensure_hk_bundle(store=real_housekeeper_api, bundle_data=downsample_bundle)
3340+
return real_housekeeper_api
3341+
3342+
3343+
@pytest.fixture()
3344+
def downsample_context(
3345+
cg_context: CGConfig,
3346+
store_with_case_and_sample_with_reads: Store,
3347+
downsample_hk_api: HousekeeperAPI,
3348+
) -> CGConfig:
3349+
"""Return cg context with added Store and Housekeeper API."""
3350+
cg_context.status_db_ = store_with_case_and_sample_with_reads
3351+
cg_context.housekeeper_api_ = downsample_hk_api
3352+
return cg_context
3353+
3354+
3355+
@pytest.fixture()
3356+
def downsample_api(
3357+
downsample_context: CGConfig,
3358+
store_with_case_and_sample_with_reads: Store,
3359+
downsample_hk_api: HousekeeperAPI,
3360+
downsample_sample_internal_id_1: str,
3361+
downsample_case_internal_id: str,
3362+
number_of_reads_in_millions: int,
3363+
tmp_path_factory,
3364+
) -> DownSampleAPI:
3365+
"""Return a DownsampleAPI."""
3366+
downsample_api = DownSampleAPI(
3367+
config=downsample_context,
3368+
sample_id=downsample_sample_internal_id_1,
3369+
number_of_reads=number_of_reads_in_millions,
3370+
case_id=downsample_case_internal_id,
3371+
)
3372+
return downsample_api

tests/models/downsample/conftest.py

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,80 +9,3 @@
99
from cg.store import Store
1010
from cg.store.models import Family, Sample
1111
from tests.store_helpers import StoreHelpers
12-
13-
14-
@pytest.fixture()
15-
def store_with_case_and_sample_with_reads(
16-
store: Store,
17-
helpers: StoreHelpers,
18-
downsample_case_internal_id: str,
19-
downsample_sample_internal_id_1: str,
20-
downsample_sample_internal_id_2: str,
21-
) -> Store:
22-
"""Return a store with a case and a sample with reads."""
23-
case: Family = helpers.add_case(store=store, internal_id=downsample_case_internal_id)
24-
25-
for sample_internal_id in [downsample_sample_internal_id_1, downsample_sample_internal_id_2]:
26-
helpers.add_sample(
27-
store=store,
28-
internal_id=sample_internal_id,
29-
customer_id=case.customer_id,
30-
reads=100_000_000,
31-
)
32-
sample: Sample = store.get_sample_by_internal_id(internal_id=sample_internal_id)
33-
helpers.add_relationship(store=store, case=case, sample=sample)
34-
35-
return store
36-
37-
38-
@pytest.fixture()
39-
def downsample_case_internal_id() -> str:
40-
"""Return a case internal id."""
41-
return "supersonicturtle"
42-
43-
44-
@pytest.fixture()
45-
def downsample_sample_internal_id_1() -> str:
46-
"""Return a sample internal id."""
47-
return "ACC12345675213"
48-
49-
50-
@pytest.fixture()
51-
def downsample_sample_internal_id_2() -> str:
52-
"""Return a sample internal id."""
53-
return "ACC12345684213"
54-
55-
56-
@pytest.fixture()
57-
def number_of_reads_in_millions() -> int:
58-
"""Return a number of reads in millions."""
59-
return 50
60-
61-
62-
@pytest.fixture()
63-
def downsample_hk_api(
64-
real_housekeeper_api: HousekeeperAPI,
65-
fastq_file: Path,
66-
downsample_sample_internal_id_1: str,
67-
downsample_sample_internal_id_2: str,
68-
timestamp_yesterday: str,
69-
helpers: StoreHelpers,
70-
tmp_path_factory,
71-
) -> HousekeeperAPI:
72-
"""Return a Housekeeper API with a real database."""
73-
for sample_internal_id in [downsample_sample_internal_id_1, downsample_sample_internal_id_2]:
74-
tmp_fastq_file = tmp_path_factory.mktemp(f"{sample_internal_id}.fastq.gz")
75-
downsample_bundle: Dict = {
76-
"name": sample_internal_id,
77-
"created": timestamp_yesterday,
78-
"expires": timestamp_yesterday,
79-
"files": [
80-
{
81-
"path": tmp_fastq_file.as_posix(),
82-
"archive": False,
83-
"tags": [SequencingFileTag.FASTQ, sample_internal_id],
84-
}
85-
],
86-
}
87-
helpers.ensure_hk_bundle(store=real_housekeeper_api, bundle_data=downsample_bundle)
88-
return real_housekeeper_api

0 commit comments

Comments
 (0)