Skip to content

Commit

Permalink
resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Feb 6, 2024
1 parent 9ff2662 commit 5228043
Showing 1 changed file with 0 additions and 169 deletions.
169 changes: 0 additions & 169 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from pathlib import Path
from unittest.mock import ANY

import numpy as np
import pandas as pd
from starlette.testclient import TestClient

from antarest.core.model import PublicMode
Expand Down Expand Up @@ -2106,170 +2104,3 @@ def test_copy(client: TestClient, admin_access_token: str, study_id: str) -> Non
res = client.get(f"/v1/studies/{copied.json()}", headers=admin_headers).json()
assert res["groups"] == []
assert res["public_mode"] == PublicMode.READ


def test_download_matrices(client: TestClient, admin_access_token: str, study_id: str) -> None:
admin_headers = {"Authorization": f"Bearer {admin_access_token}"}

# todo: replacer ce test dans un autre fichier

# =============================
# STUDIES PREPARATION
# =============================

# Manage parent study
copied = client.post(f"/v1/studies/{study_id}/copy?dest=copied&use_task=false", headers=admin_headers)
parent_id = copied.json()

# Create Variant
res = client.post(
f"/v1/studies/{parent_id}/variants",
headers=admin_headers,
params={"name": "variant_1"},
)
assert res.status_code == 200
variant_id = res.json()

# Create a new area to implicitly create normalized matrices
area_name = "new_area"
res = client.post(
f"/v1/studies/{variant_id}/areas",
headers=admin_headers,
json={"name": area_name, "type": "AREA", "metadata": {"country": "FR"}},
)
assert res.status_code in {200, 201}

# Change study start_date
res = client.put(
f"/v1/studies/{variant_id}/config/general/form", json={"firstMonth": "july"}, headers=admin_headers
)
assert res.status_code == 200

# Really generates the snapshot
client.get(f"/v1/studies/{variant_id}/areas", headers=admin_headers)
assert res.status_code == 200

# =============================
# TEST SPECIFIC MATRICES
# =============================

# todo: test bindingconstraints version 87 ou avant, links before 82 and after 82

# allocation and correlation matrices
for path in ["input/hydro/allocation", "input/hydro/correlation"]:
res = client.get(f"/v1/studies/{parent_id}/raw/download?path={path}&format=csv", headers=admin_headers)
assert res.status_code == 200
content = io.BytesIO(res.content)
dataframe = pd.read_csv(content, index_col=0, sep="\t")
assert list(dataframe.index) == list(dataframe.columns) == ["de", "es", "fr", "it"]
for i in range((len(dataframe))):
assert dataframe.iloc[i, i] == 1.0

# test for empty matrix
res = client.get(
f"/v1/studies/{parent_id}/raw/download?path=input/hydro/common/capacity/waterValues_de&format=csv",
headers=admin_headers,
)
assert res.status_code == 200
content = io.BytesIO(res.content)
dataframe = pd.read_csv(content, index_col=0, sep="\t")
assert dataframe.empty

# modulation matrix
res = client.get(
f"/v1/studies/{parent_id}/raw/download?path=input/thermal/prepro/de/01_solar/modulation&format=csv",
headers=admin_headers,
)
assert res.status_code == 200
content = io.BytesIO(res.content)
dataframe = pd.read_csv(content, index_col=0, sep="\t")
assert dataframe.index[0] == "2018-01-01 00:00:00"
dataframe.index = range(len(dataframe))
liste_transposee = list(zip(*[8760 * [1.0], 8760 * [1.0], 8760 * [1.0], 8760 * [0.0]]))
expected_df = pd.DataFrame(columns=["0", "1", "2", "3"], index=range(8760), data=liste_transposee)
assert dataframe.equals(expected_df)

# =============================
# TESTS NOMINAL CASE ON RAW AND VARIANT STUDY
# =============================

raw_matrix_path = r"input/load/series/load_de"
variant_matrix_path = f"input/load/series/load_{area_name}"
fake_str = "fake_str"

for uuid, path in zip([parent_id, variant_id], [raw_matrix_path, variant_matrix_path]):
# get downloaded bytes
res = client.get(f"/v1/studies/{uuid}/raw/download?path={path}&format=xlsx", headers=admin_headers)
assert res.status_code == 200

# load into dataframe
dataframe = pd.read_excel(io.BytesIO(res.content), index_col=0)

# check time coherence
generated_index = dataframe.index
first_date = generated_index[0].to_pydatetime()
second_date = generated_index[1].to_pydatetime()
assert first_date.month == second_date.month == 1 if uuid == parent_id else 7
assert first_date.day == second_date.day == 1
assert first_date.hour == 0
assert second_date.hour == 1

# reformat into a json to help comparison
new_cols = [int(col) for col in dataframe.columns]
dataframe.columns = new_cols
dataframe.index = range(len(dataframe))
actual_matrix = dataframe.to_dict(orient="split")

# asserts that the result is the same as the one we get with the classic get /raw endpoint
res = client.get(f"/v1/studies/{uuid}/raw?path={path}&formatted=true", headers=admin_headers)
expected_matrix = res.json()
assert actual_matrix == expected_matrix

# =============================
# TEST OTHER PARAMETERS
# =============================

# test only few possibilities as each API call is quite long
for header in [True, False]:
index = not header
res = client.get(
f"/v1/studies/{parent_id}/raw/download?path={raw_matrix_path}&format=csv&header={header}&index={index}",
headers=admin_headers,
)
assert res.status_code == 200
content = io.BytesIO(res.content)
dataframe = pd.read_csv(content, index_col=0 if index else None, header="infer" if header else None, sep="\t")
first_index = dataframe.index[0]
assert first_index == "2018-01-01 00:00:00" if index else first_index == 0
assert isinstance(dataframe.columns[0], str) if header else isinstance(dataframe.columns[0], np.int64)

# =============================
# ERRORS
# =============================

# fake study_id
res = client.get(f"/v1/studies/{fake_str}/raw/download?path={raw_matrix_path}&format=csv", headers=admin_headers)
assert res.status_code == 404
assert res.json()["exception"] == "StudyNotFoundError"

# fake path
res = client.get(
f"/v1/studies/{parent_id}/raw/download?path=input/links/de/{fake_str}&format=csv", headers=admin_headers
)
assert res.status_code == 404
assert res.json()["exception"] == "ChildNotFoundError"

# path that does not lead to a matrix
res = client.get(
f"/v1/studies/{parent_id}/raw/download?path=settings/generaldata&format=csv", headers=admin_headers
)
assert res.status_code == 404
assert res.json()["exception"] == "IncorrectPathError"
assert res.json()["description"] == "The path filled does not correspond to a matrix : settings/generaldata"

# wrong format
res = client.get(
f"/v1/studies/{parent_id}/raw/download?path={raw_matrix_path}&format={fake_str}", headers=admin_headers
)
assert res.status_code == 422
assert res.json()["exception"] == "RequestValidationError"

0 comments on commit 5228043

Please sign in to comment.