From 52280439e2225323805872ee90fa0d96790034cb Mon Sep 17 00:00:00 2001 From: belthlemar Date: Tue, 6 Feb 2024 11:36:00 +0100 Subject: [PATCH] resolve conflicts --- tests/integration/test_integration.py | 169 -------------------------- 1 file changed, 169 deletions(-) diff --git a/tests/integration/test_integration.py b/tests/integration/test_integration.py index afa54736f9..03e3cbafe6 100644 --- a/tests/integration/test_integration.py +++ b/tests/integration/test_integration.py @@ -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 @@ -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"