Skip to content

Commit

Permalink
✅ Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
znatty22 committed Mar 15, 2018
1 parent 4c93511 commit 22ac7c1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 42 deletions.
12 changes: 3 additions & 9 deletions tests/diagnosis/test_diagnosis_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ def test_post(self):
"""
Test create a new diagnosis
"""
# Create study
study = Study(external_id='phs001')

# Create a participant
p = Participant(external_id='Test subject 0', is_proband=True,
study=study)
db.session.add(p)
db.session.commit()
kwargs = self._create_save_to_db()

# Create diagnosis data
kwargs = {
Expand All @@ -42,7 +35,7 @@ def test_post(self):
'age_at_event_days': 365,
'diagnosis_category': 'cancer',
'tumor_location': 'Brain',
'participant_id': p.kf_id
'participant_id': kwargs.get('participant_id')
}
# Send get request
response = self.client.post(url_for(DIAGNOSES_LIST_URL),
Expand All @@ -60,6 +53,7 @@ def test_post(self):
if k == 'participant_id':
continue
self.assertEqual(diagnosis[k], getattr(dg, k))
self.assertEqual(2, Diagnosis.query.count())

def test_post_missing_req_params(self):
"""
Expand Down
12 changes: 0 additions & 12 deletions tests/participant/test_participant_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def test_post_participant(self):
resp = json.loads(response.data.decode("utf-8"))

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

self.assertIn('participant', resp['_status']['message'])
self.assertIn('created', resp['_status']['message'])
Expand Down Expand Up @@ -77,7 +76,6 @@ def test_get_participant(self):
headers=self._api_headers())
resp = json.loads(response.data.decode("utf-8"))
self.assertEqual(response.status_code, 200)
self._test_response_content(resp, 200)

participant = resp['results']
p = Participant.query.first()
Expand Down Expand Up @@ -123,7 +121,6 @@ def test_patch_participant(self):

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

Expand Down Expand Up @@ -221,12 +218,3 @@ def _make_participant(self, external_id="TEST-0001"):
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)
13 changes: 3 additions & 10 deletions tests/sample/test_sample_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@ def test_post(self):
"""
Test create a new sample
"""
study = Study(external_id='phs001')
db.session.add(study)
db.session.commit()

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

# Create sample data
kwargs = {
Expand All @@ -43,7 +35,7 @@ def test_post(self):
'anatomical_site': 'Brain',
'age_at_event_days': 365,
'tumor_descriptor': 'Metastatic',
'participant_id': p.kf_id
'participant_id': kwargs.get('participant_id')
}
# Send post request
response = self.client.post(url_for(SAMPLES_LIST_URL),
Expand All @@ -59,6 +51,7 @@ def test_post(self):
for k, v in kwargs.items():
if k is not 'participant_id':
self.assertEqual(sample.get(k), v)
self.assertEqual(2, Sample.query.count())

def test_post_missing_req_params(self):
"""
Expand Down
18 changes: 7 additions & 11 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,15 @@ def test_read_only(self, client, entities, endpoint, method, fields):
assert (field not in body['results']
or body['results'][field] != 'test')

@pytest.mark.parametrize('endpoint, method, field', [
('/participants', 'POST', 'blah'),
('/participants', 'PATCH', 'blah'),
('/demographics', 'PATCH', 'blah'),
('/diagnoses', 'POST', 'blah'),
('/diagnoses', 'PATCH', 'blah'),
('/samples', 'POST', 'blah'),
('/samples', 'PATCH', 'blah')
])
def test_unknown_field(self, client, entities, endpoint, method, field):
@pytest.mark.parametrize('method', ['POST', 'PATCH'])
@pytest.mark.parametrize('endpoint', ['/participants',
'/demographics',
'/diagnoses',
'/samples'])
def test_unknown_field(self, client, entities, endpoint, method):
""" Test that unknown fields are rejected when trying to create """
inputs = entities[endpoint]
inputs.update({field: 'test'})
inputs.update({'blah': 'test'})
action = 'create'
if method.lower() in {'put', 'patch'}:
action = 'update'
Expand Down

0 comments on commit 22ac7c1

Please sign in to comment.