|
| 1 | +from unittest import TestCase |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +from qtt.measurements.scans import measure_segment_scope_reader |
| 5 | + |
| 6 | +class TestScans(TestCase): |
| 7 | + |
| 8 | + def test_measure_segment_scope_reader_2D(self): |
| 9 | + period = 1e-3 |
| 10 | + width = [0.9, 0.9] |
| 11 | + resolution = [96, 96] |
| 12 | + sample_rate = 1e6 |
| 13 | + mock_data_arrays = MagicMock(T=[2, 3, 4]) |
| 14 | + |
| 15 | + mock_scope = MagicMock(sample_rate=sample_rate) |
| 16 | + mock_scope.acquire.return_value = mock_data_arrays |
| 17 | + |
| 18 | + raw_data_mock = MagicMock() |
| 19 | + data_mock = MagicMock() |
| 20 | + waveform = dict(period=period, width_horz=width[0], width_vert=width[1], resolution=resolution) |
| 21 | + average_count = 123 |
| 22 | + |
| 23 | + with patch('qtt.measurements.scans.process_2d_sawtooth', return_value = (data_mock, None)) as process_mock: |
| 24 | + with patch('numpy.array') as array_mock: |
| 25 | + array_mock.return_value.T = raw_data_mock |
| 26 | + result_data = measure_segment_scope_reader(mock_scope, waveform, average_count) |
| 27 | + |
| 28 | + array_mock.assert_called_once_with(mock_data_arrays) |
| 29 | + mock_scope.acquire.assert_called_once_with(number_of_averages=average_count) |
| 30 | + process_mock.assert_called_with(raw_data_mock, period, sample_rate, |
| 31 | + resolution, width, fig=None, start_zero=False) |
| 32 | + self.assertEqual(data_mock, result_data) |
| 33 | + |
| 34 | + def test_measure_segment_scope_reader_1D(self): |
| 35 | + period = 1e-3 |
| 36 | + width = 0.9 |
| 37 | + sample_rate = 1e6 |
| 38 | + mock_data_arrays = MagicMock(T=[2, 3, 4]) |
| 39 | + |
| 40 | + mock_scope = MagicMock(sample_rate=sample_rate) |
| 41 | + mock_scope.acquire.return_value = mock_data_arrays |
| 42 | + |
| 43 | + raw_data_mock = MagicMock() |
| 44 | + data_mock = MagicMock() |
| 45 | + waveform = dict(period=period, width=width) |
| 46 | + average_count = 123 |
| 47 | + |
| 48 | + with patch('qtt.measurements.scans.process_1d_sawtooth', return_value = (data_mock, None)) as process_mock: |
| 49 | + with patch('numpy.array') as array_mock: |
| 50 | + array_mock.return_value.T = raw_data_mock |
| 51 | + result_data = measure_segment_scope_reader(mock_scope, waveform, average_count) |
| 52 | + |
| 53 | + array_mock.assert_called_once_with(mock_data_arrays) |
| 54 | + mock_scope.acquire.assert_called_once_with(number_of_averages=average_count) |
| 55 | + process_mock.assert_called_with(raw_data_mock, [width], period, sample_rate, |
| 56 | + resolution=None, start_zero=False, fig=None) |
| 57 | + self.assertEqual(data_mock, result_data) |
| 58 | + |
| 59 | + def test_measure_segment_scope_reader_no_processing(self): |
| 60 | + mock_scope = MagicMock() |
| 61 | + mock_scope.acquire.return_value = MagicMock() |
| 62 | + |
| 63 | + average_count = 123 |
| 64 | + numpy_array_mock = MagicMock() |
| 65 | + with patch('numpy.array', return_value=numpy_array_mock): |
| 66 | + result_data = measure_segment_scope_reader(mock_scope, None, average_count, process=False) |
| 67 | + mock_scope.acquire.assert_called_once_with(number_of_averages=average_count) |
| 68 | + self.assertEqual(numpy_array_mock, result_data) |
0 commit comments