Skip to content

Commit

Permalink
✅ Add tests for study resource
Browse files Browse the repository at this point in the history
  • Loading branch information
dankolbman committed Mar 16, 2018
1 parent 46b655d commit 868634b
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 5 deletions.
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def entities(client):

# Add kf_ids
inputs['kf_ids'] = {'/participants': p.kf_id}
inputs['kf_ids'].update({'/studies': study.kf_id})
inputs['kf_ids'].update({'/demographics': p.demographic.kf_id})
inputs['kf_ids'].update({'/diagnoses': diagnosis.kf_id})
inputs['kf_ids'].update({'/samples': sample.kf_id})
Expand Down
161 changes: 161 additions & 0 deletions tests/study/test_study_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import json

from flask import url_for

from dataservice.extensions import db
from dataservice.api.study.models import Study
from tests.utils import FlaskTestCase

STUDY_URL = 'api.studies'
STUDY_LIST_URL = 'api.studies_list'


class StudyTest(FlaskTestCase):
'''
Test study api endopoints
'''

def test_post_study(self):
'''
Test creating a new study
'''
response = self._make_study(external_id='TEST')
resp = json.loads(response.data.decode('utf-8'))

self.assertEqual(response.status_code, 201)
self._test_response_content(resp, 201)

self.assertIn('study', resp['_status']['message'])
self.assertIn('created', resp['_status']['message'])
self.assertNotIn('_id', resp['results'])

s = Study.query.first()
study = resp['results']
self.assertEqual(s.kf_id, study['kf_id'])
self.assertEqual(s.external_id, study['external_id'])

def test_get_study(self):
'''
Test retrieving a study by id
'''
resp = self._make_study('TEST')
resp = json.loads(resp.data.decode('utf-8'))
kf_id = resp['results']['kf_id']

response = self.client.get(url_for(STUDY_URL,
kf_id=kf_id),
headers=self._api_headers())
resp = json.loads(response.data.decode('utf-8'))
self.assertEqual(response.status_code, 200)
self._test_response_content(resp, 200)

study = resp['results']
self.assertEqual(kf_id, study['kf_id'])

def test_patch_study(self):
'''
Test updating an existing study
'''
response = self._make_study(external_id='TEST')
resp = json.loads(response.data.decode('utf-8'))
study = resp['results']
kf_id = study.get('kf_id')
external_id = study.get('external_id')

# Update the study via http api
body = {
'external_id': 'new_id'
}
response = self.client.patch(url_for(STUDY_URL,
kf_id=kf_id),
headers=self._api_headers(),
data=json.dumps(body))
self.assertEqual(response.status_code, 200)

self.assertEqual(Study.query.get(kf_id).external_id,
body['external_id'])

resp = json.loads(response.data.decode('utf-8'))
self._test_response_content(resp, 200)
self.assertIn('study', resp['_status']['message'])
self.assertIn('updated', resp['_status']['message'])

study = resp['results']
self.assertEqual(study['kf_id'], kf_id)
self.assertEqual(study['external_id'], body['external_id'])

def test_patch_study_no_required_field(self):
'''
Test that we may update the study without a required field
'''
response = self._make_study(external_id='TEST')
resp = json.loads(response.data.decode('utf-8'))
study = resp['results']
kf_id = study.get('kf_id')
external_id = study.get('external_id')

# Update the study via http api
body = {
'version': '2.0'
}
response = self.client.patch(url_for(STUDY_URL,
kf_id=kf_id),
headers=self._api_headers(),
data=json.dumps(body))
self.assertEqual(response.status_code, 200)

self.assertEqual(Study.query.get(kf_id).version, '2.0')

resp = json.loads(response.data.decode('utf-8'))
self._test_response_content(resp, 200)
self.assertIn('study', resp['_status']['message'])
self.assertIn('updated', resp['_status']['message'])

study = resp['results']
self.assertEqual(study['kf_id'], kf_id)
self.assertEqual(study['external_id'], external_id)
self.assertEqual(study['version'], body['version'])

def test_delete_study(self):
'''
Test deleting a study by id
'''
resp = self._make_study('TEST')
resp = json.loads(resp.data.decode('utf-8'))
kf_id = resp['results']['kf_id']

response = self.client.delete(url_for(STUDY_URL,
kf_id=kf_id),
headers=self._api_headers())

resp = json.loads(response.data.decode('utf-8'))
self.assertEqual(response.status_code, 200)

response = self.client.get(url_for(STUDY_URL,
kf_id=kf_id),
headers=self._api_headers())

resp = json.loads(response.data.decode('utf-8'))
self.assertEqual(response.status_code, 404)

def _make_study(self, external_id='TEST-0001'):
'''
Convenience method to create a study with a given source name
'''
body = {
'external_id': external_id,
'version': '1.0'
}
response = self.client.post(url_for(STUDY_LIST_URL),
headers=self._api_headers(),
data=json.dumps(body))
return response

def _test_response_content(self, resp, status_code):
'''
Test that response body has expected fields
'''
self.assertIn('results', resp)
self.assertIn('_status', resp)
self.assertIn('message', resp['_status'])
self.assertEqual(resp['_status']['code'], status_code)
20 changes: 15 additions & 5 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class TestAPI:
('/demographics/123', 'GET', 404),
('/participants', 'GET', 200),
('/persons', 'GET', 404),
('/participants/123', 'GET', 404)
('/participants/123', 'GET', 404),
('/studies', 'GET', 200),
('/studies/123', 'GET', 404)
])
def test_status_codes(self, client, endpoint, method, status_code):
""" Test endpoint response codes """
Expand Down Expand Up @@ -47,7 +49,11 @@ def test_status_codes(self, client, endpoint, method, status_code):
('/participants', 'GET', 'success'),
('/participants/123', 'GET', 'could not find participant `123`'),
('/participants/123', 'PATCH', 'could not find participant `123`'),
('/participants/123', 'DELETE', 'could not find participant `123`')
('/participants/123', 'DELETE', 'could not find participant `123`'),
('/studies', 'GET', 'success'),
('/studies/123', 'GET', 'could not find study `123`'),
('/studies/123', 'PATCH', 'could not find study `123`'),
('/studies/123', 'DELETE', 'could not find study `123`'),
])
def test_status_messages(self, client, endpoint, method, status_message):
"""
Expand All @@ -63,7 +69,8 @@ def test_status_messages(self, client, endpoint, method, status_message):
('/participants', 'GET'),
('/samples', 'GET'),
('/diagnoses', 'GET'),
('/demographics', 'GET')
('/demographics', 'GET'),
('/studies', 'GET')
])
def test_status_format(self, client, endpoint, method):
""" Test that the _response field is consistent """
Expand All @@ -80,7 +87,9 @@ def test_status_format(self, client, endpoint, method):
('/participants', 'PATCH', ['created_at', 'modified_at']),
('/demographics', 'PATCH', ['created_at', 'modified_at']),
('/diagnoses', 'PATCH', ['created_at', 'modified_at']),
('/samples', 'PATCH', ['created_at', 'modified_at'])
('/samples', 'PATCH', ['created_at', 'modified_at']),
('/studies', 'PATCH', ['created_at', 'modified_at']),
('/studies', 'PATCH', ['created_at', 'modified_at'])
])
def test_read_only(self, client, entities, endpoint, method, fields):
""" Test that given fields can not be written or modified """
Expand All @@ -103,7 +112,8 @@ def test_read_only(self, client, entities, endpoint, method, fields):
@pytest.mark.parametrize('endpoint', ['/participants',
'/demographics',
'/diagnoses',
'/samples'])
'/samples',
'/studies'])
def test_unknown_field(self, client, entities, endpoint, method):
""" Test that unknown fields are rejected when trying to create """
inputs = entities[endpoint]
Expand Down
12 changes: 12 additions & 0 deletions tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dateutil import parser

from dataservice.extensions import db
from dataservice.api.study.models import Study
from dataservice.api.participant.models import Participant
from dataservice.api.demographic.models import Demographic
from dataservice.api.sample.models import Sample
Expand All @@ -17,6 +18,13 @@ class TestPagination:

@pytest.fixture(scope='module')
def participants(client):

# Add a bunch of studies for pagination
for _ in range(101):
s = Study(external_id='blah')
db.session.add(s)
db.session.commit()

s = Study(external_id='blah', name='test')
db.session.add(s)
db.session.flush()
Expand All @@ -38,6 +46,7 @@ def participants(client):
('/demographics'),
('/samples'),
('/diagnoses'),
('/studies'),
])
def test_pagination(self, client, participants, endpoint):
""" Test pagination of resource """
Expand Down Expand Up @@ -69,6 +78,7 @@ def test_pagination(self, client, participants, endpoint):
('/demographics'),
('/samples'),
('/diagnoses'),
('/studies'),
])
def test_limit(self, client, participants, endpoint):
# Check that limit param operates correctly
Expand All @@ -92,6 +102,7 @@ def test_limit(self, client, participants, endpoint):
('/demographics'),
('/samples'),
('/diagnoses'),
('/studies'),
])
def test_after(self, client, participants, endpoint):
""" Test `after` offeset paramater """
Expand Down Expand Up @@ -120,6 +131,7 @@ def test_after(self, client, participants, endpoint):
('/demographics'),
('/samples'),
('/diagnoses'),
('/studies'),
])
def test_self(self, client, participants, endpoint):
""" Test that the self link gives the same page """
Expand Down

0 comments on commit 868634b

Please sign in to comment.