Skip to content

Commit

Permalink
fix: dataset metadata response parsing (#43)
Browse files Browse the repository at this point in the history
* add final slash

* update tests
  • Loading branch information
MDunitz authored Sep 8, 2021
1 parent 2ba9729 commit 084dc5c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions backend/czi_hosted/data_common/dataset_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def request_dataset_metadata_from_data_portal(data_portal_api_base: str, explore
headers = {"Content-Type": "application/json", "Accept": "application/json"}
try:
response = requests.get(
url=f"{data_portal_api_base}/datasets/meta?url={explorer_url}",
url=f"{data_portal_api_base}/datasets/meta?url={explorer_url}/",
headers=headers
)
if response.status_code == 200:
dataset_identifiers = json.loads(response.body)
dataset_identifiers = json.loads(response.content)
return dataset_identifiers
else:
return None
Expand Down Expand Up @@ -69,7 +69,6 @@ def get_dataset_metadata_for_explorer_location(dataset_explorer_location: str, a
in the server config.
In the case of a single dataset the dataset location is pulled directly from the server_config.
"""

if app_config.server_config.data_locator__api_base:
explorer_url_path = f"{app_config.server_config.get_web_base_url()}/{dataset_explorer_location}"
dataset_metadata = request_dataset_metadata_from_data_portal(
Expand All @@ -78,6 +77,7 @@ def get_dataset_metadata_for_explorer_location(dataset_explorer_location: str, a
)
if dataset_metadata:
return dataset_metadata

server_config = app_config.server_config
dataset_metadata = {
"collection_id": None,
Expand Down
12 changes: 8 additions & 4 deletions backend/test/test_czi_hosted/unit/common/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def setUpClass(cls, mock_get):
cls.schema = json.loads(result.data)

assert mock_get.call_count == 1
assert f"http://{mock_get._mock_call_args[1]['url']}" == f"http://{cls.data_locator_api_base}/datasets/meta?url={cls.config.server_config.get_web_base_url()}{cls.TEST_DATASET_URL_BASE}" # noqa
assert f"http://{mock_get._mock_call_args[1]['url']}" == f"http://{cls.data_locator_api_base}/datasets/meta?url={cls.config.server_config.get_web_base_url()}{cls.TEST_DATASET_URL_BASE}/" # noqa

@patch('backend.czi_hosted.data_common.dataset_metadata.requests.get')
def test_data_adaptor_uses_corpora_api(self, mock_get):
Expand Down Expand Up @@ -624,7 +624,9 @@ def test_metadata_api_called_for_new_dataset(self, mock_get):

# check that the metadata api was correctly called for the new (uncached) dataset
self.assertEqual(mock_get.call_count, 1)
self.assertEqual(f"http://{mock_get._mock_call_args[1]['url']}", 'http://api.cellxgene.staging.single-cell.czi.technology/dp/v1/datasets/meta?url=None/e/pbmc3k_v0.cxg')
self.assertEqual(
f"http://{mock_get._mock_call_args[1]['url']}",
"http://api.cellxgene.staging.single-cell.czi.technology/dp/v1/datasets/meta?url=None/e/pbmc3k_v0.cxg/")

@patch('backend.czi_hosted.data_common.dataset_metadata.requests.get')
def test_data_locator_defaults_to_name_based_lookup_if_metadata_api_throws_error(self, mock_get):
Expand All @@ -638,7 +640,9 @@ def test_data_locator_defaults_to_name_based_lookup_if_metadata_api_throws_error

# check that the metadata api was correctly called for the new (uncached) dataset
self.assertEqual(mock_get.call_count, 1)
self.assertEqual(f"http://{mock_get._mock_call_args[1]['url']}", 'http://api.cellxgene.staging.single-cell.czi.technology/dp/v1/datasets/meta?url=None/e/pbmc3k.cxg')
self.assertEqual(
f"http://{mock_get._mock_call_args[1]['url']}",
'http://api.cellxgene.staging.single-cell.czi.technology/dp/v1/datasets/meta?url=None/e/pbmc3k.cxg/')


# check schema loads correctly even with metadata api exception
Expand Down Expand Up @@ -699,7 +703,7 @@ def test_config_with_portal_metadata(self, mock_get):

class MockResponse:
def __init__(self, body, status_code):
self.body = body
self.content = body
self.status_code = status_code

def json(self):
Expand Down

0 comments on commit 084dc5c

Please sign in to comment.