Skip to content

Commit

Permalink
fix(api-raw): correct the /studies/{uuid}/raw to return "text/plain…
Browse files Browse the repository at this point in the history
…" content for matrices
  • Loading branch information
laurent-laporte-pro committed Oct 12, 2023
1 parent 67dd82e commit 7362385
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
11 changes: 8 additions & 3 deletions antarest/study/web/raw_studies_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,16 @@ def get_study(
extra={"user": current_user.id},
)
parameters = RequestParameters(user=current_user)
output = study_service.get(uuid, path, depth, formatted, parameters)

resource_path = pathlib.PurePosixPath(path)
output = study_service.get(uuid, str(resource_path), depth=depth, formatted=formatted, params=parameters)

if isinstance(output, bytes):
resource_path = pathlib.Path(path)
suffix = resource_path.suffix.lower()
# Guess the suffix form the target data
parent_cfg = study_service.get(uuid, str(resource_path.parent), depth=2, formatted=True, params=parameters)
child = parent_cfg[resource_path.name]
suffix = pathlib.PurePosixPath(child).suffix

content_type, encoding = CONTENT_TYPES.get(suffix, (None, None))
if content_type == "application/json":
# Use `JSONResponse` to ensure to return a valid JSON response
Expand Down
67 changes: 58 additions & 9 deletions tests/integration/raw_studies_blueprint/test_fetch_raw_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import pathlib
import shutil
from urllib.parse import urlencode
from unittest.mock import ANY

import numpy as np
import pytest
Expand Down Expand Up @@ -54,9 +54,9 @@ def test_get_study(
user_folder_dir = study_dir.joinpath("user/folder")
for file_path in user_folder_dir.glob("*.*"):
rel_path = file_path.relative_to(study_dir).as_posix()
query_string = urlencode({"path": f"/{rel_path}", "depth": 1})
res = client.get(
f"/v1/studies/{study_id}/raw?{query_string}",
f"/v1/studies/{study_id}/raw",
params={"path": f"/{rel_path}", "depth": 1},
headers=headers,
)
res.raise_for_status()
Expand All @@ -80,32 +80,81 @@ def test_get_study(
user_folder_dir = study_dir.joinpath("user/unknown")
for file_path in user_folder_dir.glob("*.*"):
rel_path = file_path.relative_to(study_dir)
query_string = urlencode({"path": f"/{rel_path.as_posix()}", "depth": 1})
res = client.get(
f"/v1/studies/{study_id}/raw?{query_string}",
f"/v1/studies/{study_id}/raw",
params={"path": f"/{rel_path.as_posix()}", "depth": 1},
headers=headers,
)
res.raise_for_status()
actual = res.content
expected = file_path.read_bytes()
assert actual == expected

# If we ask for properties, we should have a JSON content
rel_path = "/input/links/de/properties/fr"
res = client.get(
f"/v1/studies/{study_id}/raw",
params={"path": f"/{rel_path}", "depth": 2},
headers=headers,
)
res.raise_for_status()
actual = res.json()
assert actual == {
"asset-type": "ac",
"colorb": 112,
"colorg": 112,
"colorr": 112,
"display-comments": True,
"filter-synthesis": "",
"filter-year-by-year": "hourly",
"hurdles-cost": True,
"link-style": "plain",
"link-width": 1,
"loop-flow": False,
"transmission-capacities": "enabled",
"use-phase-shifter": False,
}

# If we ask for a matrix, we should have a JSON content if formatted is True
rel_path = "/input/links/de/fr"
res = client.get(
f"/v1/studies/{study_id}/raw",
params={"path": f"/{rel_path}", "formatted": True},
headers=headers,
)
res.raise_for_status()
actual = res.json()
assert actual == {"index": ANY, "columns": ANY, "data": ANY}

# If we ask for a matrix, we should have a CSV content if formatted is False
rel_path = "/input/links/de/fr"
res = client.get(
f"/v1/studies/{study_id}/raw",
params={"path": f"/{rel_path}", "formatted": False},
headers=headers,
)
res.raise_for_status()
actual = res.text
actual_lines = actual.splitlines()
first_row = [float(x) for x in actual_lines[0].split("\t")]
assert first_row == [100000, 100000, 0.010000, 0.010000, 0, 0, 0, 0]

# Some files can be corrupted
user_folder_dir = study_dir.joinpath("user/bad")
for file_path in user_folder_dir.glob("*.*"):
rel_path = file_path.relative_to(study_dir)
query_string = urlencode({"path": f"/{rel_path.as_posix()}", "depth": 1})
res = client.get(
f"/v1/studies/{study_id}/raw?{query_string}",
f"/v1/studies/{study_id}/raw",
params={"path": f"/{rel_path.as_posix()}", "depth": 1},
headers=headers,
)
assert res.status_code == http.HTTPStatus.UNPROCESSABLE_ENTITY

# We can access to the configuration the classic way,
# for instance, we can get the list of areas:
query_string = urlencode({"path": "/input/areas/list", "depth": 1})
res = client.get(
f"/v1/studies/{study_id}/raw?{query_string}",
f"/v1/studies/{study_id}/raw",
params={"path": "/input/areas/list", "depth": 1},
headers=headers,
)
res.raise_for_status()
Expand Down
8 changes: 5 additions & 3 deletions tests/storage/web/test_studies_bp.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def test_server() -> None:
client = TestClient(app)
client.get("/v1/studies/study1/raw?path=settings/general/params")

mock_service.get.assert_called_once_with("study1", "settings/general/params", 3, True, PARAMS)
mock_service.get.assert_called_once_with(
"study1", "settings/general/params", depth=3, formatted=True, params=PARAMS
)


@pytest.mark.unit_test
Expand Down Expand Up @@ -121,7 +123,7 @@ def test_server_with_parameters() -> None:
parameters = RequestParameters(user=ADMIN)

assert result.status_code == HTTPStatus.OK
mock_storage_service.get.assert_called_once_with("study1", "/", 4, True, parameters)
mock_storage_service.get.assert_called_once_with("study1", "/", depth=4, formatted=True, params=parameters)

result = client.get("/v1/studies/study2/raw?depth=WRONG_TYPE")
assert result.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
Expand All @@ -130,7 +132,7 @@ def test_server_with_parameters() -> None:
assert result.status_code == HTTPStatus.OK

excepted_parameters = RequestParameters(user=ADMIN)
mock_storage_service.get.assert_called_with("study2", "/", 3, True, excepted_parameters)
mock_storage_service.get.assert_called_with("study2", "/", depth=3, formatted=True, params=excepted_parameters)


@pytest.mark.unit_test
Expand Down

0 comments on commit 7362385

Please sign in to comment.