def test_load_data(self): """Test load data.""" sample_config = { "fake_data": { "value": "true", "section": "bci_config", "readableName": "Fake EEG Data On/Off", "helpTip": "If ‘true’, fake EEG data will be used instead of real EEG data.", "recommended_values": "", "type": "bool" }, "acq_device": { "value": "LSL", "section": "acq_config", "readableName": "Acquisition Device Connection Method", "helpTip": "Specifies the method used to connect to the data acquisition device (LSL or DSI).", "recommended_values": ["DSI", "LSL"], "type": "str" } } parameters = Parameters(source=None) parameters.load(sample_config) self.assertEqual(len(parameters), 2)
def test_add_missing(self): """Test add_missing_items""" entry1 = { "value": "8000", "section": "acquisition", "readableName": "Acquisition Port", "helpTip": "", "recommended_values": "", "type": "int" } entry2 = { "value": "LSL", "section": "acquisition", "readableName": "Acquisition Device", "helpTip": "", "recommended_values": ["LSL", "DSI"], "type": "str" } {"acq_port": entry1, "acq_device": entry2} parameters = Parameters(source=None) parameters.load({"acq_port": entry1}) new_params = Parameters(source=None) new_params.load({"acq_port": entry1, "acq_device": entry2}) self.assertFalse('acq_device' in parameters.keys()) self.assertTrue(parameters.add_missing_items(new_params)) self.assertTrue('acq_device' in parameters.keys()) self.assertFalse(parameters.add_missing_items(new_params), "Only new parameters should be added.")
def test_values(self): """Test values method""" sample_config = { "acq_port": { "value": "8000", "section": "acquisition", "readableName": "Acquisition Port", "helpTip": "", "recommended_values": "", "type": "int" }, "acq_device": { "value": "LSL", "section": "acquisition", "readableName": "Acquisition Device", "helpTip": "", "recommended_values": ["LSL", "DSI"], "type": "str" } } parameters = Parameters(source=None, cast_values=True) parameters.load(sample_config) self.assertEqual(list(parameters.keys()), ['acq_port', 'acq_device']) self.assertEqual(list(parameters.values()), [8000, 'LSL']) parameters.cast_values = False for value in parameters.values(): self.assertEqual(type(value), dict)
def test_update(self): """Test update method""" sample_config = { "acq_port": { "value": "8000", "section": "acquisition", "readableName": "Acquisition Port", "helpTip": "", "recommended_values": "", "type": "int" }, "acq_device": { "value": "LSL", "section": "acquisition", "readableName": "Acquisition Device", "helpTip": "", "recommended_values": ["LSL", "DSI"], "type": "str" } } parameters = Parameters(source=None, cast_values=True) parameters.load(sample_config) parameters.update(acq_port=9000, acq_device='DSI') self.assertEqual(parameters['acq_port'], 9000) self.assertEqual(parameters['acq_device'], 'DSI')
def test_cast_values(self): """Test cast_values.""" sample_config = { "myint": { "value": "1", "section": "", "readableName": "", "helpTip": "", "recommended_values": "", "type": "int" }, "mybool": { "value": "true", "section": "", "readableName": "", "helpTip": "", "recommended_values": "", "type": "bool" }, "mypath": { "value": "bcipy/parameters/parameters.json", "section": "", "readableName": "", "helpTip": "", "recommended_values": "", "type": "directorypath" }, "mystr": { "value": "hello", "section": "", "readableName": "", "helpTip": "", "recommended_values": "", "type": "str" }, } parameters = Parameters(source=None, cast_values=True) parameters.load(sample_config) self.assertEqual(parameters['myint'], 1) self.assertEqual(parameters.get('myint', 0), 1, 'Should support `get` method with a default value') self.assertEqual( parameters.get('myint'), 1, 'Should support `get` method without a default value') self.assertEqual(parameters['mybool'], True) self.assertEqual(parameters.get('mybool', False), True) self.assertEqual(parameters['mypath'], 'bcipy/parameters/parameters.json') self.assertEqual(parameters['mystr'], 'hello') self.assertEqual(parameters.get('mystr'), 'hello') self.assertFalse(parameters.get('missing_param', False)) parameters.cast_values = False self.assertEqual(parameters['myint']['value'], '1') self.assertEqual(parameters['mybool']['value'], 'true')
def test_load_data_with_missing_field(self): """Test that an exception is raised if data has a missing field.""" data_with_missing_help_tip = { "fake_data": { "value": "true", "section": "bci_config", "readableName": "Fake EEG Data On/Off", "recommended_values": "", "type": "bool" } } parameters = Parameters(source=None) with self.assertRaises(Exception): parameters.load(data_with_missing_help_tip)
def test_load_data_with_unsupported_type(self): """Test that an exception is raised if data has an unsupported data type.""" data_with_unsupported_type = { "fake_data": { "value": "true", "section": "bci_config", "readableName": "Fake EEG Data On/Off", "helpTip": "", "recommended_values": "", "type": "custom_type" } } parameters = Parameters(source=None) with self.assertRaises(Exception): parameters.load(data_with_unsupported_type)
def test_setting_existing_cast_values(self): """Test setting data when Parameters are cast""" sample_config = { "acq_port": { "value": "8000", "section": "acquisition", "readableName": "Acquisition Port", "helpTip": "", "recommended_values": "", "type": "int" }, "acq_device": { "value": "LSL", "section": "acquisition", "readableName": "Acquisition Device", "helpTip": "", "recommended_values": ["LSL", "DSI"], "type": "str" }, "is_txt_stim": { "value": "false", "section": "", "readableName": "", "helpTip": "", "recommended_values": "", "type": "bool" } } parameters = Parameters(source=None, cast_values=True) parameters.load(sample_config) self.assertEqual(parameters['is_txt_stim'], False) parameters['acq_port'] = 9000 parameters['acq_device'] = 'DSI' parameters['is_txt_stim'] = True print(parameters) self.assertEqual(parameters['acq_port'], 9000) self.assertEqual(parameters['acq_device'], 'DSI') self.assertEqual(parameters['is_txt_stim'], True) parameters.cast_values = False self.assertEqual(parameters['acq_port']['value'], '9000') self.assertEqual(parameters['acq_device']['value'], 'DSI') self.assertEqual(parameters['is_txt_stim']['value'], 'true')