Example #1
0
    def test_get_populations(self, service_proxy_mock):
        client = ROSCLEClient.ROSCLEClient(0)

        populations = Mock()
        populations.neurons = [
            PopulationInfo("foo", "bar", [NeuronParameter("p", 42.0)],
                           [0, 8, 15], range(0, 3))
        ]

        client._ROSCLEClient__cle_get_populations = Mock(
            return_value=populations)
        self.assertEqual(
            client.get_populations(), {
                'populations':
                [{
                    'name': 'foo',
                    'neuron_model': 'bar',
                    'parameters': [{
                        'parameterName': 'p',
                        'value': 42.0
                    }],
                    'gids': [0, 8, 15],
                    'indices': range(0, 3)
                }]
            })
Example #2
0
    def test_reset(self):
        client = ROSCLEClient.ROSCLEClient(0)

        simulation_recorder = Mock()
        reset_resp = Mock()
        reset_resp.success = True
        reset = Mock(return_value=reset_resp)
        client._ROSCLEClient__simulation_recorder = simulation_recorder
        client._ROSCLEClient__cle_reset = reset

        pop1 = Mock()
        pop2 = Mock()

        pop1.name = u'foo'
        pop1.step = 1
        pop2.name = 'bar'
        pop2.step = 0
        client.reset(ResetSimulationRequest.RESET_FULL, populations=[pop1, pop2])

        simulation_recorder.assert_has_calls(call(SimulationRecorderRequest.CANCEL))
        simulation_recorder.assert_has_calls(call(SimulationRecorderRequest.RESET))
        self.assertEqual(pop1.name, 'foo')
        self.assertEqual(pop2.name, 'bar')
        self.assertEqual(pop1.step, 1)
        self.assertEqual(pop2.step, 1)
        self.assertTrue(reset.called)

        reset_resp.success = False
        reset_resp.error_message = "Foobar"
        self.assertRaises(ROSCLEClientException, client.reset, ResetSimulationRequest.RESET_FULL)

        client.stop_communication("Test stop")
        self.assertRaises(ROSCLEClientException, client.reset, ResetSimulationRequest.RESET_FULL)
Example #3
0
    def test_rosservicewrapper_call(self, service_proxy_mock):
        client = ROSCLEClient.ROSCLEClient(0)

        # When everything is fine, the service should just be called
        service_wrapper = ROSCLEClient.ROSCLEServiceWrapper(
            'service_name', Empty, client)
        self.assertEqual(client, service_wrapper.ros_cle_client)
        service_wrapper()
        self.assertEquals(service_wrapper.handler.call_count, 1)

        # When something's wrong during the service call it should throw ROSCLEClientException
        service_wrapper = ROSCLEClient.ROSCLEServiceWrapper(
            'service_name', Empty, client, invalidate_on_failure=False)
        service_wrapper.handler.side_effect = rospy.ServiceException()

        self.assertRaises(ROSCLEClient.ROSCLEClientException, service_wrapper)

        service_wrapper.handler.side_effect = rospy.exceptions.ROSInterruptException(
        )

        self.assertRaises(ROSCLEClient.ROSCLEClientException, service_wrapper)

        # and invalidate the ROSCLEClient if asked to
        service_wrapper = ROSCLEClient.ROSCLEServiceWrapper(
            'service_name', Empty, client, invalidate_on_failure=True)
        self.assertRaises(ROSCLEClient.ROSCLEClientException, service_wrapper)
Example #4
0
    def test_command_simulation_recorder(self):
        client = ROSCLEClient.ROSCLEClient(0)

        client._ROSCLEClient__simulation_recorder = Mock(return_value=['foo', 'bar'])
        self.assertEqual(client.command_simulation_recorder(0), ['foo', 'bar'])
        client._ROSCLEClient__simulation_recorder.assert_called_once_with(0)

        client.stop_communication("Test stop")
        with self.assertRaises(ROSCLEClientException):
            client.command_simulation_recorder(0)
Example #5
0
 def test_roscleclient_constructor(self, service_wrapper_mock):
     client = ROSCLEClient.ROSCLEClient(0)
     listened_services = [x[0][0] for x in service_wrapper_mock.call_args_list]
     expected_services = [
         SERVICE_SIM_RESET_ID(0), SERVICE_GET_TRANSFER_FUNCTIONS(0),
         SERVICE_EDIT_TRANSFER_FUNCTION(0), SERVICE_ADD_TRANSFER_FUNCTION(0),
         SERVICE_DELETE_TRANSFER_FUNCTION(0), SERVICE_GET_BRAIN(0), SERVICE_SET_BRAIN(0),
         SERVICE_GET_POPULATIONS(0), SERVICE_GET_CSV_RECORDERS_FILES(0),
         SERVICE_SIMULATION_RECORDER(0)
     ]
     self.assertNotIn(False, [x in listened_services for x in expected_services])
Example #6
0
    def test_delete_simulation_transfer_function(self):
        msg = DeleteTransferFunction()
        resp = msg._response_class(success=True)

        client = ROSCLEClient.ROSCLEClient(0)

        client._ROSCLEClient__cle_delete_transfer_function = MagicMock(return_value=resp)
        self.assertEquals(client.delete_simulation_transfer_function("tf_1"), resp.success)

        client.stop_communication("Test stop")
        self.assertFalse(client.delete_simulation_transfer_function("tf_1"))
Example #7
0
    def test_get_csv_files(self):
        client = ROSCLEClient.ROSCLEClient(0)

        files = Mock()
        files.files = [
            CSVRecordedFile("foo", "foo.csv"), CSVRecordedFile("bar", "bar.csv")
        ]

        client._ROSCLEClient__cle_get_CSV_recorders_files = Mock(return_value=files)
        self.assertEqual(client.get_simulation_CSV_recorders_files(), files.files)

        client.stop_communication("Test stop")
        self.assertEqual(client.get_simulation_CSV_recorders_files(), files.files)
Example #8
0
    def test_get_brain(self):
        msg = GetBrain()
        resp = msg._response_class(brain_data='the brain data', brain_type='the brain type',
                                   data_type='the data type')

        client = ROSCLEClient.ROSCLEClient(0)

        client._ROSCLEClient__cle_get_brain = MagicMock(return_value=resp)
        self.assertEquals(client.get_simulation_brain(), resp)

        client.stop_communication("Test stop")
        with self.assertRaises(ROSCLEClientException):
            client.get_simulation_brain()
Example #9
0
    def test_activate_transfer_function(self):

        msg = ActivateTransferFunction()
        resp = msg._response_class(error_message="")

        client = ROSCLEClient.ROSCLEClient(0)

        client._ROSCLEClient__cle_activate_transfer_function = MagicMock(return_value=resp)
        self.assertEquals(client.activate_simulation_transfer_function("tf_1", True), resp.error_message)

        client.stop_communication("Test stop")
        with self.assertRaises(ROSCLEClientException):
            client.activate_simulation_transfer_function("tf_1", True)
Example #10
0
    def test_edit_simulation_transfer_function(self):
        msg = EditTransferFunction()
        resp = msg._response_class(error_message="Some error message")

        client = ROSCLEClient.ROSCLEClient(0)

        client._ROSCLEClient__cle_edit_transfer_function = MagicMock(return_value=resp)
        self.assertEquals(
            client.edit_simulation_transfer_function("tf_1", "def tf_1(): \n return 1"),
            resp.error_message)

        client.stop_communication("Test stop")
        with self.assertRaises(ROSCLEClientException):
            client.edit_simulation_transfer_function("tf_1", "def tf_1(): \n return 1")
Example #11
0
    def test_set_populations(self, service_proxy_mock):
        msg = SetPopulations()
        resp = msg._response_class(message='error message')

        client = ROSCLEClient.ROSCLEClient(0)
        client._ROSCLEClient__cle_set_populations = MagicMock(
            return_value=resp)

        # client.stop_communication("Test stop")
        client.set_simulation_populations('py', 'brain_population', 'data',
                                          'change_population')

        client._ROSCLEClient__cle_set_populations.assert_called_once_with(
            'py', 'brain_population', 'data', 'change_population')
Example #12
0
    def test_get_simulation_transfer_functions(self):
        msg = GetTransferFunctions()
        msg.transfer_functions = ['some', 'source', 'code']
        msg.active = [True, False, True]

        client = ROSCLEClient.ROSCLEClient(0)

        client._ROSCLEClient__cle_get_transfer_functions = MagicMock(return_value=msg)

        response = client.get_simulation_transfer_functions()
        self.assertEqual(response, (msg.transfer_functions, msg.active))

        client.stop_communication("Test stop")
        response = client.get_simulation_transfer_functions()
        self.assertEqual(response, ([], []))
Example #13
0
    def test_add_simulation_transfer_function(self, service_proxy_mock):
        msg = AddTransferFunction()
        resp = msg._response_class(error_message="Some error message")

        client = ROSCLEClient.ROSCLEClient(0)

        client._ROSCLEClient__cle_add_transfer_function = MagicMock(
            return_value=resp)
        self.assertEqual(
            client.add_simulation_transfer_function(
                "def tf_1(): \n  return 1"), resp.error_message)

        client.stop_communication("Test stop")
        with self.assertRaises(ROSCLEClientException):
            client.add_simulation_transfer_function("def tf_1(): \n return 1")
Example #14
0
    def test_set_brain(self):
        msg = SetBrain()
        resp = msg._response_class(error_message='error message', error_line=-1, error_column=-1)

        client = ROSCLEClient.ROSCLEClient(0)

        change_population = srv.SetBrainRequest.DO_RENAME_POPULATION

        client._ROSCLEClient__cle_set_brain = MagicMock(return_value=resp)
        self.assertEquals(client.set_simulation_brain(
            'data', 'py', 'text', '{"population_1": 2}', change_population), resp)
        client._ROSCLEClient__cle_set_brain.assert_called_once_with(
            'data', 'py', 'text', '{"population_1": 2}', change_population)

        client.stop_communication("Test stop")
        with self.assertRaises(ROSCLEClientException):
            client.set_simulation_brain('data', 'py', 'text', '{"population_1": 2}', change_population)
Example #15
0
    def test_get_csv_files(self, service_proxy_mock, mock_cle_recorded_file):

        mock_cle_recorded_file.return_value = ("bar1", ["bar1 header\n"],
                                               ['data1', 'data2\n'])
        client = ROSCLEClient.ROSCLEClient(0)

        files = Mock()
        files.files = [CSVRecordedFile(), CSVRecordedFile()]

        client._ROSCLEClient__cle_get_CSV_recorders_files = Mock(
            return_value=files)
        self.assertEqual(client.get_simulation_CSV_recorders_files(),
                         files.files)

        client.stop_communication("Test stop")
        self.assertEqual(client.get_simulation_CSV_recorders_files(),
                         files.files)
Example #16
0
    def test_set_brain(self, service_proxy_mock):
        msg = SetBrain()
        resp = msg._response_class(error_message='error message',
                                   error_line=-1,
                                   error_column=-1)

        client = ROSCLEClient.ROSCLEClient(0)

        client._ROSCLEClient__cle_set_brain = MagicMock(return_value=resp)
        self.assertEquals(
            client.set_simulation_brain('py', 'text', 'data',
                                        '{"population_1": 2}'), resp)
        client._ROSCLEClient__cle_set_brain.assert_called_once_with(
            'py', 'data', 'text', '{"population_1": 2}')

        client.stop_communication("Test stop")
        with self.assertRaises(ROSCLEClientException):
            client.set_simulation_brain('py', 'text', 'data',
                                        '{"population_1": 2}')