From 268deedb0059b851540cbbd62964c0d9783a12ec Mon Sep 17 00:00:00 2001 From: Leonardo Schwarz Date: Thu, 17 Oct 2024 09:20:17 +0200 Subject: [PATCH] modernize --- .../peak_filtering/test_chain_filters.py | 127 +++++++++--------- 1 file changed, 64 insertions(+), 63 deletions(-) diff --git a/tests/unit/spectrum/peak_filtering/test_chain_filters.py b/tests/unit/spectrum/peak_filtering/test_chain_filters.py index ef90a6b..366b2ab 100644 --- a/tests/unit/spectrum/peak_filtering/test_chain_filters.py +++ b/tests/unit/spectrum/peak_filtering/test_chain_filters.py @@ -1,75 +1,76 @@ -import unittest -from unittest.mock import MagicMock +import pytest +from pytest_mock import MockerFixture from depiction.spectrum.peak_filtering import ChainFilters -class TestChainFilters(unittest.TestCase): - def setUp(self) -> None: - self.mock_filter_1 = MagicMock(name="mock_filter_1") - self.mock_filter_2 = MagicMock(name="mock_filter_2") - self.chain_filters = ChainFilters( - filters=[ - self.mock_filter_1, - self.mock_filter_2, - ] - ) +@pytest.fixture() +def mock_filter_1(mocker: MockerFixture): + return mocker.MagicMock(name="mock_filter_1") - def test_filter_index_peaks(self) -> None: - self.mock_filter_1.filter_index_peaks.return_value = [1, 2, 3] - self.mock_filter_2.filter_index_peaks.return_value = [2, 3] - mock_spectrum_mz_arr = MagicMock(name="mock_spectrum_mz_arr") - mock_spectrum_int_arr = MagicMock(name="mock_spectrum_int_arr") - peak_idx_arr = [1, 2, 3, 4] - result_peak_idx_arr = self.chain_filters.filter_index_peaks( - spectrum_mz_arr=mock_spectrum_mz_arr, - spectrum_int_arr=mock_spectrum_int_arr, - peak_idx_arr=peak_idx_arr, - ) +@pytest.fixture() +def mock_filter_2(mocker: MockerFixture): + return mocker.MagicMock(name="mock_filter_2") - self.assertEqual(result_peak_idx_arr, [2, 3]) - self.mock_filter_1.filter_index_peaks.assert_called_once_with( - mock_spectrum_mz_arr, - mock_spectrum_int_arr, - peak_idx_arr, - ) - self.mock_filter_2.filter_index_peaks.assert_called_once_with( - mock_spectrum_mz_arr, - mock_spectrum_int_arr, - [1, 2, 3], - ) - def test_filter_peaks(self) -> None: - self.mock_filter_1.filter_peaks.return_value = ([1, 2, 3], [4, 5, 6]) - self.mock_filter_2.filter_peaks.return_value = ([2, 3], [5, 6]) - mock_spectrum_mz_arr = MagicMock(name="mock_spectrum_mz_arr") - mock_spectrum_int_arr = MagicMock(name="mock_spectrum_int_arr") - peak_mz_arr = [1, 2, 3, 4] - peak_int_arr = [4, 5, 6, 7] +@pytest.fixture() +def chain_filters(mock_filter_1, mock_filter_2): + return ChainFilters(filters=[mock_filter_1, mock_filter_2]) - result_peak_mz_arr, result_peak_int_arr = self.chain_filters.filter_peaks( - spectrum_mz_arr=mock_spectrum_mz_arr, - spectrum_int_arr=mock_spectrum_int_arr, - peak_mz_arr=peak_mz_arr, - peak_int_arr=peak_int_arr, - ) - self.assertEqual(result_peak_mz_arr, [2, 3]) - self.assertEqual(result_peak_int_arr, [5, 6]) - self.mock_filter_1.filter_peaks.assert_called_once_with( - mock_spectrum_mz_arr, - mock_spectrum_int_arr, - peak_mz_arr, - peak_int_arr, - ) - self.mock_filter_2.filter_peaks.assert_called_once_with( - mock_spectrum_mz_arr, - mock_spectrum_int_arr, - [1, 2, 3], - [4, 5, 6], - ) +def test_filter_index_peaks(mocker, mock_filter_1, mock_filter_2, chain_filters) -> None: + mock_filter_1.filter_index_peaks.return_value = [1, 2, 3] + mock_filter_2.filter_index_peaks.return_value = [2, 3] + mock_spectrum_mz_arr = mocker.MagicMock(name="mock_spectrum_mz_arr") + mock_spectrum_int_arr = mocker.MagicMock(name="mock_spectrum_int_arr") + peak_idx_arr = [1, 2, 3, 4] + result_peak_idx_arr = chain_filters.filter_index_peaks( + spectrum_mz_arr=mock_spectrum_mz_arr, + spectrum_int_arr=mock_spectrum_int_arr, + peak_idx_arr=peak_idx_arr, + ) -if __name__ == "__main__": - unittest.main() + assert result_peak_idx_arr == [2, 3] + mock_filter_1.filter_index_peaks.assert_called_once_with( + mock_spectrum_mz_arr, + mock_spectrum_int_arr, + peak_idx_arr, + ) + mock_filter_2.filter_index_peaks.assert_called_once_with( + mock_spectrum_mz_arr, + mock_spectrum_int_arr, + [1, 2, 3], + ) + + +def test_filter_peaks(mocker, mock_filter_1, mock_filter_2, chain_filters) -> None: + mock_filter_1.filter_peaks.return_value = ([1, 2, 3], [4, 5, 6]) + mock_filter_2.filter_peaks.return_value = ([2, 3], [5, 6]) + mock_spectrum_mz_arr = mocker.MagicMock(name="mock_spectrum_mz_arr") + mock_spectrum_int_arr = mocker.MagicMock(name="mock_spectrum_int_arr") + peak_mz_arr = [1, 2, 3, 4] + peak_int_arr = [4, 5, 6, 7] + + result_peak_mz_arr, result_peak_int_arr = chain_filters.filter_peaks( + spectrum_mz_arr=mock_spectrum_mz_arr, + spectrum_int_arr=mock_spectrum_int_arr, + peak_mz_arr=peak_mz_arr, + peak_int_arr=peak_int_arr, + ) + + assert result_peak_mz_arr == [2, 3] + assert result_peak_int_arr == [5, 6] + mock_filter_1.filter_peaks.assert_called_once_with( + mock_spectrum_mz_arr, + mock_spectrum_int_arr, + peak_mz_arr, + peak_int_arr, + ) + mock_filter_2.filter_peaks.assert_called_once_with( + mock_spectrum_mz_arr, + mock_spectrum_int_arr, + [1, 2, 3], + [4, 5, 6], + )