def test_requests_throws_exception(self, mock_get): mock_resp = None mock_get.return_value = mock_resp mock_get.side_effect = RequestException() result = client.get_call(dummy_url, header=json_header) self.assertEqual(result, None)
def test_valid_response(self, mock_get): mock_resp = _mock_response(content=self.valid_result_data) mock_get.return_value = mock_resp result = client.get_call(dummy_url, header=json_header) self.assertEqual(result.content, self.valid_result_data) self.assertEqual(result.status_code, 200) mock_get.assert_called_with(base_url + dummy_url, headers=json_header)
def test_valid_response_without_base_url(self, mock_get): mock_resp = _mock_response(content=self.valid_result_data) mock_get.return_value = mock_resp url = 'http://my-full-url' result = client.get_call(url, without_base_url=True, header=json_header) self.assertEqual(result.content, self.valid_result_data) self.assertEqual(result.status_code, 200) mock_get.assert_called_with(url, headers=json_header)
def get_region_by_name(referencespace, parcellation, region): get_result = client.get_call('/referencespaces/' + referencespace + '/parcellations/' + parcellation + '/regions/' + region) if get_result is not None: region = json.loads(get_result.content.decode('utf-8')) return Region(region['region']['name'], referencespace, parcellation, kwargs=region['region']) return None
def get_referencespace_by_id(id): get_result = client.get_call('/referencespaces/' + id) if get_result is not None: space_name = json.loads(get_result.content.decode('utf-8'))['referencespace']['name'] space_id = json.loads(get_result.content.decode('utf-8'))['referencespace']['id'] return Referencespace( space_name, space_id, parcellations.get_all_parcellations(space_name) ) return None
def get_all_regions(referencespace, parcellation): result = [] get_result = client.get_call('/referencespaces/' + referencespace + '/parcellations/' + parcellation + '/regions') if get_result is not None: regions = json.loads( get_result.content.decode('utf-8'))['_embedded']['regions'] for region in regions: result.append( Region(region['region']['name'], referencespace, parcellation, kwargs=region['region'])) return result
def get_all_referencespaces(): result = [] get_result = client.get_call('/referencespaces') if get_result is not None: referencespaces = json.loads(get_result.content.decode('utf-8'))['_embedded']['referencespaces'] for space in referencespaces: space_name = space['referencespace']['name'] space_id = space['referencespace']['id'] result.append(Referencespace( space_name, space_id, parcellations.get_all_parcellations(space_name) )) return result
def get_parcellation_by_name(referencespace, parcellation): """ Returns a single parcellation for a given referencespace :param referencespace: Name of the referencespace :param parcellation: Name of the searched parcellation :return: A single Parcellation or None """ get_result = client.get_call('/referencespaces/' + referencespace + '/parcellations/' + parcellation) if get_result is not None: parcellation = json.loads(get_result.content.decode('utf-8')) return Parcellation( parcellation['parcellation']['name'], parcellation['parcellation']['id'], referencespace, regions.get_all_regions(referencespace, parcellation['parcellation']['name']) ) return None
def get_all_parcellations(referencespace): """ Returns a list of all parcellations for a given referencespace :param referencespace: Name of the referencespace :return: List of Parcellations """ result = [] get_result = client.get_call('/referencespaces/' + referencespace + '/parcellations') if get_result is not None: parcellations = json.loads(get_result.content.decode('utf-8'))['_embedded']['parcellations'] for parcellation in parcellations: result.append(Parcellation( parcellation['parcellation']['name'], parcellation['parcellation']['id'], referencespace, regions.get_all_regions(referencespace, parcellation['parcellation']['name']) )) return result
def test_content_none(self, mock_get): mock_resp = None mock_get.return_value = mock_resp result = client.get_call(dummy_url, header=json_header) self.assertEqual(result, None)
def test_content_not_none_but_wrong_status(self, mock_get): mock_resp = _mock_response(content=self.valid_result_data, status=418) mock_get.return_value = mock_resp result = client.get_call(dummy_url, header=json_header) self.assertEqual(result, None)