Skip to content

Commit e66a5b3

Browse files
committed
Fixing tests
1 parent dee5763 commit e66a5b3

File tree

1 file changed

+63
-61
lines changed

1 file changed

+63
-61
lines changed

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ def _set_up(self):
351351
self.metadata_prefixed = pd.DataFrame.from_dict(metadata_prefixed_dict,
352352
orient='index')
353353

354-
self.test_raw_data = RawData(1)
355354
self.test_study = Study(1)
356355
self.data_type = "18S"
357356
self.data_type_id = 2
@@ -773,36 +772,20 @@ class TestPrepTemplateReadWrite(BaseTestPrepTemplate):
773772

774773
def setUp(self):
775774
self._set_up()
776-
fd, seqs_fp = mkstemp(suffix='_seqs.fastq')
777-
close(fd)
778-
fd, barcodes_fp = mkstemp(suffix='_barcodes.fastq')
779-
close(fd)
780-
filepaths = [(seqs_fp, 1), (barcodes_fp, 2)]
781-
with open(seqs_fp, "w") as f:
782-
f.write("\n")
783-
with open(barcodes_fp, "w") as f:
784-
f.write("\n")
785-
self.new_raw_data = RawData.create(2, [Study(1)], filepaths=filepaths)
786-
787-
db_test_raw_dir = join(get_db_files_base_dir(), 'raw_data')
788-
db_seqs_fp = join(db_test_raw_dir, "5_%s" % basename(seqs_fp))
789-
db_barcodes_fp = join(db_test_raw_dir, "5_%s" % basename(barcodes_fp))
790-
self._clean_up_files = [db_seqs_fp, db_barcodes_fp]
775+
self._clean_up_files = []
791776

792777
def test_create_duplicate_header(self):
793778
"""Create raises an error when duplicate headers are present"""
794779
self.metadata['STR_COLUMN'] = pd.Series(['', '', ''],
795780
index=self.metadata.index)
796781
with self.assertRaises(QiitaDBDuplicateHeaderError):
797-
PrepTemplate.create(self.metadata, self.new_raw_data,
798-
self.test_study, self.data_type)
782+
PrepTemplate.create(self.metadata, self.test_study, self.data_type)
799783

800784
def test_create_bad_sample_names(self):
801785
# set a horrible list of sample names
802786
self.metadata.index = ['o()xxxx[{::::::::>', 'sample.1', 'sample.3']
803787
with self.assertRaises(QiitaDBColumnError):
804-
PrepTemplate.create(self.metadata, self.new_raw_data,
805-
self.test_study, self.data_type)
788+
PrepTemplate.create(self.metadata, self.test_study, self.data_type)
806789

807790
def test_create_unknown_sample_names(self):
808791
# set two real and one fake sample name
@@ -812,19 +795,19 @@ def test_create_unknown_sample_names(self):
812795
orient='index')
813796
# Test error raised and correct error given
814797
with self.assertRaises(QiitaDBExecutionError) as err:
815-
PrepTemplate.create(self.metadata, self.new_raw_data,
816-
self.test_study, self.data_type)
798+
PrepTemplate.create(self.metadata, self.test_study, self.data_type)
817799
self.assertEqual(
818-
str(err.exception), 'Samples found in prep template but not sample'
819-
' template: 1.NOTREAL')
800+
str(err.exception),
801+
'Samples found in prep template but not sample template: 1.NOTREAL'
802+
)
820803

821804
def test_create_shorter_prep_template(self):
822805
# remove one sample so not all samples in the prep template
823806
del self.metadata_dict['SKB7.640196']
824807
self.metadata = pd.DataFrame.from_dict(self.metadata_dict,
825808
orient='index')
826-
pt = PrepTemplate.create(self.metadata, self.new_raw_data,
827-
self.test_study, self.data_type)
809+
pt = PrepTemplate.create(self.metadata, self.test_study,
810+
self.data_type)
828811

829812
# make sure the two samples were added correctly
830813
self.assertEqual(pt.id, 2)
@@ -875,8 +858,7 @@ def test_create_error_cleanup(self):
875858
exp_id = get_count("qiita.prep_template") + 1
876859

877860
with self.assertRaises(QiitaDBExecutionError):
878-
PrepTemplate.create(metadata, self.new_raw_data,
879-
self.test_study, self.data_type)
861+
PrepTemplate.create(metadata, self.test_study, self.data_type)
880862

881863
sql = """SELECT EXISTS(
882864
SELECT * FROM qiita.prep_template
@@ -893,6 +875,11 @@ def test_create_error_cleanup(self):
893875
WHERE prep_template_id=%s)"""
894876
self.assertFalse(self.conn_handler.execute_fetchone(sql, (exp_id,))[0])
895877

878+
sql = """SELECT EXISTS(
879+
SELECT * FROM qiita.study_prep_template
880+
WHERE prep_template_id=%s)"""
881+
self.assertFalse(self.conn_handler.execute_fetchone(sql, (exp_id,))[0])
882+
896883
self.assertFalse(exists_table("prep_%d" % exp_id, self.conn_handler))
897884

898885
def _common_creation_checks(self, new_id, pt, fp_count):
@@ -905,7 +892,13 @@ def _common_creation_checks(self, new_id, pt, fp_count):
905892
(new_id,))
906893
# prep_template_id, data_type_id, raw_data_id, preprocessing_status,
907894
# investigation_type
908-
self.assertEqual(obs, [[new_id, 2, 5, 'not_preprocessed', None]])
895+
self.assertEqual(obs, [[new_id, 2, None, 'not_preprocessed', None]])
896+
897+
# The prep template has been linked to the study
898+
obs = self.conn_handler.execute_fetchall(
899+
"SELECT * FROM qiita.study_prep_template "
900+
"WHERE prep_template_id=%s", (new_id,))
901+
self.assertEqual(obs, [[self.test_study.id, new_id]])
909902

910903
# The relevant rows to prep_template_sample have been added.
911904
obs = self.conn_handler.execute_fetchall(
@@ -991,17 +984,17 @@ def test_create(self):
991984
"""Creates a new PrepTemplate"""
992985
fp_count = get_count('qiita.filepath')
993986
new_id = get_count('qiita.prep_template') + 1
994-
pt = PrepTemplate.create(self.metadata, self.new_raw_data,
995-
self.test_study, self.data_type)
987+
pt = PrepTemplate.create(self.metadata, self.test_study,
988+
self.data_type)
996989
self._common_creation_checks(new_id, pt, fp_count)
997990

998991
def test_create_already_prefixed_samples(self):
999992
"""Creates a new PrepTemplate"""
1000993
fp_count = get_count('qiita.filepath')
1001994
new_id = get_count('qiita.prep_template') + 1
1002995
pt = npt.assert_warns(QiitaDBWarning, PrepTemplate.create,
1003-
self.metadata_prefixed, self.new_raw_data,
1004-
self.test_study, self.data_type)
996+
self.metadata_prefixed, self.test_study,
997+
self.data_type)
1005998
self._common_creation_checks(new_id, pt, fp_count)
1006999

10071000
def test_generate_files(self):
@@ -1032,8 +1025,8 @@ def test_create_data_type_id(self):
10321025
"""Creates a new PrepTemplate passing the data_type_id"""
10331026
fp_count = get_count('qiita.filepath')
10341027
new_id = get_count('qiita.prep_template') + 1
1035-
pt = PrepTemplate.create(self.metadata, self.new_raw_data,
1036-
self.test_study, self.data_type_id)
1028+
pt = PrepTemplate.create(self.metadata, self.test_study,
1029+
self.data_type_id)
10371030
self._common_creation_checks(new_id, pt, fp_count)
10381031

10391032
def test_create_warning(self):
@@ -1043,8 +1036,7 @@ def test_create_warning(self):
10431036
new_id = get_count('qiita.prep_template') + 1
10441037
del self.metadata['barcode']
10451038
pt = npt.assert_warns(QiitaDBWarning, PrepTemplate.create,
1046-
self.metadata, self.new_raw_data,
1047-
self.test_study, self.data_type)
1039+
self.metadata, self.test_study, self.data_type)
10481040

10491041
# The returned object has the correct id
10501042
self.assertEqual(pt.id, new_id)
@@ -1055,7 +1047,13 @@ def test_create_warning(self):
10551047
(new_id,))
10561048
# prep_template_id, data_type_id, raw_data_id, preprocessing_status,
10571049
# investigation_type
1058-
self.assertEqual(obs, [[new_id, 2, 5, 'not_preprocessed', None]])
1050+
self.assertEqual(obs, [[new_id, 2, None, 'not_preprocessed', None]])
1051+
1052+
# The prep template has been linked to the study
1053+
obs = self.conn_handler.execute_fetchall(
1054+
"SELECT * FROM qiita.study_prep_template "
1055+
"WHERE prep_template_id=%s", (new_id,))
1056+
self.assertEqual(obs, [[self.test_study.id, new_id]])
10591057

10601058
# The relevant rows to prep_template_sample have been added.
10611059
obs = self.conn_handler.execute_fetchall(
@@ -1136,9 +1134,8 @@ def test_create_warning(self):
11361134
def test_create_investigation_type_error(self):
11371135
"""Create raises an error if the investigation_type does not exists"""
11381136
with self.assertRaises(QiitaDBColumnError):
1139-
PrepTemplate.create(self.metadata, self.new_raw_data,
1140-
self.test_study, self.data_type_id,
1141-
'Not a term')
1137+
PrepTemplate.create(self.metadata, self.test_study,
1138+
self.data_type_id, 'Not a term')
11421139

11431140
def test_delete_error(self):
11441141
"""Try to delete a prep template that already has preprocessed data"""
@@ -1152,29 +1149,35 @@ def test_delete_unkonwn_id_error(self):
11521149

11531150
def test_delete(self):
11541151
"""Deletes prep template 2"""
1155-
pt = PrepTemplate.create(self.metadata, self.new_raw_data,
1156-
self.test_study, self.data_type_id)
1152+
pt = PrepTemplate.create(self.metadata, self.test_study,
1153+
self.data_type_id)
11571154
PrepTemplate.delete(pt.id)
11581155

11591156
obs = self.conn_handler.execute_fetchall(
1160-
"SELECT * FROM qiita.prep_template WHERE prep_template_id=2")
1157+
"SELECT * FROM qiita.prep_template WHERE prep_template_id=%s",
1158+
(pt.id,))
11611159
exp = []
11621160
self.assertEqual(obs, exp)
11631161

1162+
obs = self.conn_handler.execute_fetchall(
1163+
"SELECT * FROM qiita.study_prep_template "
1164+
"WHERE prep_template_id=%s", (pt.id,))
1165+
11641166
obs = self.conn_handler.execute_fetchall(
11651167
"SELECT * FROM qiita.prep_template_sample "
1166-
"WHERE prep_template_id=2")
1168+
"WHERE prep_template_id=%s", (pt.id,))
11671169
exp = []
11681170
self.assertEqual(obs, exp)
11691171

11701172
obs = self.conn_handler.execute_fetchall(
1171-
"SELECT * FROM qiita.prep_columns WHERE prep_template_id=2")
1173+
"SELECT * FROM qiita.prep_columns WHERE prep_template_id=%s",
1174+
(pt.id,))
11721175
exp = []
11731176
self.assertEqual(obs, exp)
11741177

11751178
with self.assertRaises(QiitaDBExecutionError):
11761179
self.conn_handler.execute_fetchall(
1177-
"SELECT * FROM qiita.prep_2")
1180+
"SELECT * FROM qiita.prep_%d" % pt.id)
11781181

11791182
def test_setitem(self):
11801183
"""setitem raises an error (currently not allowed)"""
@@ -1191,8 +1194,8 @@ def test_to_file(self):
11911194
"""to file writes a tab delimited file with all the metadata"""
11921195
fd, fp = mkstemp()
11931196
close(fd)
1194-
pt = PrepTemplate.create(self.metadata, self.new_raw_data,
1195-
self.test_study, self.data_type)
1197+
pt = PrepTemplate.create(self.metadata, self.test_study,
1198+
self.data_type)
11961199
pt.to_file(fp)
11971200
self._clean_up_files.append(fp)
11981201
with open(fp, 'U') as f:
@@ -1206,14 +1209,14 @@ def test_preprocessing_status(self):
12061209
self.assertEqual(pt.preprocessing_status, 'success')
12071210

12081211
# not preprocessed case
1209-
pt = PrepTemplate.create(self.metadata, self.new_raw_data,
1210-
self.test_study, self.data_type_id)
1212+
pt = PrepTemplate.create(self.metadata, self.test_study,
1213+
self.data_type_id)
12111214
self.assertEqual(pt.preprocessing_status, 'not_preprocessed')
12121215

12131216
def test_preprocessing_status_setter(self):
12141217
"""Able to update the preprocessing status"""
1215-
pt = PrepTemplate.create(self.metadata, self.new_raw_data,
1216-
self.test_study, self.data_type_id)
1218+
pt = PrepTemplate.create(self.metadata, self.test_study,
1219+
self.data_type_id)
12171220
self.assertEqual(pt.preprocessing_status, 'not_preprocessed')
12181221
pt.preprocessing_status = 'preprocessing'
12191222
self.assertEqual(pt.preprocessing_status, 'preprocessing')
@@ -1222,8 +1225,8 @@ def test_preprocessing_status_setter(self):
12221225

12231226
def test_preprocessing_status_setter_failed(self):
12241227
"""Able to update preprocessing_status with a failure message"""
1225-
pt = PrepTemplate.create(self.metadata, self.new_raw_data,
1226-
self.test_study, self.data_type_id)
1228+
pt = PrepTemplate.create(self.metadata, self.test_study,
1229+
self.data_type_id)
12271230
state = 'failed: some error message'
12281231
self.assertEqual(pt.preprocessing_status, 'not_preprocessed')
12291232
pt.preprocessing_status = state
@@ -1236,8 +1239,8 @@ def test_preprocessing_status_setter_valueerror(self):
12361239

12371240
def test_investigation_type_setter(self):
12381241
"""Able to update the investigation type"""
1239-
pt = PrepTemplate.create(self.metadata, self.new_raw_data,
1240-
self.test_study, self.data_type_id)
1242+
pt = PrepTemplate.create(self.metadata, self.test_study,
1243+
self.data_type_id)
12411244
self.assertEqual(pt.investigation_type, None)
12421245
pt.investigation_type = "Other"
12431246
self.assertEqual(pt.investigation_type, 'Other')
@@ -1261,8 +1264,8 @@ def test_status(self):
12611264

12621265
# New prep templates have the status to sandbox because there is no
12631266
# processed data associated with them
1264-
pt = PrepTemplate.create(self.metadata, self.new_raw_data,
1265-
self.test_study, self.data_type_id)
1267+
pt = PrepTemplate.create(self.metadata, self.test_study,
1268+
self.data_type_id)
12661269
self.assertEqual(pt.status, 'sandbox')
12671270

12681271
def test_update_category(self):
@@ -1308,8 +1311,7 @@ def test_check_restrictions(self):
13081311

13091312
del self.metadata['primer']
13101313
pt = npt.assert_warns(QiitaDBWarning, PrepTemplate.create,
1311-
self.metadata, self.new_raw_data,
1312-
self.test_study, self.data_type)
1314+
self.metadata, self.test_study, self.data_type)
13131315

13141316
obs = pt.check_restrictions(
13151317
[PREP_TEMPLATE_COLUMNS['EBI'],

0 commit comments

Comments
 (0)