From 862ada93baefcf289916bc255714dbd59f4f657b Mon Sep 17 00:00:00 2001 From: Olivia Box Power Date: Mon, 4 Nov 2024 15:10:07 +0000 Subject: [PATCH] Fix failing tests --- src/fingertips_py/__init__.py | 2 +- src/fingertips_py/api_calls.py | 19 ------------------- src/fingertips_py/metadata.py | 4 ++-- src/fingertips_py/retrieve_data.py | 16 +++++++++------- tests/test_all.py | 13 +++---------- 5 files changed, 15 insertions(+), 39 deletions(-) diff --git a/src/fingertips_py/__init__.py b/src/fingertips_py/__init__.py index 385ab03..6326855 100644 --- a/src/fingertips_py/__init__.py +++ b/src/fingertips_py/__init__.py @@ -1,7 +1,7 @@ __version__ = '0.4.0' -from fingertips_py.api_calls import get_json, get_data_in_tuple, make_request +from fingertips_py.api_calls import get_json, make_request from fingertips_py.retrieve_data import get_all_data_for_profile, get_all_data_for_indicators, get_data_by_indicator_ids, \ get_all_areas_for_all_indicators, get_data_for_indicator_at_all_available_geographies from fingertips_py.metadata import get_metadata_for_profile_as_dataframe, get_metadata, get_metadata_for_indicator_as_dataframe, \ diff --git a/src/fingertips_py/api_calls.py b/src/fingertips_py/api_calls.py index 804bfd1..e921b3c 100644 --- a/src/fingertips_py/api_calls.py +++ b/src/fingertips_py/api_calls.py @@ -60,25 +60,6 @@ def get_json_return_df(url, transpose=True): if transpose: df = df.transpose() return df - - -def get_data_in_tuple(url): - """ - :param url: A url to make a request - :return: A list of returned data in tuples - """ - try: - req = requests.get(url) - except requests.exceptions.SSLError: - req = requests.get(url, verify=False) - json_resp = json.loads(req.content.decode('utf-8')) - tup_list = [] - for item in json_resp: - tup_list.append([(k, v) for k, v in item.items()]) - if isinstance(tup_list[0][0], str): - return [(t[1], t[0]) for t in tup_list] - else: - return tup_list def get_data_in_dict(url, key = None, value = None): diff --git a/src/fingertips_py/metadata.py b/src/fingertips_py/metadata.py index 25e06bd..0900662 100644 --- a/src/fingertips_py/metadata.py +++ b/src/fingertips_py/metadata.py @@ -5,7 +5,7 @@ import pandas as pd from urllib.error import HTTPError, URLError -from fingertips_py.api_calls import get_data_in_tuple, base_url, make_request, get_json, get_json_return_df, deal_with_url_error, get_data_in_dict +from fingertips_py.api_calls import base_url, make_request, get_json, get_json_return_df, deal_with_url_error, get_data_in_dict def get_all_ages(is_test=False): @@ -419,7 +419,7 @@ def get_metadata(indicator_ids=None, domain_ids=None, profile_ids=None): return df if indicator_ids and profile_ids: df = get_metadata_for_profile_as_dataframe(profile_ids) - df = pd.concat([df, get_metadata_for_profile_as_dataframe(indicator_ids)]) + df = pd.concat([df, get_metadata_for_indicator_as_dataframe(indicator_ids)]) return df if domain_ids and profile_ids: df = get_metadata_for_profile_as_dataframe(profile_ids) diff --git a/src/fingertips_py/retrieve_data.py b/src/fingertips_py/retrieve_data.py index 5671c38..09d7e26 100644 --- a/src/fingertips_py/retrieve_data.py +++ b/src/fingertips_py/retrieve_data.py @@ -41,7 +41,7 @@ def get_data_by_indicator_ids(indicator_ids, area_type_id, parent_area_type_id=1 indicator_ids = str(indicator_ids) populated_url = url_suffix.format(indicator_ids, str(area_type_id), parent_area_type_id) try: - df = pd.read_csv(base_url + populated_url) + df = pd.read_csv(base_url + populated_url, low_memory = False) except URLError: df = deal_with_url_error(base_url + populated_url) if is_test: @@ -70,10 +70,11 @@ def get_all_data_for_profile(profile_id, parent_area_type_id=15, area_type_id = area_types = get_area_type_ids_for_profile(profile_id) df = pd.DataFrame() for area in area_types: - populated_url = f'all_data/csv/by_profile_id?child_area_type_id={area}\ - &parent_area_type_id={parent_area_type_id}&profile_id={profile_id}' + populated_url = (f'all_data/csv/by_profile_id?child_area_type_id={area}&' + f'parent_area_type_id={parent_area_type_id}&' + f'profile_id={profile_id}') try: - df_returned = pd.read_csv(base_url + populated_url) + df_returned = pd.read_csv(base_url + populated_url, low_memory = False) except HTTPError: raise Exception('There has been a server error with Fingertips for this request. ') except URLError: @@ -109,10 +110,11 @@ def get_all_data_for_indicators(indicators, area_type_id, parent_area_type_id=15 else: indicators = str(indicators) - populated_url = f'all_data/csv/by_indicator_id?indicator_ids={indicators}&\ - child_area_type_id={area_type_id}&parent_area_type_id={parent_area_type_id}' + populated_url = (f'all_data/csv/by_indicator_id?indicator_ids={indicators}&' + f'child_area_type_id={area_type_id}&' + f'parent_area_type_id={parent_area_type_id}') try: - df = pd.read_csv(base_url + populated_url) + df = pd.read_csv(base_url + populated_url, low_memory = False) except URLError: df = deal_with_url_error(base_url + populated_url) df.reset_index() diff --git a/tests/test_all.py b/tests/test_all.py index 8578925..f4b1a43 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -1,6 +1,6 @@ import pandas as pd import pytest -from fingertips_py.api_calls import get_json, get_data_in_tuple, make_request, get_json_return_df, base_url +from fingertips_py.api_calls import get_json, make_request, get_json_return_df, base_url from fingertips_py.retrieve_data import get_all_data_for_profile, get_all_data_for_indicators, get_data_by_indicator_ids, \ get_all_areas_for_all_indicators, get_data_for_indicator_at_all_available_geographies from fingertips_py.metadata import get_metadata_for_profile_as_dataframe, get_metadata, get_metadata_for_indicator_as_dataframe, \ @@ -17,12 +17,6 @@ def test_get_json(): assert data['userId'] == 1 assert isinstance(data, dict) -#no -def test_get_data_in_tuple(): - data = get_data_in_tuple(base_url + 'ages') - assert type(data[1]) == tuple - - def test_make_request(): data = make_request(base_url + 'area_types', 'Id') assert type(data) == dict @@ -36,10 +30,9 @@ def test_get_json_return_df(): # need to think about this one def test_get_all_data_for_profile(): - data = get_all_data_for_profile(84, is_test=True) + data = get_all_data_for_profile(84, area_type_id=402, is_test=True) assert isinstance(data[0], pd.DataFrame) is True - assert data[1] == base_url + 'all_data/csv/by_profile_id?child_area_type_id=154&parent_area_type_id=15&profile_id=84' - assert data[0].shape[1] == 26 + assert data[1] == base_url + 'all_data/csv/by_profile_id?child_area_type_id=402&parent_area_type_id=15&profile_id=84' def test_get_all_areas():