|
8 | 8 |
|
9 | 9 | from unittest import TestCase, main |
10 | 10 | import filecmp |
11 | | -from os import remove, close |
12 | | -from os.path import basename, exists, expanduser, join |
| 11 | +from os import remove, close, makedirs |
| 12 | +from os.path import basename, exists, expanduser, join, isdir |
13 | 13 | from tempfile import mkstemp |
14 | 14 | from json import dumps |
15 | 15 | import pandas as pd |
| 16 | +from shutil import rmtree |
16 | 17 |
|
17 | 18 | from qiita_client.qiita_client import (QiitaClient, _format_payload, |
18 | 19 | ArtifactInfo) |
@@ -110,7 +111,10 @@ def setUp(self): |
110 | 111 | def tearDown(self): |
111 | 112 | for fp in self.clean_up_files: |
112 | 113 | if exists(fp): |
113 | | - remove(fp) |
| 114 | + if isdir(fp): |
| 115 | + rmtree(fp) |
| 116 | + else: |
| 117 | + remove(fp) |
114 | 118 |
|
115 | 119 | def test_init(self): |
116 | 120 | obs = QiitaClient(URL, |
@@ -184,7 +188,7 @@ def test_patch(self): |
184 | 188 | with open(fp, 'w') as f: |
185 | 189 | f.write('\n') |
186 | 190 | self.clean_up_files.append(fp) |
187 | | - obs = self.tester.patch(f'/qiita_db/artifacts/{artifact_id}/', 'add', |
| 191 | + obs = self.tester.patch('/qiita_db/artifacts/%s/' % artifact_id, 'add', |
188 | 192 | '/html_summary/', value=fp) |
189 | 193 | self.assertIsNone(obs) |
190 | 194 |
|
@@ -440,6 +444,50 @@ def test_push_file_to_central(self): |
440 | 444 | fp_obs = self.tester.push_file_to_central(fp_source) |
441 | 445 | self.assertEqual(fp_source, fp_obs) |
442 | 446 |
|
| 447 | + def _create_test_dir(self, prefix=None): |
| 448 | + """Creates a test directory with files and subdirs.""" |
| 449 | + # prefix |
| 450 | + # |- testdir/ |
| 451 | + # |---- fileA.txt |
| 452 | + # |---- subdirA_l1/ |
| 453 | + # |-------- fileB.fna |
| 454 | + # |-------- subdirC_l2/ |
| 455 | + # |------------ fileC.log |
| 456 | + # |------------ fileD.seq |
| 457 | + # |---- subdirB_l1/ |
| 458 | + # |-------- fileE.sff |
| 459 | + if (prefix is not None) and (prefix != ""): |
| 460 | + prefix = join(prefix, 'testdir') |
| 461 | + else: |
| 462 | + prefix = 'testdir' |
| 463 | + |
| 464 | + for dir in [join(prefix, 'subdirA_l1', 'subdirC_l2'), |
| 465 | + join(prefix, 'subdirB_l1')]: |
| 466 | + if not exists(dir): |
| 467 | + makedirs(dir) |
| 468 | + for file, cont in [(join(prefix, 'fileA.txt'), 'contentA'), |
| 469 | + (join(prefix, 'subdirA_l1', |
| 470 | + 'fileB.fna'), 'this is B'), |
| 471 | + (join(prefix, 'subdirA_l1', 'subdirC_l2', |
| 472 | + 'fileC.log'), 'call me c'), |
| 473 | + (join(prefix, 'subdirA_l1', 'subdirC_l2', |
| 474 | + 'fileD.seq'), 'I d'), |
| 475 | + (join(prefix, 'subdirB_l1', 'fileE.sff'), 'oh e')]: |
| 476 | + with open(file, "w") as f: |
| 477 | + f.write(cont + "\n") |
| 478 | + self.clean_up_files.append(prefix) |
| 479 | + |
| 480 | + return prefix |
| 481 | + |
| 482 | + def test_push_file_to_central_dir(self): |
| 483 | + self.tester._plugincoupling = 'https' |
| 484 | + |
| 485 | + fp_source = self._create_test_dir('/tmp/test_push_dir/') |
| 486 | + fp_obs = self.tester.push_file_to_central(fp_source) |
| 487 | + self.assertEqual(fp_source, fp_obs) |
| 488 | + # As we don't necessarily know the QIITA_BASE_DIR, we cannot fetch one |
| 489 | + # of the files to double check for it's content |
| 490 | + |
443 | 491 |
|
444 | 492 | if __name__ == '__main__': |
445 | 493 | main() |
0 commit comments