Skip to content

Commit d42b534

Browse files
committed
a more versatile function
1 parent 816c4f3 commit d42b534

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

qiita_client/testing.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from unittest import TestCase
1010
from os import environ, sep
11-
from os.path import isabs, join, exists
11+
from os.path import join, relpath
1212
from time import sleep
1313

1414
from qiita_client import QiitaClient
@@ -84,29 +84,46 @@ def _wait_for_running_job(self, job_id):
8484

8585
return status
8686

87-
def _fix_plugincoupling_filepath(self, fp, BASE_DATA_DIR="/qiita_data/"):
88-
# In some plugin tests, example files are generated temporarily on
89-
# local tmp directories. This is fine for plugincoupling == filesystem
90-
# but will cause "File not found errors" when communication through
91-
# other protocols as the file actually has never been pushed to Qiita
92-
# main. This helper function shall fix this by copying the according
93-
# files over to Qiita main WHEN not using "filesystem" and fix the
94-
# according file paths, i.e. we need to prepend the BASE_DATA_DIR to
95-
# the filename, which should be '/qiita_data/' e.g. here
96-
# https://github.com/jlab/qiita-keycloak
97-
if not exists(fp):
98-
raise ValueError("Filepath does not exist!")
87+
def deposite_in_qiita_basedir(self, fps, update_fp_only=False):
88+
"""Pushs a file to qiita main AND adapts given filepath accordingly.
9989
90+
A helper function to fix file paths in tests such that they point to the
91+
expected BASE_DATA_DIR. This becomes necessary when uncoupling the
92+
plugin filesystem as some methods now actually fetches expected files
93+
from BASE_DATA_DIR. This will fail for protocols other than filesystem
94+
IF files are created locally by the plugin test.
95+
96+
Parameters
97+
----------
98+
fps : str or [str]
99+
Filepath or list of filepaths to file(s) that shall be part of
100+
BASE_DATA_DIR, but currently points to some tmp file for testing.
101+
update_fp_only : bool
102+
Some tests operate on filepaths only - files do not actually need to
103+
exist. Thus, we don't need to tranfer a file.
104+
"""
100105
if self.qclient._plugincoupling == 'filesystem':
101-
return fp
106+
return fps
107+
108+
# use artifact 1 info to determine BASA_DATA_DIR, as we know that the
109+
# filepath ends with ....raw_data/1_s_G1_L001_sequences.fastq.gz, thus
110+
# BASE_DATA_DIR must be the prefix, e.g. /qiita_data/
111+
# This might break IF file
112+
# qiita-spots/qiita/qiita_db/support_files/populate_test_db.sql
113+
# changes.
114+
ainfo = self.qclient.get('/qiita_db/artifacts/1/')
115+
base_data_dir = ainfo['files']['raw_forward_seqs'][0]['filepath'][
116+
:(-1 * len('raw_data/1_s_G1_L001_sequences.fastq.gz'))]
117+
if isinstance(fps, str):
118+
if not update_fp_only:
119+
self.qclient.push_file_to_central(fps)
120+
return join(base_data_dir, relpath(fps, sep))
121+
elif isinstance(fps, list):
122+
for fp in fps:
123+
if not update_fp_only:
124+
self.qclient.push_file_to_central(fp)
125+
return [join(base_data_dir, relpath(fp, sep)) for fp in fps]
102126
else:
103-
processed_fp = fp
104-
# chop off leading / for join to work properly when prepending
105-
# the BASE_DATA_DIR
106-
if isabs(processed_fp):
107-
processed_fp = processed_fp[len(sep):]
108-
processed_fp = join(BASE_DATA_DIR, processed_fp)
109-
# ensure file is transferred to qiita main
110-
self.qclient.push_file_to_central(fp)
111-
# return the filepath prepended with qiita main base_data_dir
112-
return processed_fp
127+
raise ValueError(
128+
"_deposite_in_qiita_basedir is not implemented for type %s"
129+
% type(fps))

0 commit comments

Comments
 (0)