Skip to content

Commit 7cf9fd6

Browse files
committed
Adding check_restrictions functionality and tests
1 parent 01ac199 commit 7cf9fd6

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,3 +1157,24 @@ def update_category(self, category, samples_and_values):
11571157
% (category, value_str, value_types_str, column_type))
11581158

11591159
raise e
1160+
1161+
def check_restrictions(self, restrictions):
1162+
"""Checks if the template fulfills the restrictions
1163+
1164+
Parameters
1165+
----------
1166+
restrictions : list of Restriction
1167+
The restrictions to test if the template fulfills
1168+
1169+
Returns
1170+
-------
1171+
set of str
1172+
The missing columns
1173+
"""
1174+
def _col_iter():
1175+
for restriction in restrictions:
1176+
for col in restriction.columns:
1177+
yield col
1178+
categories = self.categories()
1179+
missing = {col for col in _col_iter() if col not in categories}
1180+
return missing

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
get_count)
3333
from qiita_db.metadata_template.prep_template import PrepTemplate, PrepSample
3434
from qiita_db.metadata_template.sample_template import SampleTemplate, Sample
35-
from qiita_db.metadata_template.constants import PREP_TEMPLATE_COLUMNS
35+
from qiita_db.metadata_template.constants import (
36+
PREP_TEMPLATE_COLUMNS, PREP_TEMPLATE_COLUMNS_TARGET_GENE)
3637

3738

3839
class BaseTestPrepSample(TestCase):
@@ -1299,6 +1300,20 @@ def test_qiime_map_fp(self):
12991300
'1_prep_1_qiime_19700101-000000.txt')
13001301
self.assertEqual(pt.qiime_map_fp, exp)
13011302

1303+
def test_check_restrictions(self):
1304+
obs = self.tester.check_restrictions([PREP_TEMPLATE_COLUMNS['EBI']])
1305+
self.assertEqual(obs, set())
1306+
1307+
del self.metadata['primer']
1308+
pt = npt.assert_warns(QiitaDBWarning, PrepTemplate.create,
1309+
self.metadata, self.new_raw_data,
1310+
self.test_study, self.data_type)
1311+
1312+
obs = pt.check_restrictions(
1313+
[PREP_TEMPLATE_COLUMNS['EBI'],
1314+
PREP_TEMPLATE_COLUMNS_TARGET_GENE['demultiplex']])
1315+
self.assertEqual(obs, {'primer'})
1316+
13021317

13031318
EXP_PREP_TEMPLATE = (
13041319
'sample_name\tbarcode\tcenter_name\tcenter_project_name\t'

qiita_db/metadata_template/test/test_sample_template.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# -----------------------------------------------------------------------------
88

99
from future.builtins import zip
10+
from future.utils import viewvalues
1011
from unittest import TestCase, main
1112
from datetime import datetime
1213
from tempfile import mkstemp
@@ -1955,6 +1956,16 @@ def test_to_dataframe(self):
19551956
'anonymized_name', 'tot_org_carb', 'description_duplicate',
19561957
'env_feature'})
19571958

1959+
def test_check_restrictions(self):
1960+
obs = self.tester.check_restrictions([SAMPLE_TEMPLATE_COLUMNS['EBI']])
1961+
self.assertEqual(obs, set())
1962+
1963+
del self.metadata['collection_timestamp']
1964+
st = npt.assert_warns(QiitaDBWarning, SampleTemplate.create,
1965+
self.metadata, self.new_study)
1966+
obs = st.check_restrictions([SAMPLE_TEMPLATE_COLUMNS['EBI']])
1967+
self.assertEqual(obs, {'collection_timestamp'})
1968+
19581969
EXP_SAMPLE_TEMPLATE = (
19591970
"sample_name\tcollection_timestamp\tdescription\tdna_extracted"
19601971
"\thost_subject_id\tint_column\tlatitude\tlongitude"

0 commit comments

Comments
 (0)