Skip to content

Commit

Permalink
✅ Update all unit tests to support Study entity
Browse files Browse the repository at this point in the history
  • Loading branch information
znatty22 committed Feb 15, 2018
1 parent 5fdbea9 commit 920b87f
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 179 deletions.
12 changes: 10 additions & 2 deletions tests/aliquot/test_aliquot_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import uuid

from dataservice.extensions import db
from dataservice.api.study.models import Study
from dataservice.api.participant.models import Participant
from dataservice.api.sample.models import Sample
from dataservice.api.aliquot.models import Aliquot
Expand All @@ -20,6 +21,9 @@ def create_participant_sample_aliquot(self):
create a participant, sample, and aliquot save to db
returns participant_id, sample_id, and aliquot_id
"""
# Create study
study = Study(external_id='phs001')

dt = datetime.now()
participant_id = "Test_Subject_0"
sample_id = "Test_Sample_0"
Expand All @@ -45,7 +49,8 @@ def create_participant_sample_aliquot(self):
sample_0 = Sample(**sample_data, aliquots=[aliquot_0])
participant_0 = Participant(
external_id=participant_id,
samples=[sample_0])
samples=[sample_0],
study=study)
db.session.add(participant_0)
db.session.commit()
return participant_id, sample_id, aliquot_id
Expand All @@ -67,10 +72,13 @@ def test_create_aliquot(self):
"""
Test creation of aliquot
"""
# Create study
study = Study(external_id='phs001')

dt = datetime.now()
participant_id = "Test_Subject_0"
# creating participant
p = Participant(external_id=participant_id)
p = Participant(external_id=participant_id, study=study)
db.session.add(p)
db.session.commit()

Expand Down
33 changes: 15 additions & 18 deletions tests/demographic/test_demographic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from sqlalchemy.exc import IntegrityError

from dataservice.api.study.models import Study
from dataservice.api.participant.models import Participant
from dataservice.api.demographic.models import Demographic
from dataservice.extensions import db
Expand All @@ -18,10 +19,13 @@ def test_create(self):
"""
Test create demographic
"""
study = Study(external_id='phs001')
db.session.add(study)
db.session.commit()

# Create and save participant
participant_id = 'Test subject 0'
p = Participant(external_id=participant_id)
p = Participant(external_id=participant_id, study_id=study.kf_id)
db.session.add(p)
db.session.commit()

Expand Down Expand Up @@ -200,26 +204,13 @@ def test_get_participant_demographic(self):
"""
Test get demographic via the participant
"""
# Create demographic
data = {
'external_id': 'demo_1',
'race': 'asian'
}
d = Demographic(**data)

# Create and save participant
participant_id = 'Test subject 0'
p = Participant(external_id=participant_id, demographic=d)
db.session.add(p)
db.session.commit()
# Create and save demographic
participant_id, demo_id = self._create_save_demographic()

p = Participant.query.filter_by(external_id=participant_id).\
one_or_none()

p_demo = p.demographic

self.assertEqual(p_demo.external_id, d.external_id)
self.assertEqual(p_demo.race, d.race)
self.assertEqual(p.demographic.external_id, demo_id)

def _create_save_demographic(self):
"""
Expand All @@ -229,6 +220,11 @@ def _create_save_demographic(self):
Create a demographic and add it to participant as kwarg
Save participant
"""
# Create study
study = Study(external_id='phs001')
db.session.add(study)
db.session.commit()

# Create demographic
demo_id = 'demo_1'
data = {
Expand All @@ -241,7 +237,8 @@ def _create_save_demographic(self):

# Create and save participant
participant_id = 'Test subject 0'
p = Participant(external_id=participant_id, demographic=d)
p = Participant(external_id=participant_id, demographic=d,
study_id=study.kf_id)
db.session.add(p)
db.session.commit()

Expand Down
15 changes: 12 additions & 3 deletions tests/demographic/test_demographic_resources.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import json
from flask import url_for
from urllib.parse import urlparse
from pprint import pprint

from dataservice.extensions import db
from dataservice.api.common import id_service
from dataservice.api.demographic.models import Demographic
from dataservice.api.participant.models import Participant
from dataservice.api.study.models import Study
from tests.utils import FlaskTestCase

DEMOGRAPHICS_URL = 'api.demographics'
Expand All @@ -21,8 +21,12 @@ def test_post(self):
"""
Test create a new demographic
"""
study = Study(external_id='phs001')
db.session.add(study)
db.session.commit()

# Create a participant
p = Participant(external_id='Test subject 0')
p = Participant(external_id='Test subject 0', study_id=study.kf_id)
db.session.add(p)
db.session.commit()

Expand Down Expand Up @@ -339,6 +343,10 @@ def _create_save_to_db(self):
Create a demographic and add it to participant as kwarg
Save participant
"""
study = Study(external_id='phs001')
db.session.add(study)
db.session.commit()

# Create demographic
kwargs = {
'external_id': 'd1',
Expand All @@ -350,7 +358,8 @@ def _create_save_to_db(self):

# Create and save participant with demographic
participant_id = 'Test subject 0'
p = Participant(external_id=participant_id, demographic=d)
p = Participant(external_id=participant_id, demographic=d,
study_id=study.kf_id)
db.session.add(p)
db.session.commit()

Expand Down
Loading

0 comments on commit 920b87f

Please sign in to comment.