16
16
from qiita_pet .test .rest .test_base import RESTHandlerTestCase
17
17
18
18
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' ]
32
33
return {i : {c : 1 for c in categories } for i in ids }
33
34
34
35
35
36
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
+
36
44
def test_patch_accept_new_categories (self ):
37
45
body = {'1.SKM1.999998' : {'dna_extracted' : 'foo' ,
38
46
'host_taxid' : 'foo' ,
@@ -131,10 +139,6 @@ def test_patch_accept_new_categories(self):
131
139
self .assertIn ('new_field1' , obs ['categories' ])
132
140
self .assertIn ('new_field2' , obs ['categories' ])
133
141
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
-
138
142
# remove a few existing fields, representing retired fields.
139
143
for sample_id in body :
140
144
del (body [sample_id ]['ph' ])
@@ -205,12 +209,11 @@ def test_patch_sample_ids_exist_incomplete_metadata(self):
205
209
self .assertEqual (obs , exp )
206
210
207
211
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 )
211
213
# If no new categories, both new and existing samples should succeed.
212
214
# 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 )
214
217
response = self .patch ('/api/v1/study/1/samples' , headers = self .headers ,
215
218
data = body , asjson = True )
216
219
self .assertEqual (response .code , 201 )
@@ -220,7 +223,8 @@ def test_patch_sample_ids_complete_metadata_and_unknown_metadata(self):
220
223
# If new categories are added, patch() should succeed.
221
224
# New and existing samples should have new categories.
222
225
# 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 )
224
228
# body['blank.a2']['DOES_NOT_EXIST'] will be '', not None.
225
229
# body['1.SKM8.640201']['WHAT'] will be '', not None.
226
230
body ['1.SKM8.640201' ]['DOES_NOT_EXIST' ] = 'foo'
@@ -242,43 +246,24 @@ def test_patch_sample_ids_complete_metadata_and_unknown_metadata(self):
242
246
obs = response .body .decode ("utf-8" ).replace ('NaN' , 'null' )
243
247
obs = json .loads (obs )
244
248
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 ])
277
260
278
261
# If categories were removed, both existing and new samples should
279
262
# fail.
280
263
# 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 )
282
267
del (body ['1.SKM8.640201' ]['env_biome' ])
283
268
del (body ['blank.a3' ]['env_biome' ])
284
269
@@ -290,7 +275,9 @@ def test_patch_sample_ids_complete_metadata_and_unknown_metadata(self):
290
275
self .assertEqual (obs , exp )
291
276
292
277
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 )
294
281
response = self .patch ('/api/v1/study/1/samples' , headers = self .headers ,
295
282
data = body , asjson = True )
296
283
self .assertEqual (response .code , 200 )
@@ -302,7 +289,8 @@ def test_patch_sample_ids_already_exist(self):
302
289
self .assertNotEqual (df .loc ['1.SKM4.640180' ]['elevation' ], '1' )
303
290
304
291
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 )
306
294
response = self .patch ('/api/v1/study/1/samples' , headers = self .headers ,
307
295
data = body , asjson = True )
308
296
self .assertEqual (response .code , 201 )
@@ -311,7 +299,10 @@ def test_patch_sample_ids_do_not_exist(self):
311
299
self .assertEqual (df .loc ['1.blank.a1' ]['elevation' ], '1' )
312
300
313
301
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 )
315
306
response = self .patch ('/api/v1/study/1/samples' , headers = self .headers ,
316
307
data = body , asjson = True )
317
308
self .assertEqual (response .code , 201 )
0 commit comments