def test_get_models_successfully(self, mocked_get): client = StorageClient.StorageClient() res = client.get_models( "fakeToken", "fakeContextId", ResourceType.ROBOT) self.assertEqual(res[0].name, 'testZip1')
def test_clone_all_experiment_files(self, mocked_list, mocked_get): with patch('tempfile.mkdtemp', return_value='/tmp/nrpTemp') as mock_temp_make: mocked_get.side_effect = None client = StorageClient.StorageClient() experiment_path = os.path.join( self.experiments_directory, "experiment_configuration.exc") mocked_list.return_value = [{ "uuid": "07b35b8f-67cd-4e94-8bec-5ede8049590d", "name": "env_editor.autosaved", "parent": "3ce08569-bdb7-49ee-a751-5640f4b879d4", "contentType": "text/plain", "type": "file", "modifiedOn": "2017-08-31T13:56:34.306090Z" }, { "uuid": "6a63d03e-6dad-4793-80d7-8e32a83ddd14", "name": "simple_move_robot.py", "parent": "3ce08569-bdb7-49ee-a751-5640f4b879d4", "contentType": "application/hbp-neurorobotics.tfs+python", "type": "file", "modifiedOn": "2017-08-30T12:32:47.842214Z" }] with patch("__builtin__.open", mock_open(read_data="data")) as mock_file: res = client.clone_all_experiment_files("fakeToken", "fakeExperiment") self.assertIn('nrpTemp', res[0])
def test_get_user_not_ok(self, mocked_get): client = StorageClient.StorageClient() with self.assertRaises(Exception) as context: client.get_user("wrong token") self.assertTrue( 'Could not verify auth token, status code 404' in context.exception)
def test_get_experiments_failed(self, mocked_get): client = StorageClient.StorageClient() with self.assertRaises(Exception) as context: client.list_experiments("fakeToken", 'ctx') self.assertTrue( 'Failed to communicate with the storage server, status code 404' in context.exception)
def test_temp_folder_not_exists(self): with patch('os.listdir', return_value=['/tmp/other']) as mock_temp, \ patch('tempfile.mkdtemp', return_value='/tmp/nrpTemp') as mock_temp_make: client = StorageClient.StorageClient() fake_temp_directory = client.get_temp_directory() self.assertEqual(fake_temp_directory, '/tmp/nrpTemp')
def test_authenticate_not_ok(self, mocked_post): client = StorageClient.StorageClient() with self.assertRaises(Exception) as context: client.authenticate("non_existing_user", "non_existing_password") self.assertTrue( 'Failed to communicate with the storage server, status code 404' in context.exception)
def test_filter_textures(self): textures = [{"name": "test.png"}, {"name": "test2.JPG"}, { "name": "test3.jPeG"}, {"name": "test4.gif"}, {"name": "test5.txt"}] client = StorageClient.StorageClient() result = client.filter_textures(textures) self.assertEqual(result, [{"name": "test.png"}, {"name": "test2.JPG"}, { "name": "test3.jPeG"}, {"name": "test4.gif"}])
def test_get_custom_model_successfully(self, mocked_get): client = StorageClient.StorageClient() res = client.get_custom_model( "fakeToken", "fakeContextId", "modelZipPath") self.assertEqual(res, 'Test')
def test_delete_file_connection_error(self, mocked_put): client = StorageClient.StorageClient() mocked_put.side_effect = requests.exceptions.ConnectionError() with self.assertRaises(requests.exceptions.ConnectionError) as context: client.delete_file( "fakeToken", "fakeExperiment", "experiment_configuration.exc") self.assertEqual(requests.exceptions.ConnectionError, context.expected)
def test_create_folder_successfully(self, mocked_post): client = StorageClient.StorageClient() res = client.create_folder( "fakeToken", "fakeExperiment", "fakeName") self.assertEqual(res['uuid'], '5b1a2363-1529-40cd-a8b7-94bfd6dea23d') self.assertEqual(res['name'], 'fakeFolder')
def test_get_custom_models_successfully(self, mocked_get): client = StorageClient.StorageClient() res = client.get_custom_models( "fakeToken", "fakeContextId", "environments") self.assertEqual(res[0], {'name': 'testZip1'}) self.assertEqual(res[1], {'name': 'testZip2'})
def test_list_files_successfully(self, mocked_post): client = StorageClient.StorageClient() res = client.list_files( "fakeToken", "fakeExperiment") self.assertEqual( res[0]['uuid'], '07b35b8f-67cd-4e94-8bec-5ede8049590d') self.assertEqual(res[1]['name'], 'simple_move_robot.py')
def test_list_files_connection_error(self, mocked_post): client = StorageClient.StorageClient() mocked_post.side_effect = requests.exceptions.ConnectionError() with self.assertRaises(requests.exceptions.ConnectionError) as context: client.list_files( "fakeToken", "fakeExperiment") self.assertEqual(requests.exceptions.ConnectionError, context.expected)
def test_parse_and_check(self): client = StorageClient.StorageClient() experiment_path = os.path.join( self.experiments_directory, "experiment_configuration.exc") res = client.parse_and_check_file_is_valid(experiment_path, exp_conf_api_gen.CreateFromDocument, exp_conf_api_gen.ExD_) self.assertEqual(res.name, 'Baseball tutorial experiment - Exercise')
def test_delete_file_failed(self, mocked_delete): client = StorageClient.StorageClient() with self.assertRaises(Exception) as context: client.delete_file( "fakeToken", "fakeExperiment", "experiment_configuration.exc") self.assertTrue( 'Failed to communicate with the storage server, status code 404' in context.exception)
def test_get_folder_uuid_by_name_ok(self, mocked_get): mocked_get.return_value = [{"name": 'Experiment_0', "uuid": "Experiment_0_uuid"}, { "name": 'Experiment_1', "uuid": "Experiment_1_uuid"}] client = StorageClient.StorageClient() uuid = client.get_folder_uuid_by_name( 'fakeToken', 'fake_context_id', 'Experiment_0') self.assertEqual(uuid, 'Experiment_0_uuid') mocked_get.assert_called_with( 'fakeToken', 'fake_context_id', name='Experiment_0', get_all=True)
def test_get_texture_failed(self, mocked_get): client = StorageClient.StorageClient() with self.assertRaises(Exception) as context: client.get_textures( "fakeToken", "fakeContextId") self.assertTrue( 'Failed to communicate with the storage server, status code 404' in context.exception)
def test_create_or_update_successfully(self, mocked_post): client = StorageClient.StorageClient() res = client.create_or_update( "fakeToken", "fakeExperiment", "experiment_configuration.exc", "FakeContent", "text/plain") self.assertEqual(res, 200)
def test_get_models_connection_error(self, mocked_get): client = StorageClient.StorageClient() mocked_get.side_effect = requests.exceptions.ConnectionError() with self.assertRaises(requests.exceptions.ConnectionError) as context: client.get_models( "fakeToken", "fakeContextId", ResourceType.ROBOT) self.assertEqual(requests.exceptions.ConnectionError, context.expected)
def test_get_custom_model_connection_error(self, mocked_get): client = StorageClient.StorageClient() mocked_get.side_effect = requests.exceptions.ConnectionError() with self.assertRaises(requests.exceptions.ConnectionError) as context: client.get_custom_model( "fakeToken", "fakeContextId", "modelZipPath") self.assertEqual(requests.exceptions.ConnectionError, context.expected)
def test_create_folder_failed(self, mocked_post): client = StorageClient.StorageClient() with self.assertRaises(Exception) as context: res = client.create_folder( "fakeToken", "fakeExperiment", "fakeName") self.assertTrue( 'Failed to communicate with the storage server, status code 404' in context.exception)
def test_get_model_successfully(self, mocked_get): client = StorageClient.StorageClient() model = MagicMock() model.name = 'model_brain' model.type = ResourceType.BRAIN res = client.get_model( "fakeToken", "fakeContextId", model) self.assertEqual(res, 'Test')
def test_generate_textures_ok(self, mocked_get_textures, mock_create_textures, mock_create_material_from_textures, mock_copy_file_content): mocked_get_textures.return_value = [ {"name": 'test1.png'}, {"name": "test2.gif"}] client = StorageClient.StorageClient() client._StorageClient__texture_directories = ['/somewhere/near/the/rainbow'] client.generate_textures('fakeExperiment', 'fake_token') mocked_get_textures.assert_called_with('fakeExperiment', 'fake_token') mock_create_textures.assert_called() mock_create_material_from_textures.assert_called() mock_copy_file_content.assert_called_with( 'fake_token', '/somewhere/near/the/rainbow/materials/textures', 'fakeExperiment%2Fresources%2Ftextures', 'test2.gif', is_texture=True)
def test_create_or_update_connection_error(self, mocked_post): client = StorageClient.StorageClient() mocked_post.side_effect = requests.exceptions.ConnectionError() with self.assertRaises(requests.exceptions.ConnectionError) as context: client.create_or_update( "fakeToken", "fakeExperiment", "experiment_configuration.exc", "FakeContent", "text/plain") self.assertEqual(requests.exceptions.ConnectionError, context.expected)
def test_get_file_name_successfully(self, mocked_get): client = StorageClient.StorageClient() def get_fake_experiment_file(token, headers): with open(os.path.join(self.experiments_directory, "experiment_configuration.exc")) as exd_file: exp_file_contents = exd_file.read() return MockResponse(None, 200, exp_conf_api_gen.CreateFromDocument(exp_file_contents)) mocked_get.side_effect = get_fake_experiment_file res = client.get_file( "fakeToken", "fakeExperiment", "experiment_configuration.exc") self.assertEqual(res.maturity, "production")
def test_check_file_extension(self): example1 = [{u'uiid': u'/test_folder/experiment_configuration.exc', u'name': u'experiment_configuration.exc'}, {u'uiid': u'/test_folder/.experiment_configuration.exc.swp', u'name': u'.experiment_configuration.exc.swp'}] client = StorageClient.StorageClient() self.assertTrue(client.check_file_extension( example1[1]['name'], ['.swp'])) self.assertFalse(client.check_file_extension( example1[1]['name'], ['.txt'])) self.assertFalse(client.check_file_extension( example1[0]['name'], ['.swp'])) self.assertTrue(client.check_file_extension( example1[0]['name'], ['.exc']))
def test_create_or_update_failed(self, mocked_post): client = StorageClient.StorageClient() with self.assertRaises(Exception) as context: client.create_or_update( "fakeToken", "fakeExperiment", "experiment_configuration.exc", "FakeContent", "text/plain") self.assertTrue( 'Failed to communicate with the storage server, status code 404' in context.exception)
def test_get_model_connection_error(self, mocked_get): client = StorageClient.StorageClient() mocked_get.side_effect = requests.exceptions.ConnectionError() model = MagicMock() model.name = 'model_brain' model.type = ResourceType.BRAIN with self.assertRaises(requests.exceptions.ConnectionError) as context: client.get_model( "fakeToken", "fakeContextId", model, ) self.assertEqual(requests.exceptions.ConnectionError, context.expected)
def test_get_file_by_name_successfully(self, mocked_get): client = StorageClient.StorageClient() def get_fake_experiment_file(token, headers, stream): with open(os.path.join(self.experiments_directory, "experiment_configuration.exc")) as exd_file: exp_file_contents = exd_file.read() return MockResponse(None, 200, exp_conf_api_gen.CreateFromDocument(exp_file_contents)) mocked_get.side_effect = get_fake_experiment_file res = client.get_file( "fakeToken", "fakeExperiment", "experiment_configuration.exc", by_name=True) self.assertEqual(res.name, "Baseball tutorial experiment - Exercise")
def test_get_custom_model_failed(self, mocked_get): client = StorageClient.StorageClient() model = MagicMock() model.name = 'model_brain' model.type = ResourceType.BRAIN with self.assertRaises(Exception) as context: client.get_model( "fakeToken", "fakeContextId", model ) self.assertTrue( 'Failed to communicate with the storage server, status code 404' in context.exception)