changed folder position
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,123 @@
|
||||
import pytest
|
||||
from .audio import AudioProcessor
|
||||
import torch
|
||||
|
||||
|
||||
|
||||
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
TEST_WAVEFORM = torch.tensor([]).to(DEVICE)
|
||||
TEST_SR = 16000
|
||||
SAMPLE_RATE = 16000
|
||||
NORMALIZATION_FACTOR = 32768
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def probe_audio_processor():
|
||||
"""Fixture for creating an instance of the AudioProcessor class with test waveform and sample rate.
|
||||
|
||||
This fixture is used to create an instance of the AudioProcessor class with a predfined test waveform and sample rate (TEST_SR). It returns the instantiated AudioProcessor , which can bes used as a
|
||||
dependency in other test functions.
|
||||
|
||||
|
||||
Returns:
|
||||
AudioProcessor (obj): An instance of the AudioProcessor class with the test waveform and sample rate.
|
||||
"""
|
||||
return AudioProcessor(TEST_WAVEFORM, TEST_SR)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def test_AudioProcessor_init(probe_audio_processor):
|
||||
"""
|
||||
Test the initialization of the AudioProcessor class.
|
||||
|
||||
This test verifies that the AUdioProcessor class is correctly initialized with the provided waveform and sample rate. It checks whether the instantiated AhdioProcessor object has the correct attributes
|
||||
and whether the waveform and sample rate match the expected values.
|
||||
|
||||
Args:
|
||||
probe_audio_processor (obj): An instance of the AudioProcessor class to be tested.
|
||||
|
||||
|
||||
Returns:
|
||||
None
|
||||
|
||||
|
||||
|
||||
|
||||
"""
|
||||
assert isinstance(probe_audio_processor, AudioProcessor)
|
||||
assert probe_audio_processor.waveform.device == TEST_WAVEFORM.device
|
||||
assert torch.equal(probe_audio_processor.waveform, TEST_WAVEFORM)
|
||||
assert probe_audio_processor.sr == TEST_SR
|
||||
|
||||
|
||||
|
||||
def test_cut():
|
||||
"""Test the cut function of the AudioProcessor class.
|
||||
|
||||
This test verifies that the cut function correctly extracts a segment of audio data from
|
||||
the waveform, given start and end indices. It checks whether the size of the extracted segment matches
|
||||
the expected size based on the provided start and end indices and the sample rate.
|
||||
|
||||
Returns:
|
||||
None
|
||||
|
||||
|
||||
"""
|
||||
|
||||
start = 4
|
||||
end = 7
|
||||
assert AudioProcessor(TEST_WAVEFORM, TEST_SR).cut(start, end).size() == int((end - start) * TEST_SR)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def test_audio_processor_invalid_sr():
|
||||
"""Test the behavior of AudioProcessor when an invalid smaple rate is provided.
|
||||
|
||||
This test verifies that the AudioProcessor constructor raises a ValueError when an invalid sample rate is provided. It uses the pytest.raises context manager to check if the ValueError is raised when initializing an
|
||||
AudioProcessor object with an invalid sample rate.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
with pytest.raises(ValueError):
|
||||
AudioProcessor(TEST_WAVEFORM, [44100,48000])
|
||||
|
||||
|
||||
def test_audio_processor_SAMPLE_RATE():
|
||||
"""Test the default sample rate of the AudioProcessor class.
|
||||
|
||||
This test verifies that the default sample rate of the AudioProcessor class matches the expected value defined by the constant SAMPLE_RATE. It instantiates an AudioProcessor object with a test waveform
|
||||
and checks whether the sample rate attribute (sr) of the AudioProcessor object equals the predefined constant SAMPLE_RATE.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
probe_audio_processor = AudioProcessor(TEST_WAVEFORM)
|
||||
assert probe_audio_processor.sr == SAMPLE_RATE
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import pytest
|
||||
import torch
|
||||
from scraibe import Scraibe, Diariser, Transcriber, Transcript, AudioProcessor
|
||||
from unittest.mock import MagicMOck, patch
|
||||
|
||||
|
||||
"""
|
||||
@pytest.fixture
|
||||
def example_audio_file(tmp_path):
|
||||
audio_path = tmp_path
|
||||
"""
|
||||
@pytest.fixture
|
||||
def create_scraibe_instance():
|
||||
return Scraibe()
|
||||
|
||||
|
||||
|
||||
def test_scraibe_init(create_scraibe_instance):
|
||||
model = create_scraibe_instance
|
||||
assert isinstance(model.transcriber, Transcriber)
|
||||
assert isinstance(model.diariser, Diariser)
|
||||
|
||||
|
||||
def test_scraibe_autotranscribe(create_scraibe_instance, example_audio_file):
|
||||
model = create_scraibe_instance
|
||||
transcript = example_audio_file
|
||||
assert isinstance(transcript, Transcript)
|
||||
|
||||
def test_scraibe_diarization(create_scraibe_instance, example_audio_file):
|
||||
model = create_scraibe_instance
|
||||
diarisation_result = model.diarisation(example_audio_file)
|
||||
assert isinstance(diarisation_result, dict)
|
||||
|
||||
|
||||
def test_scraibe_transcribe(create_scraibe_instance, example_audio_file):
|
||||
model = create_scraibe_instance
|
||||
transcription_result = model.transcribe(example_audio_file)
|
||||
assert isinstance(transcription_result, str)
|
||||
|
||||
|
||||
def test_remove_audio_file(create_scraibe_instance, example_audio_file):
|
||||
model = create_scraibe_instance
|
||||
with pytest.raises(ValueError):
|
||||
model.remove_audio_file("non_existing_audio_file")
|
||||
|
||||
model.remove_audio_file(example_audio_file)
|
||||
assert not os.path.exists(example_audio_file)
|
||||
|
||||
|
||||
def test_get_audio_file(create_scraibe_instance, example_audio_file):
|
||||
model = create_scraibe_instance
|
||||
audio_file = os.path.exist(example_audio_file)
|
||||
assert isinstance(audio_file, AudioProcessor)
|
||||
assert isinstance(audio_file.waveform, torch.Tensor)
|
||||
assert isinstance(audio_file.sr, torch.Tensor)
|
||||
@@ -0,0 +1,61 @@
|
||||
import pytest
|
||||
import os
|
||||
from unittest import mock
|
||||
from scraibe import Diariser
|
||||
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def diariser_instance():
|
||||
"""Fixture for creating an instance of the Diariser class with mocked token.
|
||||
|
||||
This fixture is used to create an instance of the the Diariser class with a mocked token returned by the _get_token method. It patches the _get_token method of the Diariser class
|
||||
using unit.test.mock.patch.object, ensuring that it returns a predetrmined value ('personal Hugging-Face token'). The mocked Diariser object is retunrned and can be used as a dependency in otehr tests.
|
||||
|
||||
Returns:
|
||||
Diariser(Obj): An instance of the Diariser class with a mocked token.
|
||||
"""
|
||||
with mock.patch.object(Diariser, '_get_token', return_value = 'personal Hugging-Face token')
|
||||
return Diariser('pyannote')
|
||||
|
||||
|
||||
|
||||
def test_Diariser_init(diariser_instance):
|
||||
"""Test the initialization of the Diariser class.
|
||||
|
||||
This test verifies that the Diariser class is correctly initialized with the specified model.
|
||||
It checks whether the 'model' attribute of the instantiated Diariser object equals 'pyannote'.
|
||||
|
||||
|
||||
Args:
|
||||
diariser_instance (obj): instance of the Diariser class
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
assert diariser_instance.model == 'pyannote'
|
||||
|
||||
|
||||
|
||||
def test_diarisation_function(diariser_instance):
|
||||
"""Test the diarization function of the Diariser class.
|
||||
|
||||
This test verifies that the diarization function of the Diariser class correctly processes
|
||||
an audio file and returns the diarization result. It patches the apply method of the model
|
||||
attribute of the Diariser instance using unittest.mock.patch.object, ensuring that it returns
|
||||
a predetermined value ('diarization_result') when called with the audio file argument.
|
||||
It then calls the diarization function with an example audio file and checks whether the returned
|
||||
diarization output matches the expected result ('diarization_result').
|
||||
|
||||
Args:
|
||||
diariser_instance (obj): instance of the Diariser object
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
with mock.patch.object(diariser_instance.model, 'apply', return_value='diarization_result'):
|
||||
diarization_output = diariser_instance.diarization('example_audio_file.wav')
|
||||
assert diarization_output == 'diarization_result'
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import pytest
|
||||
from unittest.mock import patch
|
||||
from scraibe import Transcriber
|
||||
|
||||
|
||||
|
||||
|
||||
@pytest.mark.parametrize("audio_file, expected_transcription",[("path_to_test_audiofile", "test_transcription")] )
|
||||
@patch("scraibe.Transcriber.load_model")
|
||||
|
||||
def test_transcriber(mock_load_model, audio_file, expected_transcription):
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
mock_load_model (_type_): _description_
|
||||
audio_file (_type_): _description_
|
||||
expected_transcription (_type_): _description_
|
||||
"""
|
||||
mock_model = mock_load_model.return_value
|
||||
mock_model.transcribe.return_value ={"text": expected_transcription}
|
||||
|
||||
transcriber = Transcriber.load_model(model="medium")
|
||||
|
||||
transcription_result = transcriber.transcribe(audio=audio_file)
|
||||
|
||||
assert transcription_result == expected_transcription
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user