forked from qiita-spots/qiita
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_util.py
1407 lines (1216 loc) · 59.5 KB
/
test_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
from unittest import TestCase, main
from tempfile import mkstemp, mkdtemp, NamedTemporaryFile, TemporaryFile
from os import close, remove, mkdir
from os.path import join, exists, basename
from shutil import rmtree
from datetime import datetime
from functools import partial
from string import punctuation
import h5py
from six import StringIO, BytesIO
import pandas as pd
from qiita_core.util import qiita_test_checker
import qiita_db as qdb
from matplotlib.figure import Figure
from matplotlib.axes import Axes
import matplotlib.pyplot as plt
@qiita_test_checker()
class DBUtilTestsBase(TestCase):
def setUp(self):
self.table = 'study'
self.required = [
'study_title', 'mixs_compliant',
'metadata_complete', 'study_description', 'first_contact',
'reprocess', 'timeseries_type_id', 'study_alias',
'study_abstract', 'principal_investigator_id', 'email']
self.files_to_remove = []
def tearDown(self):
for fp in self.files_to_remove:
if exists(fp):
remove(fp)
class DBUtilTests(DBUtilTestsBase):
def test_max_preparation_samples(self):
"""Test that we get the correct max_preparation_samples"""
obs = qdb.util.max_preparation_samples()
self.assertEqual(obs, 800)
def test_max_artifacts_in_workflow(self):
"""Test that we get the correct max_artifacts_in_workflow"""
obs = qdb.util.max_artifacts_in_workflow()
self.assertEqual(obs, 35)
def test_filepath_id_to_object_id(self):
# filepaths 1, 2 belongs to artifact 1
self.assertEqual(qdb.util.filepath_id_to_object_id(1), 1)
self.assertEqual(qdb.util.filepath_id_to_object_id(2), 1)
# filepaths 3, 4 belongs to artifact 2
self.assertEqual(qdb.util.filepath_id_to_object_id(3), 2)
self.assertEqual(qdb.util.filepath_id_to_object_id(4), 2)
# filepaths 9 belongs to artifact 4
self.assertEqual(qdb.util.filepath_id_to_object_id(9), 4)
# filepath 16 belongs to anlaysis 1
self.assertEqual(qdb.util.filepath_id_to_object_id(16), 1)
# filepath 18 belongs to study 1
self.assertIsNone(qdb.util.filepath_id_to_object_id(18))
# filepath 22 belongs to analysis/artifact 7
self.assertEqual(qdb.util.filepath_id_to_object_id(22), 7)
def test_check_required_columns(self):
# Doesn't do anything if correct info passed, only errors if wrong info
qdb.util.check_required_columns(self.required, self.table)
def test_check_required_columns_fail(self):
self.required.remove('study_title')
with self.assertRaises(qdb.exceptions.QiitaDBColumnError):
qdb.util.check_required_columns(self.required, self.table)
def test_check_table_cols(self):
# Doesn't do anything if correct info passed, only errors if wrong info
qdb.util.check_table_cols(self.required, self.table)
def test_check_table_cols_fail(self):
self.required.append('BADTHINGNOINHERE')
with self.assertRaises(qdb.exceptions.QiitaDBColumnError):
qdb.util.check_table_cols(self.required, self.table)
def test_get_table_cols(self):
obs = qdb.util.get_table_cols("qiita_user")
exp = {"email", "user_level_id", "password", "name", "affiliation",
"address", "phone", "user_verify_code", "pass_reset_code",
"pass_reset_timestamp", "receive_processing_job_emails",
"social_orcid", "social_researchgate", "social_googlescholar",
"creation_timestamp"}
self.assertEqual(set(obs), exp)
def test_exists_table(self):
"""Correctly checks if a table exists"""
# True cases
self.assertTrue(qdb.util.exists_table("filepath"))
self.assertTrue(qdb.util.exists_table("qiita_user"))
self.assertTrue(qdb.util.exists_table("analysis"))
self.assertTrue(qdb.util.exists_table("prep_1"))
self.assertTrue(qdb.util.exists_table("sample_1"))
# False cases
self.assertFalse(qdb.util.exists_table("sample_2"))
self.assertFalse(qdb.util.exists_table("prep_3"))
self.assertFalse(qdb.util.exists_table("foo_table"))
self.assertFalse(qdb.util.exists_table("bar_table"))
def test_convert_to_id(self):
"""Tests that ids are returned correctly"""
self.assertEqual(
qdb.util.convert_to_id("directory", "filepath_type"), 8)
self.assertEqual(
qdb.util.convert_to_id("private", "visibility", "visibility"), 3)
self.assertEqual(
qdb.util.convert_to_id("EMP", "portal_type", "portal"), 2)
def test_convert_to_id_bad_value(self):
"""Tests that ids are returned correctly"""
with self.assertRaises(qdb.exceptions.QiitaDBLookupError):
qdb.util.convert_to_id("FAKE", "filepath_type")
def test_get_artifact_types(self):
obs = qdb.util.get_artifact_types()
exp = {'SFF': 1, 'FASTA_Sanger': 2, 'FASTQ': 3, 'FASTA': 4,
'per_sample_FASTQ': 5, 'Demultiplexed': 6, 'BIOM': 7,
'beta_div_plots': 8, 'rarefaction_curves': 9,
'taxa_summary': 10}
self.assertEqual(obs, exp)
obs = qdb.util.get_artifact_types(key_by_id=True)
exp = {v: k for k, v in exp.items()}
self.assertEqual(obs, exp)
def test_get_filepath_types(self):
"""Tests that get_filepath_types works with valid arguments"""
obs = qdb.util.get_filepath_types()
exp = {'raw_forward_seqs': 1, 'raw_reverse_seqs': 2,
'raw_barcodes': 3, 'preprocessed_fasta': 4,
'preprocessed_fastq': 5, 'preprocessed_demux': 6, 'biom': 7,
'directory': 8, 'plain_text': 9, 'reference_seqs': 10,
'reference_tax': 11, 'reference_tree': 12, 'log': 13,
'sample_template': 14, 'prep_template': 15, 'qiime_map': 16,
'bam': 17
}
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add("SELECT filepath_type,filepath_type_id "
"FROM qiita.filepath_type")
exp = dict(qdb.sql_connection.TRN.execute_fetchindex())
self.assertEqual(obs, exp)
obs = qdb.util.get_filepath_types(key='filepath_type_id')
exp = {v: k for k, v in exp.items()}
self.assertEqual(obs, exp)
def test_get_filepath_types_fail(self):
"""Tests that get_Filetypes fails with invalid argument"""
with self.assertRaises(qdb.exceptions.QiitaDBColumnError):
qdb.util.get_filepath_types(key='invalid')
def test_get_data_types(self):
"""Tests that get_data_types works with valid arguments"""
obs = qdb.util.get_data_types()
exp = {'16S': 1, '18S': 2, 'ITS': 3, 'Proteomic': 4, 'Metabolomic': 5,
'Metagenomic': 6, 'Multiomic': 7, 'Metatranscriptomics': 8,
'Viromics': 9, 'Genomics': 10, 'Transcriptomics': 11,
'Job Output Folder': 12}
self.assertEqual(obs, exp)
obs = qdb.util.get_data_types(key='data_type_id')
exp = {v: k for k, v in exp.items()}
self.assertEqual(obs, exp)
def test_create_rand_string(self):
set_punct = set(punctuation)
obs = qdb.util.create_rand_string(200)
self.assertEqual(len(obs), 200)
self.assertTrue(set_punct.intersection(set(obs)))
obs = qdb.util.create_rand_string(400, punct=False)
self.assertEqual(len(obs), 400)
self.assertFalse(set_punct.intersection(set(obs)))
def test_get_count(self):
"""Checks that get_count retrieves proper count"""
self.assertEqual(qdb.util.get_count('qiita.study_person'), 3)
def test_check_count(self):
"""Checks that check_count returns True and False appropriately"""
self.assertTrue(qdb.util.check_count('qiita.study_person', 3))
self.assertFalse(qdb.util.check_count('qiita.study_person', 2))
def test_insert_filepaths(self):
fd, fp = mkstemp()
close(fd)
with open(fp, "w") as f:
f.write("\n")
self.files_to_remove.append(fp)
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add(
"SELECT last_value FROM qiita.filepath_filepath_id_seq")
exp_new_id = 1 + qdb.sql_connection.TRN.execute_fetchflatten()[0]
obs = qdb.util.insert_filepaths([(fp, 1)], 2, "raw_data")
self.assertEqual(obs, [exp_new_id])
# Check that the files have been copied correctly
exp_fp = join(qdb.util.get_db_files_base_dir(), "raw_data",
"2_%s" % basename(fp))
self.assertTrue(exists(exp_fp))
self.assertFalse(exists(fp))
self.files_to_remove.append(exp_fp)
# Check that the filepaths have been added to the DB
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add("SELECT * FROM qiita.filepath "
"WHERE filepath_id=%d" % exp_new_id)
obs = qdb.sql_connection.TRN.execute_fetchindex()
exp_fp = "2_%s" % basename(fp)
exp = [[exp_new_id, exp_fp, 1, '852952723', 1, 5, 1]]
self.assertEqual(obs, exp)
qdb.util.purge_filepaths()
def test_insert_filepaths_copy(self):
fd, fp = mkstemp()
close(fd)
with open(fp, "w") as f:
f.write("\n")
self.files_to_remove.append(fp)
# The id's in the database are bigserials, i.e. they get
# autoincremented for each element introduced.
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add(
"SELECT last_value FROM qiita.filepath_filepath_id_seq")
exp_new_id = 1 + qdb.sql_connection.TRN.execute_fetchflatten()[0]
obs = qdb.util.insert_filepaths([(fp, 1)], 2, "raw_data",
move_files=False, copy=True)
self.assertEqual(obs, [exp_new_id])
# Check that the files have been copied correctly
exp_fp = join(qdb.util.get_db_files_base_dir(), "raw_data",
"2_%s" % basename(fp))
self.assertTrue(exists(exp_fp))
self.assertTrue(exists(fp))
self.files_to_remove.append(exp_fp)
# Check that the filepaths have been added to the DB
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add("SELECT * FROM qiita.filepath "
"WHERE filepath_id=%d" % exp_new_id)
obs = qdb.sql_connection.TRN.execute_fetchindex()
exp_fp = "2_%s" % basename(fp)
exp = [[exp_new_id, exp_fp, 1, '852952723', 1, 5, 1]]
self.assertEqual(obs, exp)
# let's do that again but with move_files = True
exp_new_id += 1
obs = qdb.util.insert_filepaths([(fp, 1)], 2, "raw_data",
move_files=True, copy=True)
self.assertEqual(obs, [exp_new_id])
# Check that the files have been copied correctly
exp_fp = join(qdb.util.get_db_files_base_dir(), "raw_data",
"2_%s" % basename(fp))
self.assertTrue(exists(exp_fp))
self.assertTrue(exists(fp))
self.files_to_remove.append(exp_fp)
qdb.util.purge_filepaths()
def test_insert_filepaths_string(self):
fd, fp = mkstemp()
close(fd)
with open(fp, "w") as f:
f.write("\n")
self.files_to_remove.append(fp)
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add(
"SELECT last_value FROM qiita.filepath_filepath_id_seq")
exp_new_id = 1 + qdb.sql_connection.TRN.execute_fetchflatten()[0]
obs = qdb.util.insert_filepaths(
[(fp, "raw_forward_seqs")], 2, "raw_data")
self.assertEqual(obs, [exp_new_id])
# Check that the files have been copied correctly
exp_fp = join(qdb.util.get_db_files_base_dir(), "raw_data",
"2_%s" % basename(fp))
self.assertTrue(exists(exp_fp))
self.files_to_remove.append(exp_fp)
# Check that the filepaths have been added to the DB
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add("SELECT * FROM qiita.filepath "
"WHERE filepath_id=%d" % exp_new_id)
obs = qdb.sql_connection.TRN.execute_fetchindex()
exp_fp = "2_%s" % basename(fp)
exp = [[exp_new_id, exp_fp, 1, '852952723', 1, 5, 1]]
self.assertEqual(obs, exp)
qdb.util.purge_filepaths()
def test_retrieve_filepaths(self):
obs = qdb.util.retrieve_filepaths('artifact_filepath',
'artifact_id', 1)
path_builder = partial(
join, qdb.util.get_db_files_base_dir(), "raw_data")
exp = [{'fp_id': 1,
'fp': path_builder("1_s_G1_L001_sequences.fastq.gz"),
'fp_type': "raw_forward_seqs",
'checksum': '2125826711',
'fp_size': 58},
{'fp_id': 2,
'fp': path_builder("1_s_G1_L001_sequences_barcodes.fastq.gz"),
'fp_type': "raw_barcodes",
'checksum': '2125826711',
'fp_size': 58}]
self.assertEqual(obs, exp)
def test_retrieve_filepaths_sort(self):
obs = qdb.util.retrieve_filepaths(
'artifact_filepath', 'artifact_id', 1, sort='descending')
path_builder = partial(
join, qdb.util.get_db_files_base_dir(), "raw_data")
exp = [{'fp_id': 2,
'fp': path_builder("1_s_G1_L001_sequences_barcodes.fastq.gz"),
'fp_type': "raw_barcodes",
'checksum': '2125826711',
'fp_size': 58},
{'fp_id': 1,
'fp': path_builder("1_s_G1_L001_sequences.fastq.gz"),
'fp_type': "raw_forward_seqs",
'checksum': '2125826711',
'fp_size': 58}]
self.assertEqual(obs, exp)
def test_retrieve_filepaths_type(self):
obs = qdb.util.retrieve_filepaths(
'artifact_filepath', 'artifact_id', 1, sort='descending',
fp_type='raw_barcodes')
path_builder = partial(
join, qdb.util.get_db_files_base_dir(), "raw_data")
exp = [{'fp_id': 2,
'fp': path_builder("1_s_G1_L001_sequences_barcodes.fastq.gz"),
'fp_type': "raw_barcodes",
'checksum': '2125826711',
'fp_size': 58}]
self.assertEqual(obs, exp)
obs = qdb.util.retrieve_filepaths(
'artifact_filepath', 'artifact_id', 1, fp_type='raw_barcodes')
path_builder = partial(
join, qdb.util.get_db_files_base_dir(), "raw_data")
exp = [{'fp_id': 2,
'fp': path_builder("1_s_G1_L001_sequences_barcodes.fastq.gz"),
'fp_type': "raw_barcodes",
'checksum': '2125826711',
'fp_size': 58}]
self.assertEqual(obs, exp)
obs = qdb.util.retrieve_filepaths(
'artifact_filepath', 'artifact_id', 1, fp_type='biom')
path_builder = partial(
join, qdb.util.get_db_files_base_dir(), "raw_data")
self.assertEqual(obs, [])
def test_retrieve_filepaths_error(self):
with self.assertRaises(qdb.exceptions.QiitaDBError):
qdb.util.retrieve_filepaths('artifact_filepath', 'artifact_id', 1,
sort='Unknown')
def test_empty_trash_upload_folder(self):
# creating file to delete so we know it actually works
study_id = '1'
uploads_fp = join(qdb.util.get_mountpoint("uploads")[0][1], study_id)
trash = join(uploads_fp, 'trash')
if not exists(trash):
mkdir(trash)
fp = join(trash, 'my_file_to_delete.txt')
open(fp, 'w').close()
self.assertTrue(exists(fp))
qdb.util.empty_trash_upload_folder()
self.assertFalse(exists(fp))
def test_move_filepaths_to_upload_folder(self):
# we are going to test the move_filepaths_to_upload_folder indirectly
# by creating an artifact and deleting it. To accomplish this we need
# to create a new prep info file, attach a biom with html_summary and
# then delete it. However, we will do this twice to assure that
# there are no conflicts with this
study_id = 1
# creating the 2 sets of files for the 2 artifacts
fd, seqs_fp1 = mkstemp(suffix='_seqs.fastq')
close(fd)
html_fp1 = mkdtemp()
html_fp1 = join(html_fp1, 'support_files')
mkdir(html_fp1)
with open(join(html_fp1, 'index.html'), 'w') as fp:
fp.write(">AAA\nAAA")
fd, seqs_fp2 = mkstemp(suffix='_seqs.fastq')
close(fd)
html_fp2 = mkdtemp()
html_fp2 = join(html_fp2, 'support_files')
mkdir(html_fp2)
with open(join(html_fp2, 'index.html'), 'w') as fp:
fp.write(">AAA\nAAA")
# creating new prep info file
metadata_dict = {
'SKB8.640193': {'center_name': 'ANL',
'primer': 'GTGCCAGCMGCCGCGGTAA',
'barcode': 'GTCCGCAAGTTA',
'run_prefix': "s_G1_L001_sequences",
'platform': 'Illumina',
'instrument_model': 'Illumina MiSeq',
'library_construction_protocol': 'AAAA',
'experiment_design_description': 'BBBB'}}
metadata = pd.DataFrame.from_dict(
metadata_dict, orient='index', dtype=str)
pt1 = qdb.metadata_template.prep_template.PrepTemplate.create(
metadata, qdb.study.Study(study_id), "16S")
pt2 = qdb.metadata_template.prep_template.PrepTemplate.create(
metadata, qdb.study.Study(study_id), "16S")
# inserting artifact 1
artifact1 = qdb.artifact.Artifact.create(
[(seqs_fp1, 1), (html_fp1, 'html_summary')], "FASTQ",
prep_template=pt1)
filepaths = artifact1.filepaths
# inserting artifact 2
artifact2 = qdb.artifact.Artifact.create(
[(seqs_fp2, 1), (html_fp2, 'html_summary')], "FASTQ",
prep_template=pt2)
filepaths.extend(artifact2.filepaths)
# get before delete files in upload folders
GUPLOADS = qdb.util.get_files_from_uploads_folders
upload_files = set(GUPLOADS("1"))
# delete artifact 1
qdb.artifact.Artifact.delete(artifact1.id)
# confirm that _only_ the fastq from the file is recovered; this means
# that all the extra files/folders were ignored
diff_upload = set(GUPLOADS("1")) - set(upload_files)
self.assertEqual(len(diff_upload), 1)
self.assertEqual(diff_upload.pop()[1], basename(seqs_fp1))
# finish deleting artifacts :: there should be a new fastq
qdb.artifact.Artifact.delete(artifact2.id)
diff_upload = set(GUPLOADS("1")) - set(upload_files)
self.assertEqual(len(diff_upload), 2)
self.assertCountEqual(
[x[1] for x in diff_upload],
[basename(seqs_fp1), basename(seqs_fp2)])
# now let's create another artifact with the same filenames that
# artifact1 so we can test successfull overlapping of names
with open(seqs_fp1, 'w') as fp:
fp.write(">AAA\nAAA")
mkdir(html_fp1)
with open(join(html_fp1, 'index.html'), 'w') as fp:
fp.write(">AAA\nAAA")
artifact3 = qdb.artifact.Artifact.create(
[(seqs_fp1, 1), (html_fp1, 'html_summary')], "FASTQ",
prep_template=pt1)
filepaths.extend(artifact3.filepaths)
qdb.artifact.Artifact.delete(artifact3.id)
# files should be the same as the previous test
diff_upload = set(GUPLOADS("1")) - set(upload_files)
self.assertEqual(len(diff_upload), 2)
self.assertCountEqual(
[x[1] for x in diff_upload],
[basename(seqs_fp1), basename(seqs_fp2)])
bd = qdb.util.get_mountpoint("uploads")[0][1]
for x in filepaths:
self.files_to_remove.append(join(bd, "1", basename(x['fp'])))
def test_get_mountpoint(self):
exp = [(5, join(qdb.util.get_db_files_base_dir(), 'raw_data'))]
obs = qdb.util.get_mountpoint("raw_data")
self.assertEqual(obs, exp)
exp = [(1, join(qdb.util.get_db_files_base_dir(), 'analysis'))]
obs = qdb.util.get_mountpoint("analysis")
self.assertEqual(obs, exp)
exp = [(2, join(qdb.util.get_db_files_base_dir(), 'job'))]
obs = qdb.util.get_mountpoint("job")
self.assertEqual(obs, exp)
# inserting new ones so we can test that it retrieves these and
# doesn't alter other ones
qdb.sql_connection.perform_as_transaction(
"UPDATE qiita.data_directory SET active=false WHERE "
"data_directory_id=1")
count = qdb.util.get_count('qiita.data_directory')
sql = """INSERT INTO qiita.data_directory (data_type, mountpoint,
subdirectory, active)
VALUES ('analysis', 'analysis_tmp', true, true),
('raw_data', 'raw_data_tmp', true, false)"""
qdb.sql_connection.perform_as_transaction(sql)
# this should have been updated
exp = [(count + 1, join(qdb.util.get_db_files_base_dir(),
'analysis_tmp'))]
obs = qdb.util.get_mountpoint("analysis")
self.assertEqual(obs, exp)
# these 2 shouldn't
exp = [(5, join(qdb.util.get_db_files_base_dir(), 'raw_data'))]
obs = qdb.util.get_mountpoint("raw_data")
self.assertEqual(obs, exp)
exp = [(2, join(qdb.util.get_db_files_base_dir(), 'job'))]
obs = qdb.util.get_mountpoint("job")
self.assertEqual(obs, exp)
# testing multi returns
exp = [(5, join(qdb.util.get_db_files_base_dir(), 'raw_data')),
(count + 2, join(qdb.util.get_db_files_base_dir(),
'raw_data_tmp'))]
obs = qdb.util.get_mountpoint("raw_data", retrieve_all=True)
self.assertEqual(obs, exp)
# testing retrieve subdirectory
exp = [
(5, join(qdb.util.get_db_files_base_dir(), 'raw_data'), False),
(count + 2, join(qdb.util.get_db_files_base_dir(), 'raw_data_tmp'),
True)]
obs = qdb.util.get_mountpoint("raw_data", retrieve_all=True,
retrieve_subdir=True)
self.assertEqual(obs, exp)
def test_get_mountpoint_path_by_id(self):
exp = join(qdb.util.get_db_files_base_dir(), 'raw_data')
obs = qdb.util.get_mountpoint_path_by_id(5)
self.assertEqual(obs, exp)
exp = join(qdb.util.get_db_files_base_dir(), 'analysis')
obs = qdb.util.get_mountpoint_path_by_id(1)
self.assertEqual(obs, exp)
exp = join(qdb.util.get_db_files_base_dir(), 'job')
obs = qdb.util.get_mountpoint_path_by_id(2)
self.assertEqual(obs, exp)
# inserting new ones so we can test that it retrieves these and
# doesn't alter other ones
qdb.sql_connection.perform_as_transaction(
"UPDATE qiita.data_directory SET active=false WHERE "
"data_directory_id=1")
count = qdb.util.get_count('qiita.data_directory')
sql = """INSERT INTO qiita.data_directory (data_type, mountpoint,
subdirectory, active)
VALUES ('analysis', 'analysis_tmp', true, true),
('raw_data', 'raw_data_tmp', true, false)"""
qdb.sql_connection.perform_as_transaction(sql)
# this should have been updated
exp = join(qdb.util.get_db_files_base_dir(), 'analysis_tmp')
obs = qdb.util.get_mountpoint_path_by_id(count + 1)
self.assertEqual(obs, exp)
# these 2 shouldn't
exp = join(qdb.util.get_db_files_base_dir(), 'raw_data')
obs = qdb.util.get_mountpoint_path_by_id(5)
self.assertEqual(obs, exp)
exp = join(qdb.util.get_db_files_base_dir(), 'job')
obs = qdb.util.get_mountpoint_path_by_id(2)
self.assertEqual(obs, exp)
def test_get_files_from_uploads_folders(self):
# something has been uploaded and ignoring hidden files/folders
# and folders
exp = (7, 'uploaded_file.txt', '0B')
obs = qdb.util.get_files_from_uploads_folders("1")
self.assertIn(exp, obs)
# nothing has been uploaded
exp = []
obs = qdb.util.get_files_from_uploads_folders("2")
self.assertEqual(obs, exp)
def test_move_upload_files_to_trash(self):
test_filename = 'this_is_a_test_file.txt'
# create file to move to trash
fid, folder = qdb.util.get_mountpoint("uploads")[0]
test_fp = join(folder, '1', test_filename)
with open(test_fp, 'w') as f:
f.write('test')
self.files_to_remove.append(test_fp)
exp = (fid, 'this_is_a_test_file.txt', '4B')
obs = qdb.util.get_files_from_uploads_folders("1")
self.assertIn(exp, obs)
# move file
qdb.util.move_upload_files_to_trash(1, [(fid, test_filename)])
obs = qdb.util.get_files_from_uploads_folders("1")
self.assertNotIn(obs, exp)
# if the file doesn't exist, don't raise any errors
qdb.util.move_upload_files_to_trash(1, [(fid, test_filename)])
# testing errors
# - study doesn't exist
with self.assertRaises(qdb.exceptions.QiitaDBError):
qdb.util.move_upload_files_to_trash(100, [(fid, test_filename)])
# - fid doen't exist
with self.assertRaises(qdb.exceptions.QiitaDBError):
qdb.util.move_upload_files_to_trash(1, [(10, test_filename)])
# removing trash folder
rmtree(join(folder, '1', 'trash'))
def test_get_environmental_packages(self):
obs = qdb.util.get_environmental_packages()
exp = [['air', 'ep_air'],
['built environment', 'ep_built_environment'],
['host-associated', 'ep_host_associated'],
['human-amniotic-fluid', 'ep_human_amniotic_fluid'],
['human-associated', 'ep_human_associated'],
['human-blood', 'ep_human_blood'],
['human-gut', 'ep_human_gut'],
['human-oral', 'ep_human_oral'],
['human-skin', 'ep_human_skin'],
['human-urine', 'ep_human_urine'],
['human-vaginal', 'ep_human_vaginal'],
['microbial mat/biofilm', 'ep_microbial_mat_biofilm'],
['miscellaneous natural or artificial environment',
'ep_misc_artif'],
['plant-associated', 'ep_plant_associated'],
['sediment', 'ep_sediment'],
['soil', 'ep_soil'],
['wastewater/sludge', 'ep_wastewater_sludge'],
['water', 'ep_water']]
self.assertEqual(sorted(obs), sorted(exp))
def test_get_timeseries_types(self):
obs = qdb.util.get_timeseries_types()
exp = [[1, 'None', 'None'],
[2, 'real', 'single intervention'],
[3, 'real', 'multiple intervention'],
[4, 'real', 'combo intervention'],
[5, 'pseudo', 'single intervention'],
[6, 'pseudo', 'multiple intervention'],
[7, 'pseudo', 'combo intervention'],
[8, 'mixed', 'single intervention'],
[9, 'mixed', 'multiple intervention'],
[10, 'mixed', 'combo intervention']]
self.assertEqual(obs, exp)
def test_get_filepath_information(self):
obs = qdb.util.get_filepath_information(1)
# This path is machine specific. Just checking that is not empty
self.assertIsNotNone(obs.pop('fullpath'))
exp = {'filepath_id': 1, 'filepath': '1_s_G1_L001_sequences.fastq.gz',
'filepath_type': 'raw_forward_seqs', 'checksum': '2125826711',
'data_type': 'raw_data', 'mountpoint': 'raw_data',
'subdirectory': False, 'active': True}
self.assertEqual(obs, exp)
def test_filepath_id_to_rel_path(self):
obs = qdb.util.filepath_id_to_rel_path(1)
exp = 'raw_data/1_s_G1_L001_sequences.fastq.gz'
self.assertEqual(obs, exp)
obs = qdb.util.filepath_id_to_rel_path(3)
exp = 'preprocessed_data/1_seqs.fna'
self.assertEqual(obs, exp)
fd, fp = mkstemp()
close(fd)
with open(fp, 'w') as f:
f.write('\n')
self.files_to_remove.append(fp)
test = qdb.util.insert_filepaths(
[(fp, "raw_forward_seqs")], 2, "FASTQ")[0]
sql = """INSERT INTO qiita.artifact_filepath
(artifact_id, filepath_id)
VALUES (%s, %s)"""
qdb.sql_connection.perform_as_transaction(sql, [2, test])
obs = qdb.util.filepath_id_to_rel_path(test)
exp = 'FASTQ/2/%s' % basename(fp)
self.assertEqual(obs, exp)
def test_filepath_ids_to_rel_paths(self):
fd, fp = mkstemp()
close(fd)
with open(fp, 'w') as f:
f.write('\n')
self.files_to_remove.append(fp)
test = qdb.util.insert_filepaths(
[(fp, "raw_forward_seqs")], 2, "FASTQ")[0]
sql = """INSERT INTO qiita.artifact_filepath
(artifact_id, filepath_id)
VALUES (%s, %s)"""
qdb.sql_connection.perform_as_transaction(sql, [2, test])
obs = qdb.util.filepath_ids_to_rel_paths([1, 3, test])
exp = {1: 'raw_data/1_s_G1_L001_sequences.fastq.gz',
3: 'preprocessed_data/1_seqs.fna',
test: 'FASTQ/2/%s' % basename(fp)}
self.assertEqual(obs, exp)
def test_add_message(self):
count = qdb.util.get_count('qiita.message') + 1
user = qdb.user.User.create('[email protected]', 'password')
users = [user]
qdb.util.add_message("TEST MESSAGE", users)
obs = [[x[0], x[1]] for x in user.messages()]
exp = [[count, 'TEST MESSAGE']]
self.assertEqual(obs, exp)
def test_add_system_message(self):
count = qdb.util.get_count('qiita.message') + 1
qdb.util.add_system_message("SYS MESSAGE",
datetime(2015, 8, 5, 19, 41))
obs = [[x[0], x[1]]
for x in qdb.user.User('[email protected]').messages()]
exp = [[count, 'SYS MESSAGE'], [1, 'message 1']]
self.assertEqual(obs, exp)
obs = [[x[0], x[1]] for x in qdb.user.User('[email protected]').messages()]
exp = [[count, 'SYS MESSAGE']]
self.assertEqual(obs, exp)
sql = "SELECT expiration from qiita.message WHERE message_id = %s"
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add(sql, [count])
obs = qdb.sql_connection.TRN.execute_fetchindex()
exp = [[datetime(2015, 8, 5, 19, 41)]]
self.assertEqual(obs, exp)
def test_clear_system_messages(self):
message_id = qdb.util.get_count('qiita.message') + 1
user = qdb.user.User.create('[email protected]', 'password')
obs = [[x[0], x[1]] for x in user.messages()]
exp = []
self.assertEqual(obs, exp)
qdb.util.add_system_message("SYS MESSAGE",
datetime(2015, 8, 5, 19, 41))
obs = [[x[0], x[1]] for x in user.messages()]
exp = [[message_id, 'SYS MESSAGE']]
self.assertCountEqual(obs, exp)
qdb.util.clear_system_messages()
obs = [[x[0], x[1]] for x in user.messages()]
exp = []
self.assertEqual(obs, exp)
# Run again with no system messages to make sure no errors
qdb.util.clear_system_messages()
def test_supported_filepath_types(self):
obs = qdb.util.supported_filepath_types("FASTQ")
exp = [["raw_forward_seqs", True], ["raw_reverse_seqs", False],
["raw_barcodes", True]]
self.assertCountEqual(obs, exp)
obs = qdb.util.supported_filepath_types("BIOM")
exp = [["biom", True], ["directory", False], ["log", False]]
self.assertCountEqual(obs, exp)
def test_generate_analysis_list(self):
self.assertEqual(qdb.util.generate_analysis_list([]), [])
obs = qdb.util.generate_analysis_list([1, 2, 3, 5])
exp = [{'mapping_files': [
(16, qdb.util.get_filepath_information(16)['fullpath'])],
'description': 'A test analysis', 'artifacts': [8, 9], 'name':
'SomeAnalysis', 'analysis_id': 1, 'visibility': 'private'},
{'mapping_files': [], 'description': 'Another test analysis',
'artifacts': [], 'name': 'SomeSecondAnalysis',
'analysis_id': 2, 'visibility': 'private'}]
# removing timestamp for testing
for i in range(len(obs)):
del obs[i]['timestamp']
self.assertEqual(obs, exp)
self.assertEqual(
qdb.util.generate_analysis_list([1, 2, 3, 5], True), [])
@qiita_test_checker()
class UtilTests(TestCase):
"""Tests for the util functions that do not need to access the DB"""
def setUp(self):
fh, self.filepath = mkstemp()
close(fh)
with open(self.filepath, "w") as f:
f.write("Some text so we can actually compute a checksum")
def test_compute_checksum(self):
"""Correctly returns the file checksum"""
obs = qdb.util.compute_checksum(self.filepath)
exp = 1719580229
self.assertEqual(obs, exp)
def test_scrub_data_nothing(self):
"""Returns the same string without changes"""
self.assertEqual(qdb.util.scrub_data("nothing_changes"),
"nothing_changes")
def test_scrub_data_semicolon(self):
"""Correctly removes the semicolon from the string"""
self.assertEqual(qdb.util.scrub_data("remove_;_char"), "remove__char")
def test_scrub_data_single_quote(self):
"""Correctly removes single quotes from the string"""
self.assertEqual(qdb.util.scrub_data("'quotes'"), "quotes")
def test_get_visibilities(self):
obs = qdb.util.get_visibilities()
exp = ['awaiting_approval', 'sandbox', 'private', 'public', 'archived']
self.assertEqual(obs, exp)
def test_infer_status(self):
obs = qdb.util.infer_status([])
self.assertEqual(obs, 'sandbox')
obs = qdb.util.infer_status([['private']])
self.assertEqual(obs, 'private')
obs = qdb.util.infer_status([['private'], ['public']])
self.assertEqual(obs, 'public')
obs = qdb.util.infer_status([['sandbox'], ['awaiting_approval']])
self.assertEqual(obs, 'awaiting_approval')
obs = qdb.util.infer_status([['sandbox'], ['sandbox']])
self.assertEqual(obs, 'sandbox')
def test_get_pubmed_ids_from_dois(self):
exp = {'10.100/123456': '123456'}
obs = qdb.util.get_pubmed_ids_from_dois(['', '10.100/123456'])
self.assertEqual(obs, exp)
def test_generate_study_list(self):
USER = qdb.user.User
STUDY = qdb.study.Study
PREP = qdb.metadata_template.prep_template.PrepTemplate
UTIL = qdb.util
# testing owner email as name
user = USER('[email protected]')
username = user.info['name']
# test without changes
self.assertDictEqual(
STUDY_INFO, UTIL.generate_study_list(user, 'user')[0])
# change user's name to None and tests again
user.info = {'name': None}
exp = STUDY_INFO.copy()
exp['owner'] = '[email protected]'
self.assertDictEqual(
exp, qdb.util.generate_study_list(user, 'user')[0])
# returning original name
user.info = {'name': username}
# creating a new study to make sure that empty studies are also
# returned
info = {"timeseries_type_id": 1, "metadata_complete": True,
"mixs_compliant": True, "study_alias": "TST",
"study_description": "Some description of the study goes here",
"study_abstract": "Some abstract goes here",
"principal_investigator_id": qdb.study.StudyPerson(1),
"lab_person_id": qdb.study.StudyPerson(1)}
new_study = STUDY.create(
USER('[email protected]'), 'test_study_1', info=info)
snew_info = {
'study_title': 'test_study_1',
'metadata_complete': True, 'publication_pid': [],
'artifact_biom_ids': [], 'autoloaded': False,
'study_id': new_study.id, 'ebi_study_accession': None,
'owner': 'Shared', 'shared': [],
'study_abstract': 'Some abstract goes here',
'pi': ('[email protected]', 'LabDude'), 'publication_doi': [],
'study_alias': 'TST', 'study_tags': None,
'preparation_data_types': [], 'number_samples_collected': 0}
exp1 = [STUDY_INFO]
exp2 = [snew_info]
exp_both = [STUDY_INFO, snew_info]
# let's make sure that everything is private for study 1
for a in STUDY(1).artifacts():
a.visibility = 'private'
# owner of study
obs = UTIL.generate_study_list(USER('[email protected]'), 'user')
self.assertEqual(len(obs), 1)
self.assertDictEqual(obs[0], exp1[0])
# shared with
obs = UTIL.generate_study_list(USER('[email protected]'), 'user')
self.assertEqual(len(obs), 2)
self.assertDictEqual(obs[0], exp_both[0])
self.assertDictEqual(obs[1], exp_both[1])
# admin
obs = UTIL.generate_study_list(USER('[email protected]'), 'user')
self.assertEqual(obs, exp_both)
# no access/hidden
obs = UTIL.generate_study_list(USER('[email protected]'), 'user')
self.assertEqual(obs, [])
# public - none for everyone
obs = UTIL.generate_study_list(USER('[email protected]'), 'public')
self.assertEqual(obs, [])
obs = UTIL.generate_study_list(USER('[email protected]'), 'public')
self.assertEqual(obs, [])
obs = UTIL.generate_study_list(USER('[email protected]'), 'public')
self.assertEqual(obs, [])
obs = UTIL.generate_study_list(USER('[email protected]'), 'public')
self.assertEqual(obs, [])
def _avoid_duplicated_tests(all_artifacts=False):
# nothing should shange for owner, shared
obs = UTIL.generate_study_list(USER('[email protected]'), 'user')
self.assertEqual(obs, exp1)
obs = UTIL.generate_study_list(USER('[email protected]'), 'user')
self.assertEqual(obs, exp_both)
# for admin it should be shown in public and user cause there are
# 2 preps and only one is public
obs = UTIL.generate_study_list(USER('[email protected]'), 'user')
if not all_artifacts:
self.assertEqual(obs, exp_both)
else:
self.assertEqual(obs, exp2)
obs = UTIL.generate_study_list(USER('[email protected]'), 'user')
self.assertEqual(obs, [])
# for the public query, everything should be same for owner, share
# and admin but demo should now see it as public but with limited
# artifacts
obs = UTIL.generate_study_list(USER('[email protected]'), 'public')
self.assertEqual(obs, [])
obs = UTIL.generate_study_list(USER('[email protected]'), 'public')
self.assertEqual(obs, [])
obs = UTIL.generate_study_list(USER('[email protected]'), 'public')
if not all_artifacts:
exp1[0]['artifact_biom_ids'] = [7]
self.assertEqual(obs, exp1)
obs = UTIL.generate_study_list(USER('[email protected]'), 'public')
self.assertEqual(obs, exp1)
# returning artifacts
exp1[0]['artifact_biom_ids'] = [4, 5, 6, 7]
# make artifacts of prep 2 public
PREP(2).artifact.visibility = 'public'
_avoid_duplicated_tests()
# make artifacts of prep 1 awaiting_approval
PREP(1).artifact.visibility = 'awaiting_approval'
_avoid_duplicated_tests()
# making all studies public
PREP(1).artifact.visibility = 'public'
_avoid_duplicated_tests(True)
# deleting the new study study and returning artifact status
qdb.study.Study.delete(new_study.id)
PREP(1).artifact.visibility = 'private'
PREP(2).artifact.visibility = 'private'
def test_generate_study_list_errors(self):
with self.assertRaises(ValueError):
qdb.util.generate_study_list(qdb.user.User('[email protected]'), 'bad')
def test_generate_study_list_without_artifacts(self):
# creating a new study to make sure that empty studies are also
# returned
info = {"timeseries_type_id": 1, "metadata_complete": True,
"mixs_compliant": True, "study_alias": "TST",
"study_description": "Some description of the study goes here",
"study_abstract": "Some abstract goes here",
"principal_investigator_id": qdb.study.StudyPerson(1),
"lab_person_id": qdb.study.StudyPerson(1)}
new_study = qdb.study.Study.create(
qdb.user.User('[email protected]'), 'test_study_1', info=info)