Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivia-Box-Power committed Nov 4, 2024
1 parent 6a652f6 commit 862ada9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/fingertips_py/__init__.py
Original file line number Diff line number Diff line change
@@ -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, \
Expand Down
19 changes: 0 additions & 19 deletions src/fingertips_py/api_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions src/fingertips_py/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 9 additions & 7 deletions src/fingertips_py/retrieve_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 3 additions & 10 deletions tests/test_all.py
Original file line number Diff line number Diff line change
@@ -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, \
Expand All @@ -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
Expand All @@ -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():
Expand Down

0 comments on commit 862ada9

Please sign in to comment.