Skip to content

Commit f34f856

Browse files
Fixed tests
Tests ran fine individually, but failed when ran as a whole suite because categories and samples added for one test would affect tests downstream.
1 parent d4e59d8 commit f34f856

File tree

2 files changed

+54
-63
lines changed

2 files changed

+54
-63
lines changed

qiita_pet/handlers/rest/study_samples.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ def patch(self, study_id):
168168

169169
categories = set(study.sample_template.categories)
170170

171-
if set(data.columns) != categories:
172-
if set(data.columns).issubset(categories):
173-
self.fail('Not all sample information categories provided',
174-
400)
175-
return
171+
if set(data.columns) != categories and set(data.columns).issubset(
172+
categories):
173+
self.fail('Not all sample information categories provided',
174+
400)
175+
return
176176

177177
existing_samples = set(sample_info.index)
178178
overlapping_ids = set(data.index).intersection(existing_samples)

qiita_pet/test/rest/test_study_samples.py

Lines changed: 49 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,31 @@
1616
from qiita_pet.test.rest.test_base import RESTHandlerTestCase
1717

1818

19-
def _sample_creator(ids):
20-
categories = ['season_environment', 'env_package',
21-
'assigned_from_geo', 'texture', 'taxon_id',
22-
'depth', 'host_taxid', 'common_name',
23-
'water_content_soil', 'elevation', 'temp',
24-
'tot_nitro', 'samp_salinity', 'altitude',
25-
'env_biome', 'country', 'ph', 'anonymized_name',
26-
'tot_org_carb', 'description_duplicate',
27-
'env_feature', 'physical_specimen_location',
28-
'physical_specimen_remaining', 'dna_extracted',
29-
'sample_type', 'collection_timestamp',
30-
'host_subject_id', 'description',
31-
'latitude', 'longitude', 'scientific_name']
19+
def _sample_creator(ids, categories=None):
20+
if categories is None:
21+
categories = ['season_environment', 'env_package',
22+
'assigned_from_geo', 'texture', 'taxon_id',
23+
'depth', 'host_taxid', 'common_name',
24+
'water_content_soil', 'elevation', 'temp',
25+
'tot_nitro', 'samp_salinity', 'altitude',
26+
'env_biome', 'country', 'ph', 'anonymized_name',
27+
'tot_org_carb', 'description_duplicate',
28+
'env_feature', 'physical_specimen_location',
29+
'physical_specimen_remaining', 'dna_extracted',
30+
'sample_type', 'collection_timestamp',
31+
'host_subject_id', 'description',
32+
'latitude', 'longitude', 'scientific_name']
3233
return {i: {c: 1 for c in categories} for i in ids}
3334

3435

3536
class StudySamplesHandlerTests(RESTHandlerTestCase):
37+
def _get_sample_categories(self, study_id):
38+
response = self.get('/api/v1/study/1/samples/info',
39+
headers=self.headers)
40+
self.assertEqual(response.code, 200)
41+
obs = json_decode(response.body)
42+
return obs['categories']
43+
3644
def test_patch_accept_new_categories(self):
3745
body = {'1.SKM1.999998': {'dna_extracted': 'foo',
3846
'host_taxid': 'foo',
@@ -131,10 +139,6 @@ def test_patch_accept_new_categories(self):
131139
self.assertIn('new_field1', obs['categories'])
132140
self.assertIn('new_field2', obs['categories'])
133141

134-
# TODO: need a test to get metadata for 1.SKM1.999998 and 1.SKM1.999999
135-
# and confirm new_field1 and new_field2 are empty for the former and
136-
# filled for the latter.
137-
138142
# remove a few existing fields, representing retired fields.
139143
for sample_id in body:
140144
del (body[sample_id]['ph'])
@@ -205,12 +209,11 @@ def test_patch_sample_ids_exist_incomplete_metadata(self):
205209
self.assertEqual(obs, exp)
206210

207211
def test_patch_sample_ids_complete_metadata_and_unknown_metadata(self):
208-
response = self.get('/api/v1/study/1/samples', headers=self.headers)
209-
self.assertEqual(response.code, 200)
210-
212+
current = self._get_sample_categories(1)
211213
# If no new categories, both new and existing samples should succeed.
212214
# 640201 is an existing sample. blank.a1 is a new sample
213-
body = _sample_creator(['1.SKM8.640201', 'blank.a1'])
215+
body = _sample_creator(['1.SKM8.640201',
216+
'blank.a1'], categories=current)
214217
response = self.patch('/api/v1/study/1/samples', headers=self.headers,
215218
data=body, asjson=True)
216219
self.assertEqual(response.code, 201)
@@ -220,7 +223,8 @@ def test_patch_sample_ids_complete_metadata_and_unknown_metadata(self):
220223
# If new categories are added, patch() should succeed.
221224
# New and existing samples should have new categories.
222225
# 640201 is an existing sample. blank.a2 is a new sample
223-
body = _sample_creator(['1.SKM8.640201', 'blank.a2'])
226+
body = _sample_creator(['1.SKM8.640201',
227+
'blank.a2'], categories=current)
224228
# body['blank.a2']['DOES_NOT_EXIST'] will be '', not None.
225229
# body['1.SKM8.640201']['WHAT'] will be '', not None.
226230
body['1.SKM8.640201']['DOES_NOT_EXIST'] = 'foo'
@@ -242,43 +246,24 @@ def test_patch_sample_ids_complete_metadata_and_unknown_metadata(self):
242246
obs = response.body.decode("utf-8").replace('NaN', 'null')
243247
obs = json.loads(obs)
244248

245-
exp = {'header': ['does_not_exist', 'what'],
246-
'samples': {'1.SKM7.640188': [None, None],
247-
'1.SKD9.640182': [None, None],
248-
'1.SKB8.640193': [None, None],
249-
'1.SKD2.640178': [None, None],
250-
'1.SKM3.640197': [None, None],
251-
'1.SKM4.640180': [None, None],
252-
'1.SKB9.640200': [None, None],
253-
'1.SKB4.640189': [None, None],
254-
'1.SKB5.640181': [None, None],
255-
'1.SKB6.640176': [None, None],
256-
'1.SKM2.640199': [None, None],
257-
'1.SKM5.640177': [None, None],
258-
'1.SKB1.640202': [None, None],
259-
'1.SKD8.640184': [None, None],
260-
'1.SKD4.640185': [None, None],
261-
'1.SKB3.640195': [None, None],
262-
'1.SKM1.640183': [None, None],
263-
'1.SKB7.640196': [None, None],
264-
'1.SKD3.640198': [None, None],
265-
'1.SKD7.640191': [None, None],
266-
'1.SKD6.640190': [None, None],
267-
'1.SKB2.640194': [None, None],
268-
'1.SKM9.640192': [None, None],
269-
'1.SKM6.640187': [None, None],
270-
'1.SKD5.640186': [None, None],
271-
'1.SKD1.640179': [None, None],
272-
'1.blank.a1': [None, None],
273-
'1.blank.a2': ['', 'bar'],
274-
'1.SKM8.640201': ['foo', '']}}
275-
276-
self.assertDictEqual(obs, exp)
249+
self.assertEqual(obs['header'], ['does_not_exist', 'what'])
250+
251+
self.assertEqual(obs['samples']['1.blank.a2'], ['', 'bar'])
252+
self.assertEqual(obs['samples']['1.SKM8.640201'], ['foo', ''])
253+
254+
# as the number and names of samples is dynamic, simply confirm the
255+
# other samples are unchanged.
256+
for sample in obs['samples']:
257+
if sample not in ['1.blank.a2', '1.SKM8.640201']:
258+
print(sample)
259+
self.assertEqual(obs['samples'][sample], [None, None])
277260

278261
# If categories were removed, both existing and new samples should
279262
# fail.
280263
# 640201 is an existing sample. blank.a3 is a new sample
281-
body = _sample_creator(['1.SKM8.640201', 'blank.a3'])
264+
current = self._get_sample_categories(1)
265+
body = _sample_creator(['1.SKM8.640201',
266+
'blank.a3'], categories=current)
282267
del (body['1.SKM8.640201']['env_biome'])
283268
del (body['blank.a3']['env_biome'])
284269

@@ -290,7 +275,9 @@ def test_patch_sample_ids_complete_metadata_and_unknown_metadata(self):
290275
self.assertEqual(obs, exp)
291276

292277
def test_patch_sample_ids_already_exist(self):
293-
body = _sample_creator(['1.SKM8.640201', '1.SKM3.640197'])
278+
current = self._get_sample_categories(1)
279+
body = _sample_creator(['1.SKM8.640201',
280+
'1.SKM3.640197'], categories=current)
294281
response = self.patch('/api/v1/study/1/samples', headers=self.headers,
295282
data=body, asjson=True)
296283
self.assertEqual(response.code, 200)
@@ -302,7 +289,8 @@ def test_patch_sample_ids_already_exist(self):
302289
self.assertNotEqual(df.loc['1.SKM4.640180']['elevation'], '1')
303290

304291
def test_patch_sample_ids_do_not_exist(self):
305-
body = _sample_creator(['blank.a1'])
292+
current = self._get_sample_categories(1)
293+
body = _sample_creator(['blank.a1'], categories=current)
306294
response = self.patch('/api/v1/study/1/samples', headers=self.headers,
307295
data=body, asjson=True)
308296
self.assertEqual(response.code, 201)
@@ -311,7 +299,10 @@ def test_patch_sample_ids_do_not_exist(self):
311299
self.assertEqual(df.loc['1.blank.a1']['elevation'], '1')
312300

313301
def test_patch_sample_ids_partially_exist(self):
314-
body = _sample_creator(['blank.b1', '1.SKM5.640177', '1.SKB9.640200'])
302+
current = self._get_sample_categories(1)
303+
body = _sample_creator(['blank.b1',
304+
'1.SKM5.640177',
305+
'1.SKB9.640200'], categories=current)
315306
response = self.patch('/api/v1/study/1/samples', headers=self.headers,
316307
data=body, asjson=True)
317308
self.assertEqual(response.code, 201)

0 commit comments

Comments
 (0)