Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pieter Buzing committed Nov 7, 2024
1 parent 74cf8ab commit d8050d4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
44 changes: 22 additions & 22 deletions tests/instruments/picotech/test_picoscope3403.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
from qmi.instruments.picotech import _picoscope

patcher = patch("picosdk.ps3000a", autospec=True)
picoscope3403._ps = patcher.start()
picoscope3000series._ps = patcher.start()


class PicoscopeMethodsTestCase(unittest.TestCase):

def setUp(self) -> None:
_picoscope._ps = ps3000a
self._picoscope = picoscope3403.PicoTech_PicoScope3403(
self._picoscope = picoscope3000series.PicoTech_PicoScope3403(
QMI_Context('test_picoscope_3403'), 'PicoScope3403', "GR956/0069"
)
# Mock the PicoScope "open" function to be able to open the virtual unit.
Expand Down Expand Up @@ -79,7 +79,7 @@ def test_set_channel_nok_invalid_channel_index(self):

# channel index >= NUM_CHANNELS
with self.assertRaises(ValueError):
self._picoscope.set_channel(picoscope3403.PicoTech_PicoScope3403.NUM_CHANNELS, True,
self._picoscope.set_channel(picoscope3000series.PicoTech_PicoScope3403.NUM_CHANNELS, True,
ChannelCoupling.AC, 0, 1.0)

def test_set_channel_nok_invalid_input_range(self):
Expand All @@ -91,7 +91,7 @@ def test_set_channel_nok_invalid_input_range(self):
# input_range >= NUM_INPUT_RANGES
with self.assertRaises(ValueError):
self._picoscope.set_channel(1, True, ChannelCoupling.AC,
picoscope3403.PicoTech_PicoScope3403.NUM_INPUT_RANGES, 1.0)
picoscope3000series.PicoTech_PicoScope3403.NUM_INPUT_RANGES, 1.0)

def test_set_trigger_ok(self):
"""Test setting trigger with valid input values."""
Expand All @@ -105,14 +105,14 @@ def test_set_trigger_nok_invalid_channel_index(self):

# channel index >= NUM_CHANNELS
with self.assertRaises(ValueError):
self._picoscope.set_trigger(True, picoscope3403.PicoTech_PicoScope3403.NUM_CHANNELS, -10000,
self._picoscope.set_trigger(True, picoscope3000series.PicoTech_PicoScope3403.NUM_CHANNELS, -10000,
TriggerEdge.FALLING)

def test_set_trigger_nok_invalid_threshold(self):
"""Test setting trigger with invalid threshold values."""
# channel index < MIN_SAMPLE_VALUE
with self.assertRaises(ValueError):
self._picoscope.set_trigger(True, 1, picoscope3403.PicoTech_PicoScope3403.MIN_SAMPLE_VALUE - 1,
self._picoscope.set_trigger(True, 1, picoscope3000series.PicoTech_PicoScope3403.MIN_SAMPLE_VALUE - 1,
TriggerEdge.RISING)

# channel index > MAX_SAMPLE_VALUE
Expand All @@ -133,18 +133,18 @@ def test_run_block(self):
self._picoscope._ps_attr.ps3000aGetTimebase2 = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aRunBlock = Mock(return_value=0)
self._picoscope.run_block(10, 10, 100)
picoscope3403._ps.ps3000aGetTimebase2.assert_called_once()
picoscope3000series._ps.ps3000aGetTimebase2.assert_called_once()
picoscope3000series._ps.ps3000aRunBlock.assert_called_once()

def test_is_block_ready(self):
"""Test the if_block_ready function. Note that we cannot check for the case is_block_ready returns True."""
picoscope3403._ps.ps3000aGetTimebase2 = Mock(return_value=0)
picoscope3403._ps.ps3000aRunBlock = Mock(return_value=0)
picoscope3000series._ps.ps3000aGetTimebase2 = Mock(return_value=0)
picoscope3000series._ps.ps3000aRunBlock = Mock(return_value=0)
# We need to run first the run_block to be able to run the is_block_ready
self._picoscope.run_block(10, 10, 100)
self._picoscope.is_block_ready()

picoscope3403._ps.ps3000aGetTimebase2.assert_called_once()
picoscope3000series._ps.ps3000aGetTimebase2.assert_called_once()
picoscope3000series._ps.ps3000aRunBlock.assert_called_once()

def test_wait_block_ready(self):
Expand All @@ -157,7 +157,7 @@ def test_wait_block_ready(self):
self._picoscope.wait_block_ready(1.0)

picoscope3000series._ps.ps3000aGetTimebase2.assert_called_once()
picoscope3403._ps.ps3000aRunBlock.assert_called_once()
picoscope3000series._ps.ps3000aRunBlock.assert_called_once()

def test_wait_block_ready_timeout(self):
"""Test the wait_block_ready function with timeout. Note that we can check only that timeout occurs."""
Expand All @@ -169,7 +169,7 @@ def test_wait_block_ready_timeout(self):
self._picoscope.wait_block_ready(0.5)

picoscope3000series._ps.ps3000aGetTimebase2.assert_called_once()
picoscope3403._ps.ps3000aRunBlock.assert_called_once()
picoscope3000series._ps.ps3000aRunBlock.assert_called_once()

def test_wait_block_ready_invalid_timeout(self):
"""Test the wait_block_ready function returns an error at invalid timeout."""
Expand Down Expand Up @@ -201,7 +201,7 @@ def test_get_block_data_nok_invalid_channel_indexes(self):
with self.assertRaises(ValueError):
self._picoscope.get_block_data(channels)

channels = [1, picoscope3403.PicoTech_PicoScope3403.NUM_CHANNELS]
channels = [1, picoscope3000series.PicoTech_PicoScope3403.NUM_CHANNELS]
# channel index >= NUM_CHANNELS
with self.assertRaises(ValueError):
self._picoscope.get_block_data(channels)
Expand All @@ -212,7 +212,7 @@ def test_get_block_data_nok_wrong_number_of_samples(self):
samples_before = 10
samples_after = 10
picoscope3000series._ps.ps3000aGetTimebase2 = Mock(return_value=0)
picoscope3403._ps.ps3000aRunBlock = Mock(return_value=0)
picoscope3000series._ps.ps3000aRunBlock = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aSetDataBuffer = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aGetValues = Mock(return_value=0)
# We need to run first the run_block to be able to run the get_block_data correctly
Expand All @@ -239,8 +239,8 @@ def test_acquire_by_trigger_and_get_data_ok_interval_1_2_4ns(self):
self._picoscope._ps_attr.ps3000aSetChannel = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aSetSimpleTrigger = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aStop = Mock(return_value=0)
picoscope3403._ps.ps3000aGetTimebase2 = Mock(return_value=0)
picoscope3400series._ps.ps3000aRunBlock = Mock(return_value=0)
picoscope3000series._ps.ps3000aGetTimebase2 = Mock(return_value=0)
picoscope3000series._ps.ps3000aRunBlock = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aIsReady = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aSetDataBuffer = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aGetValues = Mock(return_value=0)
Expand Down Expand Up @@ -281,7 +281,7 @@ def test_acquire_by_trigger_and_get_data_ok_interval_8ns(self):
self._picoscope._ps_attr.ps3000aSetChannel = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aSetSimpleTrigger = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aStop = Mock(return_value=0)
picoscope3403._ps.ps3000aGetTimebase2 = Mock(return_value=0)
picoscope3000series._ps.ps3000aGetTimebase2 = Mock(return_value=0)
picoscope3000series._ps.ps3000aRunBlock = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aIsReady = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aSetDataBuffer = Mock(return_value=0)
Expand Down Expand Up @@ -321,7 +321,7 @@ def test_acquire_no_trigger_and_get_data_ok(self):
self._picoscope._ps_attr.ps3000aSetChannel = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aSetSimpleTrigger = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aStop = Mock(return_value=0)
picoscope3403._ps.ps3000aGetTimebase2 = Mock(return_value=0)
picoscope3000series._ps.ps3000aGetTimebase2 = Mock(return_value=0)
picoscope3000series._ps.ps3000aRunBlock = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aIsReady = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aSetDataBuffer = Mock(return_value=0)
Expand Down Expand Up @@ -359,7 +359,7 @@ def test_acquire_by_trigger_and_get_data_nok_with_timeout(self):
time_span = sampling_interval * 100 # ns

picoscope3000series._ps.ps3000aGetTimebase2 = Mock(return_value=0)
picoscope3403._ps.ps3000aRunBlock = Mock(return_value=0)
picoscope3000series._ps.ps3000aRunBlock = Mock(return_value=0)
self._picoscope._ps_attr.ps3000aIsReady = Mock(return_value=0)
with self.assertRaises(QMI_TimeoutException):
self._picoscope.acquire_by_trigger_and_get_data(
Expand All @@ -374,7 +374,7 @@ def test_acquire_by_trigger_and_get_data_nok_with_timeout(self):

def test_acquire_by_trigger_and_get_data_nok_invalid_channel_index(self):
"""Test trying to acquire data with invalid channel indexes."""
channels = [-1, picoscope3403.PicoTech_PicoScope3403.NUM_CHANNELS]
channels = [-1, picoscope3000series.PicoTech_PicoScope3403.NUM_CHANNELS]
# channel index < 0, channel index >= NUM_CHANNELS
for channel in channels:
with self.assertRaises(ValueError):
Expand All @@ -390,7 +390,7 @@ def test_acquire_by_trigger_and_get_data_nok_invalid_channel_index(self):

def test_acquire_by_trigger_and_get_data_nok_invalid_trigger_index(self):
"""Test trying to acquire data with invalid trigger channel indexes."""
trigger_channels = [-1, picoscope3403.PicoTech_PicoScope3403.NUM_CHANNELS]
trigger_channels = [-1, picoscope3000series.PicoTech_PicoScope3403.NUM_CHANNELS]
# trigger channel index < 0, trigger channel index >= NUM_CHANNELS
for trigger_channel in trigger_channels:
with self.assertRaises(ValueError):
Expand All @@ -408,7 +408,7 @@ def test_acquire_by_trigger_and_get_data_nok_invalid_trigger_index(self):
# trigger channel not in the channels list
with self.assertRaises(ValueError):
self._picoscope.acquire_by_trigger_and_get_data(
[c for c in range(1, picoscope3403.PicoTech_PicoScope3403.NUM_CHANNELS)],
[c for c in range(1, picoscope3000series.PicoTech_PicoScope3403.NUM_CHANNELS)],
[1.0],
[ChannelCoupling.AC],
trigger_channel,
Expand Down
42 changes: 21 additions & 21 deletions tests/instruments/picotech/test_picoscope4824.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
from qmi.instruments.picotech import _picoscope

patcher = patch("picosdk.ps4000a", autospec=True)
picoscope4824._ps = patcher.start()
picoscope4000series._ps = patcher.start()


class PicoscopeMethodsTestCase(unittest.TestCase):

def setUp(self) -> None:
_picoscope._ps = ps4000a
self._picoscope = picoscope4824.PicoTech_PicoScope4824(
self._picoscope = picoscope4000series.PicoTech_PicoScope4824(
QMI_Context('test_picoscope_4824'), 'PicoScope', "GR956/0069"
)
# Mock the PicoScope "open" function to be able to open the virtual unit.
Expand All @@ -35,7 +35,7 @@ def setUp(self) -> None:

def tearDown(self) -> None:
self._picoscope.close()
picoscope4824._ps.reset_mock()
picoscope4000series._ps.reset_mock()

def test_get_serial_number(self):
"""Test query serial number. Mock can only return error code, need to patch the input string."""
Expand Down Expand Up @@ -78,7 +78,7 @@ def test_set_channel_nok_invalid_channel_index(self):

# channel index >= NUM_CHANNELS
with self.assertRaises(ValueError):
self._picoscope.set_channel(picoscope4824.PicoTech_PicoScope4824.NUM_CHANNELS, True,
self._picoscope.set_channel(picoscope4000series.PicoTech_PicoScope4824.NUM_CHANNELS, True,
ChannelCoupling.AC, 0, 1.0)

def test_set_channel_nok_invalid_input_range(self):
Expand All @@ -90,7 +90,7 @@ def test_set_channel_nok_invalid_input_range(self):
# input_range >= NUM_INPUT_RANGES
with self.assertRaises(ValueError):
self._picoscope.set_channel(1, True, ChannelCoupling.AC,
picoscope4824.PicoTech_PicoScope4824.NUM_INPUT_RANGES, 1.0)
picoscope4000series.PicoTech_PicoScope4824.NUM_INPUT_RANGES, 1.0)

def test_set_trigger_ok(self):
"""Test setting trigger with valid input values."""
Expand All @@ -104,19 +104,19 @@ def test_set_trigger_nok_invalid_channel_index(self):

# channel index >= NUM_CHANNELS
with self.assertRaises(ValueError):
self._picoscope.set_trigger(True, picoscope4824.PicoTech_PicoScope4824.NUM_CHANNELS, -10000,
self._picoscope.set_trigger(True, picoscope4000series.PicoTech_PicoScope4824.NUM_CHANNELS, -10000,
TriggerEdge.FALLING)

def test_set_trigger_nok_invalid_threshold(self):
"""Test setting trigger with invalid threshold values."""
# channel index < MIN_SAMPLE_VALUE
with self.assertRaises(ValueError):
self._picoscope.set_trigger(True, 1, picoscope4824.PicoTech_PicoScope4824.MIN_SAMPLE_VALUE - 1,
self._picoscope.set_trigger(True, 1, picoscope4000series.PicoTech_PicoScope4824.MIN_SAMPLE_VALUE - 1,
TriggerEdge.RISING)

# channel index > MAX_SAMPLE_VALUE
with self.assertRaises(ValueError):
self._picoscope.set_trigger(False, 2, picoscope4824.PicoTech_PicoScope4824.MAX_SAMPLE_VALUE + 1,
self._picoscope.set_trigger(False, 2, picoscope4000series.PicoTech_PicoScope4824.MAX_SAMPLE_VALUE + 1,
TriggerEdge.FALLING)

def test_set_disable_trigger_ok(self):
Expand All @@ -132,19 +132,19 @@ def test_run_block(self):
self._picoscope._ps_attr.ps4000aGetTimebase2 = Mock(return_value=0)
self._picoscope._ps_attr.ps4000aRunBlock = Mock(return_value=0)
self._picoscope.run_block(10, 10, 100)
picoscope4824._ps.ps4000aGetTimebase2.assert_called_once()
picoscope4824._ps.ps4000aRunBlock.assert_called_once()
picoscope4000series._ps.ps4000aGetTimebase2.assert_called_once()
picoscope4000series._ps.ps4000aRunBlock.assert_called_once()

def test_is_block_ready(self):
"""Test the if_block_ready function. Note that we cannot check for the case is_block_ready returns True."""
picoscope4824._ps.ps4000aGetTimebase2 = Mock(return_value=0)
picoscope4824._ps.ps4000aRunBlock = Mock(return_value=0)
picoscope4000series._ps.ps4000aGetTimebase2 = Mock(return_value=0)
picoscope4000series._ps.ps4000aRunBlock = Mock(return_value=0)
# We need to run first the run_block to be able to run the is_block_ready
self._picoscope.run_block(10, 10, 100)
self._picoscope.is_block_ready()

picoscope4824._ps.ps4000aGetTimebase2.assert_called_once()
picoscope4824._ps.ps4000aRunBlock.assert_called_once()
picoscope4000series._ps.ps4000aGetTimebase2.assert_called_once()
picoscope4000series._ps.ps4000aRunBlock.assert_called_once()

def test_wait_block_ready(self):
"""Test that the wait_block_ready returns normally without timeout."""
Expand All @@ -155,8 +155,8 @@ def test_wait_block_ready(self):
with patch('ctypes.c_int16', side_effect=[ctypes.c_int16(1)]):
self._picoscope.wait_block_ready(1.0)

picoscope4824._ps.ps4000aGetTimebase2.assert_called_once()
picoscope4824._ps.ps4000aRunBlock.assert_called_once()
picoscope4000series._ps.ps4000aGetTimebase2.assert_called_once()
picoscope4000series._ps.ps4000aRunBlock.assert_called_once()

def test_wait_block_ready_timeout(self):
"""Test the wait_block_ready function with timeout. Note that we can check only that timeout occurs."""
Expand All @@ -167,8 +167,8 @@ def test_wait_block_ready_timeout(self):
with self.assertRaises(QMI_TimeoutException):
self._picoscope.wait_block_ready(0.5)

picoscope4824._ps.ps4000aGetTimebase2.assert_called_once()
picoscope4824._ps.ps4000aRunBlock.assert_called_once()
picoscope4000series._ps.ps4000aGetTimebase2.assert_called_once()
picoscope4000series._ps.ps4000aRunBlock.assert_called_once()

def test_wait_block_ready_invalid_timeout(self):
"""Test the wait_block_ready function returns an error at invalid timeout."""
Expand Down Expand Up @@ -200,7 +200,7 @@ def test_get_block_data_nok_invalid_channel_indexes(self):
with self.assertRaises(ValueError):
self._picoscope.get_block_data(channels)

channels = [1, picoscope4824.PicoTech_PicoScope4824.NUM_CHANNELS]
channels = [1, picoscope4000series.PicoTech_PicoScope4824.NUM_CHANNELS]
# channel index >= NUM_CHANNELS
with self.assertRaises(ValueError):
self._picoscope.get_block_data(channels)
Expand All @@ -210,8 +210,8 @@ def test_get_block_data_nok_wrong_number_of_samples(self):
channels = [1, 3]
samples_before = 10
samples_after = 10
picoscope4824._ps.ps4000aGetTimebase2 = Mock(return_value=0)
picoscope4824._ps.ps4000aRunBlock = Mock(return_value=0)
picoscope4000series._ps.ps4000aGetTimebase2 = Mock(return_value=0)
picoscope4000series._ps.ps4000aRunBlock = Mock(return_value=0)
self._picoscope._ps_attr.ps4000aSetDataBuffer = Mock(return_value=0)
self._picoscope._ps_attr.ps4000aGetValues = Mock(return_value=0)
# We need to run first the run_block to be able to run the get_block_data correctly
Expand Down

0 comments on commit d8050d4

Please sign in to comment.