Commit 172b2b47 authored by Vincent Michel's avatar Vincent Michel
Browse files

Update mca tests

parent 7bf41260
......@@ -2,7 +2,7 @@
import pytest
from bliss.controllers.mca import BaseMCA, Brand, DetectorType
from bliss.controllers.mca import PresetMode, TriggerMode
from bliss.controllers.mca import PresetMode, TriggerMode, Stats
def test_mca_enums():
......@@ -47,7 +47,7 @@ def test_base_mca():
methods = {
mca.finalize: (),
mca.set_preset_mode: ('some_mode',),
mca.set_acquisition_mode: ('some_mode',),
mca.set_acquisition_number: (12,),
mca.set_trigger_mode: ('some_mode',),
mca.set_spectrum_range: ('some', 'range'),
mca.start_acquisition: (),
......@@ -66,8 +66,8 @@ def test_base_mca():
'detector_brand',
'detector_type',
'element_count',
'acquisition_number',
'supported_preset_modes',
'supported_acquisition_modes',
'supported_trigger_modes',
'calibration_type']
......@@ -78,6 +78,7 @@ def test_base_mca():
def test_base_mca_logic(mocker):
stats = Stats(*range(1, 8))
class TestMCA(BaseMCA):
......@@ -86,10 +87,15 @@ def test_base_mca_logic(mocker):
def set_preset_mode(self, mode):
assert mode in (None, PresetMode.NONE)
supported_trigger_modes = [TriggerMode.SOFTWARE]
supported_trigger_modes = [TriggerMode.SOFTWARE, TriggerMode.GATE]
def set_trigger_mode(self, mode):
assert mode in (None, TriggerMode.SOFTWARE)
assert mode is None or mode in self.supported_trigger_modes
def set_acquisition_number(self, value):
assert value == 1
acquisition_number = 1
def initialize_attributes(self):
pass
......@@ -110,15 +116,21 @@ def test_base_mca_logic(mocker):
return [[3, 2, 1]]
def get_acquisition_statistics(self):
return ['some_stats']
return [stats]
# Create a test mca
config = {}
mca = TestMCA('incomplete', config)
assert mca.name == 'incomplete'
assert mca._config is config
assert mca.multiple_acquisition is False
# Run a single acquisition
sleep = mocker.patch('time.sleep')
assert mca.run_single_acquisition(3.) == ([[3, 2, 1]], ['some_stats'])
assert mca.run_single_acquisition(3.) == ([[3, 2, 1]], [stats])
sleep.assert_called_once_with(3.)
# Run an external acquisition
sleep = mocker.patch('time.sleep')
assert mca.run_external_acquisition() == ([[3, 2, 1]], [stats])
sleep.assert_called_once_with(.1)
......@@ -2,11 +2,12 @@
import pytest
from bliss.controllers.mca import Brand, DetectorType, Stats, PresetMode
from bliss.controllers.mca import Brand, DetectorType, Stats
from bliss.controllers.mca import PresetMode, TriggerMode
def test_get_mercury_from_config(beacon, mocker):
# Mocking
@pytest.fixture
def mercury(beacon, mocker):
beacon.reload()
m = mocker.patch('zerorpc.Client')
client = m.return_value
......@@ -21,21 +22,31 @@ def test_get_mercury_from_config(beacon, mocker):
# Emulate running behavior
def mock_not_running():
client.is_running.return_value = False
client.mock_not_running = mock_not_running
# Instantiating the mercury
mercury = beacon.get('mercury1')
assert mercury._proxy is client
m.assert_called_once_with('tcp://welisa.esrf.fr:8000')
yield mercury
def test_mercury_instanciation(mercury):
client = mercury._proxy
client.init.assert_called_once_with(
'C:\\\\blissadm\\\\mercury', 'mercury_src.ini')
assert mercury.current_configuration == 'mercury_src.ini'
assert mercury.configured
# Infos
def test_mercury_infos(mercury):
assert mercury.detector_brand == Brand.XIA
assert mercury.detector_type == DetectorType.MERCURY
assert mercury.element_count == 1
# Configuration
def test_mercury_configuration(mercury):
client = mercury._proxy
assert mercury.available_configurations == ['default.ini']
client.get_config_files.assert_called_once_with(
'C:\\\\blissadm\\\\mercury')
......@@ -43,26 +54,77 @@ def test_get_mercury_from_config(beacon, mocker):
client.get_config.assert_called_once_with(
'C:\\\\blissadm\\\\mercury', 'mercury_src.ini')
# PresetMode
def test_mercury_preset_mode(mercury):
client = mercury._proxy
# First test
mercury.set_preset_mode(None)
assert client.set_acquisition_value.call_args_list == \
[(('preset_type', 0),), (('preset_value', 0),)]
client.apply_acquisition_values.assert_called_once_with()
# Error tests
with pytest.raises(ValueError):
mercury.set_preset_mode(3)
with pytest.raises(TypeError):
mercury.set_preset_mode(PresetMode.NONE, 1)
with pytest.raises(TypeError):
mercury.set_preset_mode(PresetMode.REALTIME, None)
# Acquisition
def test_mercury_trigger_mode(mercury):
client = mercury._proxy
# First test
mercury.set_trigger_mode(None)
client.set_acquisition_value.assert_called_once_with('gate_ignore', 1)
client.apply_acquisition_values.assert_called_once_with()
# Second test
client.set_acquisition_value.reset_mock()
client.apply_acquisition_values.reset_mock()
mercury.set_trigger_mode(TriggerMode.GATE)
client.set_acquisition_value.assert_called_once_with('gate_ignore', 0)
client.apply_acquisition_values.assert_called_once_with()
# Error tests
with pytest.raises(ValueError):
mercury.set_trigger_mode(3)
def test_mercury_acquisition_number(mercury):
client = mercury._proxy
# Test setter
mercury.set_acquisition_number(1)
client.set_acquisition_value.assert_called_once_with('mapping_mode', 0)
client.apply_acquisition_values.assert_called_once_with()
# Test getter
client.get_acquisition_value.return_value = 0.
mercury.acquisition_number == 1
client.get_acquisition_value.assert_called_once_with('mapping_mode', 0)
# Error tests
with pytest.raises(ValueError):
mercury.set_acquisition_number(0)
def test_mercury_acquisition(mercury, mocker):
client = mercury._proxy
sleep = mocker.patch('time.sleep')
sleep.side_effect = lambda x: mock_not_running()
sleep.side_effect = lambda x: client.mock_not_running()
client.get_spectrums.return_value = {0: [3, 2, 1]}
client.get_statistics.return_value = {0: range(7)}
stats = Stats(*range(7))
assert mercury.run_single_acquisition(3.) == ([[3, 2, 1]], [stats])
sleep.assert_called_once_with(0.1)
# Load configuration
def test_mercury_configuration_error(mercury):
client = mercury._proxy
client.init.side_effect = IOError('File not found!')
with pytest.raises(IOError):
mercury.load_configuration('i-dont-exist')
......@@ -70,12 +132,14 @@ def test_get_mercury_from_config(beacon, mocker):
assert mercury.current_configuration is None
assert mercury.current_configuration_values is None
# Finalize
def test_mercury_finalization(mercury):
client = mercury._proxy
mercury.finalize()
client.close.assert_called_once_with()
def test_get_mercury_from_wrong_config(beacon, mocker):
def test_mercury_from_wrong_beacon_config(beacon, mocker):
# ZeroRPC error
beacon.reload()
m = mocker.patch('zerorpc.Client')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment