Skip to content

Commit

Permalink
update the calibration tests to mock open and prevent testing issues …
Browse files Browse the repository at this point in the history
…on Windows
  • Loading branch information
tab-cmd committed Dec 4, 2023
1 parent 6c05cb1 commit a923063
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 110 deletions.
2 changes: 1 addition & 1 deletion bcipy/acquisition/tests/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_default_supported_devices(self):
dsi = supported['DSI-24']
self.assertEqual('EEG', dsi.content_type)

self.assertEqual(len(devices.with_content_type('EEG')), 3)
self.assertEqual(len(devices.with_content_type('EEG')), 4)

def test_load_from_config(self):
"""Should be able to load a list of supported devices from a
Expand Down
119 changes: 64 additions & 55 deletions bcipy/task/tests/paradigm/matrix/test_matrix_calibration.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import shutil
import tempfile
import unittest

from mockito import any, mock, unstub, verify, when

from mock import mock_open, patch
import psychopy
import numpy as np

Expand Down Expand Up @@ -95,7 +93,7 @@ def setUp(self):
ContentType.EEG: self.eeg_client_mock
}
})
self.temp_dir = tempfile.mkdtemp()
self.temp_dir = ''
self.model_metadata = mock({
'device_spec': device_spec,
'transform': mock(),
Expand All @@ -119,27 +117,30 @@ def setUp(self):

def tearDown(self):
"""Override"""
shutil.rmtree(self.temp_dir)
unstub()

def test_initialize(self):
"""Test initialization"""
MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)

with patch('bcipy.helpers.triggers.open', mock_open(read_data=''), create=False):
MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)

verify(bcipy.task.paradigm.matrix.calibration, times=1).init_calibration_display_task(
self.parameters, self.win, any(), any())

def test_execute(self):
"""Test task execute"""
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)
with patch('bcipy.helpers.triggers.open', mock_open(read_data=''), create=False):
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)

when(task).write_stimuli_positions().thenReturn(None)
when(task).write_offset_trigger().thenReturn(None)
when(task).write_trigger_data(any(), any()).thenReturn(None)
Expand All @@ -155,19 +156,21 @@ def test_validate_parameters_throws_task_exception_empty_parameter(self):
parameters = {}

with self.assertRaises(Exception):
MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=parameters,
file_save=self.temp_dir)
with patch('bcipy.helpers.triggers.open', mock_open(read_data=''), create=False):
MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=parameters,
file_save=self.temp_dir)

def test_execute_save_stimuli_positions(self):
"""Test execute save stimuli positions method is called as expected."""
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)
with patch('bcipy.helpers.triggers.open', mock_open(read_data=''), create=False):
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)

when(bcipy.task.paradigm.matrix.calibration).save_stimuli_position_info(
any(), any(), task.screen_info).thenReturn(None)
Expand All @@ -184,11 +187,12 @@ def test_execute_save_stimuli_positions(self):

def test_trigger_type_targetness(self):
"""Test trigger type targetness."""
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)
with patch('bcipy.helpers.triggers.open', mock_open(read_data=''), create=False):
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)

# non-target
symbol = 'N'
Expand All @@ -206,11 +210,12 @@ def test_trigger_type_targetness(self):

def test_trigger_type_fixation(self):
"""Test trigger type fixation."""
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)
with patch('bcipy.helpers.triggers.open', mock_open(read_data=''), create=False):
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)

# fixation
symbol = '+'
Expand All @@ -221,11 +226,12 @@ def test_trigger_type_fixation(self):

def test_trigger_type_prompt(self):
"""Test trigger type prompt."""
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)
with patch('bcipy.helpers.triggers.open', mock_open(read_data=''), create=False):
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)

# prompt, index = 0, otherwise it would be a target
symbol = 'P'
Expand All @@ -236,11 +242,12 @@ def test_trigger_type_prompt(self):

def test_write_trigger_data_first_run(self):
"""Test write trigger data when it is the first run of the task."""
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)
with patch('bcipy.helpers.triggers.open', mock_open(read_data=''), create=False):
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)

client_by_type_resp = {ContentType.EEG: self.eeg_client_mock}
timing_mock = mock()
Expand All @@ -262,11 +269,12 @@ def test_write_trigger_data_first_run(self):

def test_write_trigger_data_not_first_run(self):
"""Test write trigger data when it is not the first run of the task."""
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)
with patch('bcipy.helpers.triggers.open', mock_open(read_data=''), create=False):
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)

timing_mock = mock()
timing = [('a', 0.0)]
Expand All @@ -280,11 +288,12 @@ def test_write_trigger_data_not_first_run(self):

def test_write_offset_trigger(self):
"""Test write offset trigger"""
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)
with patch('bcipy.helpers.triggers.open', mock_open(read_data=''), create=False):
task = MatrixCalibrationTask(
win=self.win,
daq=self.daq,
parameters=self.parameters,
file_save=self.temp_dir)
client_by_type_resp = {ContentType.EEG: self.eeg_client_mock}
when(self.daq).client_by_type(ContentType.EEG).thenReturn(client_by_type_resp)
when(bcipy.task.paradigm.matrix.calibration).offset_label(
Expand Down
Loading

0 comments on commit a923063

Please sign in to comment.