Skip to content

Commit a9c6c6e

Browse files
authored
Merge pull request #204 from MoseleyBioinformaticsLab/pull
Enables pulling a dictionary of entries in memory as compared to saving to disk
2 parents 481935d + 04c61ac commit a9c6c6e

5 files changed

Lines changed: 331 additions & 143 deletions

File tree

dev/test_main.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,20 @@ def test_rest(mocker, args: list, expected_output: str, print_output: bool):
108108
_test_output(mocker=mocker, args=args, expected_output=expected_output, print_output=print_output)
109109

110110

111-
@pt.fixture(name='output', params=['brite-entries.zip', 'brite-entries'])
111+
@pt.fixture(name='output', params=['brite-entries', 'brite-entries.zip'])
112112
def pull_output(request):
113113
output: str = request.param
114114
yield output
115-
if output == 'brite-entries.zip':
115+
if output == 'brite-entries.zip' and os.path.isfile(output):
116116
os.remove(output)
117117
else:
118118
sh.rmtree(output, ignore_errors=True)
119119
os.remove('pull-results.json')
120120

121121

122122
test_pull_data = [
123-
['--multi-process', '--n-workers=2'], ['--force-single-entry', '--multi-process', '--n-workers=2'], ['--force-single-entry']]
123+
['--print'], ['--print', '--multi-process'], ['--force-single-entry', '--multi-process', '--n-workers=2'],
124+
['--multi-process', '--n-workers=2'], ['--force-single-entry']]
124125

125126

126127
@pt.mark.parametrize('args', test_pull_data)
@@ -136,7 +137,7 @@ def test_pull(mocker, args: list, output: str):
136137
stdin_mock: mocker.MagicMock = mocker.patch('kegg_pull._utils.sys.stdin.read', return_value=stdin_mock)
137138
successful_entry_ids = ['br:br08005', 'br:br08902', 'br:br08431']
138139
# The expected output file names have underscores instead of colons in case testing on Windows.
139-
expected_output_files: list = [entry_id.replace(':', '_') for entry_id in successful_entry_ids]
140+
expected_output_files = [entry_id.replace(':', '_') for entry_id in successful_entry_ids]
140141
expected_pull_results = {
141142
'successful-entry-ids': successful_entry_ids,
142143
'failed-entry-ids': ['br:br03220', 'br:br03222'],
@@ -147,26 +148,31 @@ def test_pull(mocker, args: list, output: str):
147148
'num-total': 5,
148149
'percent-success': 60.0,
149150
'pull-minutes': 1.0}
150-
args: list = ['kegg_pull', 'pull', 'entry-ids', '-'] + args + [f'--output={output}']
151+
args = ['kegg_pull', 'pull', 'entry-ids', '-'] + args + [f'--output={output}']
151152
mocker.patch('sys.argv', args)
152153
time_mock: mocker.MagicMock = mocker.patch('kegg_pull.pull_cli._testable_time', side_effect=[30, 90])
154+
print_mock = mocker.patch('builtins.print')
153155
m.main()
154156
stdin_mock.assert_called_once_with()
155157
assert time_mock.call_count == 2
156158
# If running on Windows, change the actual files names to have underscores instead of colons.
157159
if os.name == 'nt': # pragma: no cover
158-
expected_output_files: list = expected_output_files[:-1] # The last brite gives different output on Windows
159-
successful_entry_ids: list = expected_output_files # pragma: no cover
160+
expected_output_files = expected_output_files[:-1] # The last brite gives different output on Windows
161+
successful_entry_ids = expected_output_files # pragma: no cover
160162
for successful_entry_id, expected_output_file in zip(successful_entry_ids, expected_output_files):
161-
if output.endswith('.zip'):
162-
with zf.ZipFile(output, 'r') as actual_zip:
163-
actual_entry: str = actual_zip.read(successful_entry_id + '.txt').decode()
164-
else:
165-
with open(f'{output}/{successful_entry_id}.txt') as actual_file:
166-
actual_entry: str = actual_file.read()
167163
with open(f'dev/test_data/brite-entries/{expected_output_file}.txt') as expected_file:
168164
expected_entry: str = expected_file.read()
169-
assert actual_entry == expected_entry
165+
if '--print' in args:
166+
print_mock.assert_any_call(successful_entry_id.replace('_', ':'))
167+
print_mock.assert_any_call(f'{expected_entry}\n')
168+
else:
169+
if output.endswith('.zip'):
170+
with zf.ZipFile(output, 'r') as actual_zip:
171+
actual_entry: str = actual_zip.read(successful_entry_id + '.txt').decode()
172+
else:
173+
with open(f'{output}/{successful_entry_id}.txt') as actual_file:
174+
actual_entry: str = actual_file.read()
175+
assert actual_entry == expected_entry
170176
with open('pull-results.json', 'r') as file:
171177
actual_pull_results: dict = json.load(file)
172178
assert actual_pull_results == expected_pull_results

0 commit comments

Comments
 (0)