Skip to content

Commit 5ac1bcf

Browse files
committed
Adjust tests to account for s/external_id/external_ids/g
test_sample.py: Add a test exercising get_sample_id_map_by_external_ids(). test_upsert.py: Update direct SQL queries to use the new tables and to ensure that their rows are returned in the expected order.
1 parent 0cb7f8a commit 5ac1bcf

11 files changed

+82
-52
lines changed

test/test_analysis.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from db.python.utils import GenericFilter
1010
from models.enums import AnalysisStatus
1111
from models.models import (
12+
PRIMARY_EXTERNAL_ORG,
1213
AnalysisInternal,
1314
AssayUpsertInternal,
1415
SampleUpsertInternal,
@@ -30,7 +31,7 @@ async def setUp(self) -> None:
3031

3132
sample = await self.sl.upsert_sample(
3233
SampleUpsertInternal(
33-
external_id='Test01',
34+
external_ids={PRIMARY_EXTERNAL_ORG: 'Test01'},
3435
type='blood',
3536
meta={'meta': 'meta ;)'},
3637
active=True,

test/test_assay.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
from db.python.layers.sample import SampleLayer
88
from db.python.tables.assay import AssayFilter
99
from db.python.utils import GenericFilter, NotFoundError
10-
from models.models.assay import AssayUpsertInternal
11-
from models.models.sample import SampleUpsertInternal
10+
from models.models import (
11+
PRIMARY_EXTERNAL_ORG,
12+
AssayUpsertInternal,
13+
SampleUpsertInternal,
14+
)
1215

1316
default_sequencing_meta = {
1417
'sequencing_type': 'genome',
@@ -34,7 +37,7 @@ async def setUp(self) -> None:
3437
self.sample_id_raw = (
3538
await self.slayer.upsert_sample(
3639
SampleUpsertInternal(
37-
external_id=self.external_sample_id,
40+
external_ids={PRIMARY_EXTERNAL_ORG: self.external_sample_id},
3841
type='blood',
3942
active=True,
4043
meta={'Testing': 'test_assay'},
@@ -221,7 +224,7 @@ async def test_query(self):
221224
"""Test query_assays in different combinations"""
222225
sample = await self.slayer.upsert_sample(
223226
SampleUpsertInternal(
224-
external_id='SAM_TEST_QUERY',
227+
external_ids={PRIMARY_EXTERNAL_ORG: 'SAM_TEST_QUERY'},
225228
type='blood',
226229
active=True,
227230
meta={'collection-year': '2022'},

test/test_audit_log.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from test.testbase import DbIsolatedTest, run_as_sync
22

33
from db.python.layers.sample import SampleLayer
4-
from models.models.sample import SampleUpsertInternal
4+
from models.models import PRIMARY_EXTERNAL_ORG, SampleUpsertInternal
55

66

77
class TestChangelog(DbIsolatedTest):
@@ -16,7 +16,7 @@ async def test_insert_sample(self):
1616
slayer = SampleLayer(self.connection)
1717
sample = await slayer.upsert_sample(
1818
SampleUpsertInternal(
19-
external_id='Test01',
19+
external_ids={PRIMARY_EXTERNAL_ORG: 'Test01'},
2020
type='blood',
2121
active=True,
2222
meta={'meta': 'meta ;)'},

test/test_cohort.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
from db.python.layers import CohortLayer, SampleLayer
77
from db.python.tables.cohort import CohortFilter
88
from db.python.utils import GenericFilter
9-
from models.models import SampleUpsertInternal, SequencingGroupUpsertInternal
9+
from models.models import (
10+
PRIMARY_EXTERNAL_ORG,
11+
SampleUpsertInternal,
12+
SequencingGroupUpsertInternal,
13+
)
1014
from models.models.cohort import (
1115
CohortCriteria,
1216
CohortCriteriaInternal,
@@ -198,7 +202,7 @@ def get_sample_model(
198202
"""Create a minimal sample"""
199203
return SampleUpsertInternal(
200204
meta={},
201-
external_id=f'EXID{eid}',
205+
external_ids={PRIMARY_EXTERNAL_ORG: f'EXID{eid}'},
202206
type=s_type,
203207
sequencing_groups=[
204208
SequencingGroupUpsertInternal(

test/test_get_participants.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from test.testbase import DbIsolatedTest, run_as_sync
22

33
from db.python.layers.participant import ParticipantLayer
4-
from models.models.participant import ParticipantUpsertInternal
4+
from models.models import PRIMARY_EXTERNAL_ORG, ParticipantUpsertInternal
55

66

77
class TestParticipant(DbIsolatedTest):
@@ -11,17 +11,20 @@ class TestParticipant(DbIsolatedTest):
1111
async def setUp(self) -> None:
1212
super().setUp()
1313

14+
self.ex01 = {PRIMARY_EXTERNAL_ORG: 'EX01', 'other': 'OTHER1'}
15+
self.ex02 = {PRIMARY_EXTERNAL_ORG: 'EX02'}
16+
1417
pl = ParticipantLayer(self.connection)
1518
await pl.upsert_participants(
1619
[
1720
ParticipantUpsertInternal(
18-
external_id='EX01',
21+
external_ids=self.ex01,
1922
reported_sex=2,
2023
karyotype='XX',
2124
meta={'field': 1},
2225
),
2326
ParticipantUpsertInternal(
24-
external_id='EX02',
27+
external_ids=self.ex02,
2528
reported_sex=1,
2629
karyotype='XY',
2730
meta={'field': 2},
@@ -37,11 +40,11 @@ async def test_get_all_participants(self):
3740

3841
self.assertEqual(2, len(ps))
3942

40-
self.assertEqual('EX01', ps[0].external_id)
43+
self.assertEqual(self.ex01, ps[0].external_ids)
4144
self.assertEqual(1, ps[0].meta['field'])
4245
self.assertEqual('XX', ps[0].karyotype)
4346

44-
self.assertEqual('EX02', ps[1].external_id)
47+
self.assertEqual(self.ex02, ps[1].external_ids)
4548

4649
@run_as_sync
4750
async def test_get_participant_by_eid(self):
@@ -51,6 +54,6 @@ async def test_get_participant_by_eid(self):
5154

5255
self.assertEqual(1, len(ps))
5356

54-
self.assertEqual('EX02', ps[0].external_id)
57+
self.assertEqual(self.ex02, ps[0].external_ids)
5558
self.assertEqual(2, ps[0].meta['field'])
5659
self.assertEqual('XY', ps[0].karyotype)

test/test_pedigree.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from db.python.layers.family import FamilyLayer
44
from db.python.layers.participant import ParticipantLayer
5-
from models.models.participant import ParticipantUpsertInternal
5+
from models.models import PRIMARY_EXTERNAL_ORG, ParticipantUpsertInternal
66

77

88
class TestPedigree(DbIsolatedTest):
@@ -51,12 +51,12 @@ async def test_pedigree_without_family(self):
5151

5252
await pl.upsert_participant(
5353
ParticipantUpsertInternal(
54-
external_id='EX01',
54+
external_ids={PRIMARY_EXTERNAL_ORG: 'EX01'},
5555
reported_sex=1,
5656
)
5757
)
5858
await pl.upsert_participant(
59-
ParticipantUpsertInternal(external_id='EX02', reported_sex=None)
59+
ParticipantUpsertInternal(external_ids={PRIMARY_EXTERNAL_ORG: 'EX02'}, reported_sex=None)
6060
)
6161

6262
rows = await fl.get_pedigree(

test/test_sample.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from test.testbase import DbIsolatedTest, run_as_sync
22

33
from db.python.layers.sample import SampleLayer
4-
from models.models.sample import SampleUpsertInternal
4+
from models.models import PRIMARY_EXTERNAL_ORG, SampleUpsertInternal
55

66

77
class TestSample(DbIsolatedTest):
@@ -19,7 +19,7 @@ async def test_add_sample(self):
1919
"""Test inserting a sample"""
2020
sample = await self.slayer.upsert_sample(
2121
SampleUpsertInternal(
22-
external_id='Test01',
22+
external_ids={PRIMARY_EXTERNAL_ORG: 'Test01'},
2323
type='blood',
2424
active=True,
2525
meta={'meta': 'meta ;)'},
@@ -32,13 +32,16 @@ async def test_add_sample(self):
3232
self.assertEqual(1, len(samples))
3333
self.assertEqual(sample.id, samples[0]['id'])
3434

35+
mapping = await self.slayer.get_sample_id_map_by_external_ids(['Test01'])
36+
self.assertDictEqual({'Test01': sample.id}, mapping)
37+
3538
@run_as_sync
3639
async def test_get_sample(self):
3740
"""Test getting formed sample"""
3841
meta_dict = {'meta': 'meta ;)'}
3942
s = await self.slayer.upsert_sample(
4043
SampleUpsertInternal(
41-
external_id='Test01',
44+
external_ids={PRIMARY_EXTERNAL_ORG: 'Test01'},
4245
type='blood',
4346
active=True,
4447
meta=meta_dict,
@@ -56,21 +59,21 @@ async def test_update_sample(self):
5659
meta_dict = {'meta': 'meta ;)'}
5760
s = await self.slayer.upsert_sample(
5861
SampleUpsertInternal(
59-
external_id='Test01',
62+
external_ids={PRIMARY_EXTERNAL_ORG: 'Test01'},
6063
type='blood',
6164
active=True,
6265
meta=meta_dict,
6366
)
6467
)
6568

66-
new_external_id = 'Test02'
69+
new_external_id_dict = {PRIMARY_EXTERNAL_ORG: 'Test02'}
6770
await self.slayer.upsert_sample(
68-
SampleUpsertInternal(id=s.id, external_id=new_external_id)
71+
SampleUpsertInternal(id=s.id, external_ids=new_external_id_dict)
6972
)
7073

7174
sample = await self.slayer.get_by_id(s.id, check_project_id=False)
7275

73-
self.assertEqual(new_external_id, sample.external_id)
76+
self.assertDictEqual(new_external_id_dict, sample.external_ids)
7477

7578
# @run_as_sync
7679
# async def test_update_sample(self):

test/test_search.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from db.python.tables.family_participant import FamilyParticipantTable
99
from models.enums import SearchResponseType
1010
from models.models import (
11+
PRIMARY_EXTERNAL_ORG,
1112
AssayUpsertInternal,
1213
FamilySearchResponseData,
1314
ParticipantSearchResponseData,
@@ -52,7 +53,7 @@ async def test_search_unavailable_sample_by_internal_id(self):
5253
Mock this in testing by limiting scope to non-existent project IDs
5354
"""
5455
sample = await self.slayer.upsert_sample(
55-
SampleUpsertInternal(external_id='EX001', type='blood')
56+
SampleUpsertInternal(external_ids={PRIMARY_EXTERNAL_ORG: 'EX001'}, type='blood')
5657
)
5758
cpg_id = sample_id_format(sample.id)
5859

@@ -67,7 +68,7 @@ async def test_search_isolated_sample_by_id(self):
6768
Search by valid CPG sample ID (special case)
6869
"""
6970
sample = await self.slayer.upsert_sample(
70-
SampleUpsertInternal(external_id='EX001', type='blood')
71+
SampleUpsertInternal(external_ids={PRIMARY_EXTERNAL_ORG: 'EX001'}, type='blood')
7172
)
7273
cpg_id = sample_id_format(sample.id)
7374
results = await self.schlay.search(query=cpg_id, project_ids=[self.project_id])
@@ -87,7 +88,7 @@ async def test_search_isolated_sequencing_group_by_id(self):
8788
Search by valid CPG sequencing group ID (special case)
8889
"""
8990
sample = await self.slayer.upsert_sample(
90-
SampleUpsertInternal(external_id='EXS001', type='blood')
91+
SampleUpsertInternal(external_ids={PRIMARY_EXTERNAL_ORG: 'EXS001'}, type='blood')
9192
)
9293
sg = await self.sglayer.upsert_sequencing_groups(
9394
[
@@ -134,7 +135,7 @@ async def test_search_isolated_sample_by_external_id(self):
134135
should only return one result
135136
"""
136137
sample = await self.slayer.upsert_sample(
137-
SampleUpsertInternal(external_id='EX001', type='blood')
138+
SampleUpsertInternal(external_ids={PRIMARY_EXTERNAL_ORG: 'EX001'}, type='blood')
138139
)
139140
results = await self.schlay.search(query='EX001', project_ids=[self.project_id])
140141

@@ -159,7 +160,7 @@ async def test_search_participant_isolated(self):
159160
should only return one result
160161
"""
161162
p = await self.player.upsert_participant(
162-
ParticipantUpsertInternal(external_id='PART01')
163+
ParticipantUpsertInternal(external_ids={PRIMARY_EXTERNAL_ORG: 'PART01'})
163164
)
164165
results = await self.schlay.search(
165166
query='PART01', project_ids=[self.project_id]
@@ -197,7 +198,7 @@ async def test_search_mixed(self):
197198
fptable = FamilyParticipantTable(self.connection)
198199

199200
p = await self.player.upsert_participant(
200-
ParticipantUpsertInternal(external_id='X:PART01')
201+
ParticipantUpsertInternal(external_ids={PRIMARY_EXTERNAL_ORG: 'X:PART01'})
201202
)
202203
f_id = await self.flayer.create_family(external_id='X:FAM01')
203204
await fptable.create_rows(
@@ -216,7 +217,7 @@ async def test_search_mixed(self):
216217

217218
sample = await self.slayer.upsert_sample(
218219
SampleUpsertInternal(
219-
external_id='X:SAM001',
220+
external_ids={PRIMARY_EXTERNAL_ORG: 'X:SAM001'},
220221
type='blood',
221222
participant_id=p.id,
222223
)

test/test_sequencing_groups.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from db.python.utils import GenericFilter
77
from models.enums.analysis import AnalysisStatus
88
from models.models import (
9+
PRIMARY_EXTERNAL_ORG,
910
AnalysisInternal,
1011
AssayUpsertInternal,
1112
SampleUpsertInternal,
@@ -20,7 +21,7 @@ def get_sample_model():
2021
"""
2122
return SampleUpsertInternal(
2223
meta={},
23-
external_id='EX_ID',
24+
external_ids={PRIMARY_EXTERNAL_ORG: 'EX_ID'},
2425
sequencing_groups=[
2526
SequencingGroupUpsertInternal(
2627
type='genome',

0 commit comments

Comments
 (0)