Add audio_visualizer/tests/test_core.py
This commit is contained in:
parent
ee5bf65157
commit
4d865bf959
1 changed files with 51 additions and 0 deletions
51
audio_visualizer/tests/test_core.py
Normal file
51
audio_visualizer/tests/test_core.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import pytest
|
||||
import numpy as np
|
||||
from audio_visualizer import core
|
||||
|
||||
@pytest.fixture
|
||||
def dummy_data():
|
||||
# Dummy data simulating SensorDataRow entries
|
||||
timestamps = np.linspace(0, 1, 100)
|
||||
frequency = np.linspace(20, 2000, 100)
|
||||
amplitude = np.sin(np.linspace(0, 10 * np.pi, 100))
|
||||
data = np.column_stack((timestamps, frequency, amplitude))
|
||||
return data
|
||||
|
||||
def test_compute_spectrum_valid_data(dummy_data):
|
||||
# Ensure compute_spectrum runs without errors and returns numpy array
|
||||
spectrum = core.compute_spectrum(dummy_data[:, 2])
|
||||
assert isinstance(spectrum, np.ndarray), "Returned spectrum should be a numpy array"
|
||||
assert spectrum.size == dummy_data[:, 2].size // 2 + 1, "Unexpected spectrum length"
|
||||
|
||||
def test_compute_spectrum_zero_input():
|
||||
data = np.zeros(256)
|
||||
spectrum = core.compute_spectrum(data)
|
||||
assert np.all(spectrum == 0), "Spectrum of all-zero input should be all zeros"
|
||||
|
||||
def test_generate_visualization(monkeypatch, tmp_path, dummy_data):
|
||||
test_file = tmp_path / "input.csv"
|
||||
np.savetxt(test_file, dummy_data, delimiter=",")
|
||||
|
||||
# Patch load_audio_data and Visualizer.render to avoid real I/O or rendering
|
||||
monkeypatch.setattr(core, "load_audio_data", lambda path: dummy_data[:, 2])
|
||||
called = {}
|
||||
def fake_render(self, spectrum):
|
||||
called['rendered'] = isinstance(spectrum, np.ndarray)
|
||||
monkeypatch.setattr(core.Visualizer, "render", fake_render)
|
||||
|
||||
core.generate_visualization(str(test_file))
|
||||
assert called.get('rendered', False), "Visualizer.render should be called with a numpy array"
|
||||
|
||||
def test_visualizer_render_executes(dummy_data):
|
||||
vis = core.Visualizer()
|
||||
spectrum = np.abs(np.fft.rfft(dummy_data[:, 2]))
|
||||
# Ensure that render executes without raising exceptions
|
||||
try:
|
||||
vis.render(spectrum)
|
||||
except Exception as e:
|
||||
pytest.fail(f"Visualizer.render raised an exception: {e}")
|
||||
|
||||
@pytest.mark.parametrize("invalid_input", [None, [], "invalid", np.array([])])
|
||||
def test_compute_spectrum_invalid_inputs(invalid_input):
|
||||
with pytest.raises((ValueError, TypeError)):
|
||||
core.compute_spectrum(invalid_input)
|
||||
Loading…
Reference in a new issue