import pytest from unittest import mock import os import tempfile import pandas as pd import laser_echo_analysis.visualization as viz @pytest.fixture def sample_analysis_results(): return { 'peak': 12345, 'average_noise': 12.3, 'signal_to_noise_ratio': 1003.66 } def test_visualize_results_creates_plot_file(sample_analysis_results): with tempfile.TemporaryDirectory() as tmpdir: plot_path = os.path.join(tmpdir, 'laser_analysis_plot.png') # Patch plt.savefig to simulate file creation and speed up tests with mock.patch('matplotlib.pyplot.savefig') as mock_save: mock_save.side_effect = lambda path, *a, **kw: open(path, 'w').close() viz.visualize_results(sample_analysis_results) mock_save.assert_called() # ensure temporary directory is usable within test # simulate writing to a desired path with mock.patch('matplotlib.pyplot.savefig') as mock_save_2: mock_save_2.side_effect = lambda path, *a, **kw: open(plot_path, 'w').close() viz.visualize_results(sample_analysis_results) assert os.path.exists(plot_path), 'Plot file was not created.' def test_visualize_results_handles_invalid_input_types(): with pytest.raises((TypeError, ValueError)): viz.visualize_results('not_a_dict') def test_visualize_results_missing_keys(monkeypatch): partial_result = {'peak': 12} with pytest.raises(KeyError): viz.visualize_results(partial_result) def test_visualize_results_plot_data_sample(sample_analysis_results): # Mock plt.show and plt.savefig to ensure no GUI is opened with mock.patch('matplotlib.pyplot.show') as mock_show, \ mock.patch('matplotlib.pyplot.savefig') as mock_save: viz.visualize_results(sample_analysis_results) mock_save.assert_called() mock_show.assert_not_called() # call count sanity check assert mock_save.call_count >= 1