forked from nomadkaraoke/python-audio-separator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
371 lines (292 loc) · 15.5 KB
/
Copy pathtest_cli.py
File metadata and controls
371 lines (292 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import json
import pytest
import logging
from audio_separator.utils.cli import main
import subprocess
import importlib.metadata
from unittest import mock
from unittest.mock import patch, MagicMock, mock_open
# Mock metadata.distribution for tests to avoid PackageNotFoundError in environment without installed package
@pytest.fixture(autouse=True)
def mock_distribution():
original_distribution = importlib.metadata.distribution
def side_effect(package_name):
if package_name == "audio-separator":
mock_dist = MagicMock()
mock_dist.version = "0.42.1"
return mock_dist
return original_distribution(package_name)
with patch("importlib.metadata.distribution", side_effect=side_effect):
yield
# Common fixture for expected arguments
@pytest.fixture
def common_expected_args():
return {
"log_formatter": mock.ANY,
"log_level": logging.INFO,
"model_file_dir": "/tmp/audio-separator-models/",
"output_dir": None,
"output_format": "FLAC",
"output_bitrate": None,
"normalization_threshold": 0.9,
"amplification_threshold": 0.0,
"output_single_stem": None,
"invert_using_spec": False,
"sample_rate": 44100,
"use_soundfile": False,
"use_autocast": False,
"chunk_duration": None,
"ensemble_algorithm": None,
"ensemble_weights": None,
"ensemble_preset": None,
"mdx_params": {"hop_length": 1024, "segment_size": 256, "overlap": 0.25, "batch_size": 1, "enable_denoise": False},
"vr_params": {"batch_size": 1, "window_size": 512, "aggression": 5, "enable_tta": False, "enable_post_process": False, "post_process_threshold": 0.2, "high_end_process": False},
"demucs_params": {"segment_size": "Default", "shifts": 2, "overlap": 0.25, "segments_enabled": True},
"mdxc_params": {"segment_size": 256, "batch_size": 1, "overlap": 8, "override_model_segment_size": False, "pitch_shift": 0},
}
# Test the CLI with version argument using subprocess
def test_cli_version_subprocess():
# Skip subprocess CLI tests - require proper CLI installation
pytest.skip("CLI subprocess tests require proper installation")
# Test the CLI with no arguments
def test_cli_no_args(capsys):
test_args = ["cli.py"]
with patch("sys.argv", test_args), patch.dict("sys.modules", {"audio_separator.separator": None}):
with pytest.raises(SystemExit) as exc_info:
main()
assert exc_info.value.code == 1
captured = capsys.readouterr()
assert "Separate audio file into different stems." in captured.out
# Test with multiple filename arguments
def test_cli_multiple_filenames():
test_args = ["cli.py", "test1.mp3", "test2.mp3"]
# Mock the open function to prevent actual file operations
mock_file = mock_open()
# Create a mock logger
mock_logger = MagicMock()
# Patch multiple functions to prevent actual file operations and separations
with patch("sys.argv", test_args), patch("builtins.open", mock_file), patch("audio_separator.separator.Separator.separate") as mock_separate, patch(
"audio_separator.separator.Separator.load_model"
), patch("logging.getLogger", return_value=mock_logger):
# Mock the separate method to return some dummy output
mock_separate.return_value = ["output_file1.mp3", "output_file2.mp3"]
# Call the main function
main()
mock_separate.assert_called_once()
args, kwargs = mock_separate.call_args
assert args[0] == ["test1.mp3", "test2.mp3"]
# Check if the logger captured information about both files
log_messages = [call[0][0] for call in mock_logger.info.call_args_list]
assert any("test1.mp3" in msg and "test2.mp3" in msg for msg in log_messages)
assert any("Separation complete" in msg for msg in log_messages)
# Test the CLI with a specific audio file
def test_cli_with_audio_file(capsys, common_expected_args):
test_args = ["cli.py", "test_audio.mp3", "--model_filename=UVR-MDX-NET-Inst_HQ_4.onnx"]
with patch("audio_separator.separator.Separator.separate") as mock_separate:
mock_separate.return_value = ["output_file.mp3"]
with patch("sys.argv", test_args):
# Call the main function in cli.py
main()
# Update expected args for this specific test
common_expected_args["model_file_dir"] = "/tmp/audio-separator-models/"
# Check if the separate method was called with the correct arguments
mock_separate.assert_called_once()
# Assertions
assert mock_separate.called
# Test the CLI with invalid log level
def test_cli_invalid_log_level():
test_args = ["cli.py", "test_audio.mp3", "--log_level=invalid"]
with patch("sys.argv", test_args):
# Assert an attribute error is raised due to the invalid LogLevel
with pytest.raises(AttributeError):
# Call the main function in cli.py
main()
# Test using model name argument
def test_cli_model_filename_argument(common_expected_args):
test_args = ["cli.py", "test_audio.mp3", "--model_filename=Custom_Model.onnx"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
# Assertions
mock_separator.assert_called_once_with(**common_expected_args)
mock_separator_instance.load_model.assert_called_once_with(model_filename="Custom_Model.onnx")
# Test using output directory argument
def test_cli_output_dir_argument(common_expected_args):
test_args = ["cli.py", "test_audio.mp3", "--output_dir=/custom/output/dir"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
# Update expected args for this specific test
expected_args = common_expected_args.copy()
expected_args["output_dir"] = "/custom/output/dir"
# Assertions
mock_separator.assert_called_once_with(**expected_args)
# Test using output format argument
def test_cli_output_format_argument(common_expected_args):
test_args = ["cli.py", "test_audio.mp3", "--output_format=MP3"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
# Update expected args for this specific test
expected_args = common_expected_args.copy()
expected_args["output_format"] = "MP3"
# Assertions
mock_separator.assert_called_once_with(**expected_args)
# Test using normalization_threshold argument
def test_cli_normalization_threshold_argument(common_expected_args):
test_args = ["cli.py", "test_audio.mp3", "--normalization=0.75"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
# Update expected args for this specific test
expected_args = common_expected_args.copy()
expected_args["normalization_threshold"] = 0.75
# Assertions
mock_separator.assert_called_once_with(**expected_args)
# Test using amplification_threshold argument
def test_cli_amplification_threshold_argument(common_expected_args):
test_args = ["cli.py", "test_audio.mp3", "--amplification=0.75"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
# Update expected args for this specific test
expected_args = common_expected_args.copy()
expected_args["amplification_threshold"] = 0.75
# Assertions
mock_separator.assert_called_once_with(**expected_args)
# Test using single stem argument
def test_cli_single_stem_argument(common_expected_args):
test_args = ["cli.py", "test_audio.mp3", "--single_stem=instrumental"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
# Update expected args for this specific test
expected_args = common_expected_args.copy()
expected_args["output_single_stem"] = "instrumental"
# Assertions
mock_separator.assert_called_once_with(**expected_args)
# Test using invert spectrogram argument
def test_cli_invert_spectrogram_argument(common_expected_args):
test_args = ["cli.py", "test_audio.mp3", "--invert_spect"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
# Update expected args for this specific test
expected_args = common_expected_args.copy()
expected_args["invert_using_spec"] = True
# Assertions
mock_separator.assert_called_once_with(**expected_args)
# Test using use_autocast argument
def test_cli_use_autocast_argument(common_expected_args):
test_args = ["cli.py", "test_audio.mp3", "--use_autocast"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
# Update expected args for this specific test
expected_args = common_expected_args.copy()
expected_args["use_autocast"] = True
# Assertions
mock_separator.assert_called_once_with(**expected_args)
# Test using custom_output_names arguments
def test_cli_custom_output_names_argument(common_expected_args):
custom_names = {
"Vocals": "vocals_output",
"Instrumental": "instrumental_output",
}
test_args = ["cli.py", "test_audio.mp3", f"--custom_output_names={json.dumps(custom_names)}"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
# Assertions
mock_separator.assert_called_once_with(**common_expected_args)
mock_separator_instance.separate.assert_called_once_with(["test_audio.mp3"], custom_output_names=custom_names)
# Test using custom_output_names arguments
def test_cli_demucs_output_names_argument(common_expected_args):
demucs_output_names = {
"Vocals": "vocals_output",
"Drums": "drums_output",
"Bass": "bass_output",
"Other": "other_output",
"Guitar": "guitar_output",
"Piano": "piano_output"
}
test_args = ["cli.py", "test_audio.mp3", f"--custom_output_names={json.dumps(demucs_output_names)}", "--model_filename=htdemucs_6s.yaml"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
# Assertions
mock_separator.assert_called_once_with(**common_expected_args)
mock_separator_instance.separate.assert_called_once_with(["test_audio.mp3"], custom_output_names=demucs_output_names)
# Test using --extra_models for ensemble mode
def test_cli_extra_models_argument(common_expected_args):
test_args = ["cli.py", "test_audio.mp3", "-m", "model1.onnx", "--extra_models", "model2.onnx", "model3.onnx"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
# Assertions
mock_separator.assert_called_once_with(**common_expected_args)
mock_separator_instance.load_model.assert_called_once_with(model_filename=["model1.onnx", "model2.onnx", "model3.onnx"])
# Test that -m with single model still passes a string (backward compat)
def test_cli_single_model_passes_string(common_expected_args):
test_args = ["cli.py", "test_audio.mp3", "-m", "my_model.onnx"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
mock_separator_instance.load_model.assert_called_once_with(model_filename="my_model.onnx")
# Test old CLI syntax: -m model audio.wav (model before audio file)
def test_cli_old_syntax_model_before_audio(common_expected_args):
test_args = ["cli.py", "-m", "my_model.onnx", "test_audio.mp3"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
mock_separator_instance.load_model.assert_called_once_with(model_filename="my_model.onnx")
mock_separator_instance.separate.assert_called_once_with(["test_audio.mp3"], custom_output_names=None)
# Test --ensemble_preset passes preset to Separator and calls load_model() with default
def test_cli_ensemble_preset(common_expected_args):
test_args = ["cli.py", "test_audio.mp3", "--ensemble_preset", "vocal_balanced"]
with patch("sys.argv", test_args):
with patch("audio_separator.separator.Separator") as mock_separator:
mock_separator_instance = mock_separator.return_value
mock_separator_instance.separate.return_value = ["output_file.mp3"]
main()
expected_args = common_expected_args.copy()
expected_args["ensemble_preset"] = "vocal_balanced"
mock_separator.assert_called_once_with(**expected_args)
# With preset and no explicit models, load_model() called with default
mock_separator_instance.load_model.assert_called_once_with()
# Test --list_presets exits cleanly
def test_cli_list_presets(capsys):
test_args = ["cli.py", "--list_presets"]
with patch("sys.argv", test_args):
with pytest.raises(SystemExit) as exc_info:
main()
assert exc_info.value.code == 0
captured = capsys.readouterr()
assert "vocal_balanced" in captured.out
assert "karaoke" in captured.out