Skip to content

Commit

Permalink
feat(tests): add bc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Jan 5, 2024
1 parent 7a1ffa8 commit 00880c5
Showing 1 changed file with 95 additions and 35 deletions.
130 changes: 95 additions & 35 deletions tests/integration/study_data_blueprint/test_binding_constraints.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
import pytest
from starlette.testclient import TestClient

Expand Down Expand Up @@ -135,6 +136,9 @@ def test_lifecycle__nominal(self, client: TestClient, user_access_token: str) ->
assert binding_constraints_list[1]["id"] == "binding_constraint_2"
assert binding_constraints_list[2]["id"] == "binding_constraint_3"

# Group section should not exist as the study version is prior to 8.7
assert "group" not in binding_constraints_list[0]

# =============================
# BINDING CONSTRAINT EDITION
# =============================
Expand All @@ -156,9 +160,10 @@ def test_lifecycle__nominal(self, client: TestClient, user_access_token: str) ->
headers=user_headers,
)
binding_constraint = res.json()
comments = binding_constraint["comments"]
assert res.status_code == 200, res.json()
assert comments == new_comment
assert res.status_code == 200, binding_constraint
# Group section should not exist as the study version is prior to 8.7
assert "group" not in binding_constraint
assert binding_constraint["comments"] == new_comment

# Add Constraint term
area1_name = "area1"
Expand Down Expand Up @@ -262,6 +267,60 @@ def test_lifecycle__nominal(self, client: TestClient, user_access_token: str) ->
assert len(dataframe["index"]) == 366
assert len(dataframe["columns"]) == 3 # less, equal, greater

# =============================
# MATRIX EDITION
# =============================

daily_matrix = np.ones((366, 3)).tolist()
bc_id_with_matrix = "binding_constraint_4"
res = client.post(
f"/v1/studies/{study_id}/bindingconstraints",
json={
"name": f"{bc_id_with_matrix}",
"enabled": True,
"time_step": "daily",
"operator": "less",
"coeffs": {},
"comments": "Creation with matrix",
"values": daily_matrix,
},
headers=user_headers,
)
assert res.status_code == 200, res.json()

# Check that the matrix is a daily/weekly matrix
res = client.get(
f"/v1/studies/{variant_id}/raw",
params={"path": f"input/bindingconstraints/{bc_id_with_matrix}", "depth": 1, "formatted": True},
headers=user_headers,
)
assert res.status_code == 200, res.json()
dataframe = res.json()
assert len(dataframe["index"]) == 366
assert len(dataframe["columns"]) == 3 # less, equal, greater

wrong_matrix = np.ones((352, 3))
bc_id_with_wrong_matrix = "binding_constraint_5"
res = client.post(
f"/v1/studies/{study_id}/bindingconstraints",
json={
"name": f"{bc_id_with_wrong_matrix}",
"enabled": True,
"time_step": "daily",
"operator": "less",
"coeffs": {},
"comments": "Creation with matrix",
"values": wrong_matrix.tolist(),
},
headers=user_headers,
)
assert res.status_code == 500
assert res.json()["exception"] == "CommandApplicationError"
assert (
res.json()["description"]
== f"Unexpected exception occurred when trying to apply command CommandName.CREATE_BINDING_CONSTRAINT: Invalid matrix shape {wrong_matrix.shape}, expected (366, 3)"
)

# =============================
# BINDING CONSTRAINT ERRORS
# =============================
Expand Down Expand Up @@ -327,7 +386,7 @@ def test_lifecycle__nominal(self, client: TestClient, user_access_token: str) ->
res = client.post(
f"/v1/studies/{study_id}/bindingconstraints",
json={
"name": "binding_constraint_1",
"name": "binding_constraint_x",
"enabled": True,
"time_step": "hourly",
"operator": "less",
Expand All @@ -348,7 +407,7 @@ def test_lifecycle__nominal(self, client: TestClient, user_access_token: str) ->
res = client.post(
f"/v1/studies/{study_id}/bindingconstraints",
json={
"name": "binding_constraint_2",
"name": "binding_constraint_x",
"enabled": True,
"time_step": "hourly",
"operator": "less",
Expand All @@ -361,34 +420,35 @@ def test_lifecycle__nominal(self, client: TestClient, user_access_token: str) ->
assert res.status_code == 400
assert res.json()["description"] == "You cannot fill a 'matrix_term' as these values refer to v8.7+ studies"

def test_87(self, client: TestClient, admin_access_token: str, study_id: str) -> None:
admin_headers = {"Authorization": f"Bearer {admin_access_token}"}
# Upgrade study to version 870
res = client.put(
f"/v1/studies/{study_id}/upgrade",
headers=admin_headers,
params={"target_version": 870},
)
res.raise_for_status()
task_id = res.json()
task = wait_task_completion(client, admin_access_token, task_id)
from antarest.core.tasks.model import TaskStatus

assert task.status == TaskStatus.COMPLETED, task

# Creation with wrong matrix according to version
res = client.post(
f"/v1/studies/{study_id}/bindingconstraints",
json={
"name": "binding_constraint_700",
"enabled": True,
"time_step": "hourly",
"operator": "less",
"coeffs": {},
"comments": "New API",
"values": [[]],
},
headers=admin_headers,
)
assert res.status_code == 400
assert res.json()["description"] == "You cannot fill 'values' as it refers to the matrix before v8.7"
def test_87(self, client: TestClient, admin_access_token: str, study_id: str) -> None:
admin_headers = {"Authorization": f"Bearer {admin_access_token}"}
# Upgrade study to version 870
res = client.put(
f"/v1/studies/{study_id}/upgrade",
headers=admin_headers,
params={"target_version": 870},
)
res.raise_for_status()
task_id = res.json()
task = wait_task_completion(client, admin_access_token, task_id)
from antarest.core.tasks.model import TaskStatus

assert task.status == TaskStatus.COMPLETED, task

# Creation with wrong matrix according to version
res = client.post(
f"/v1/studies/{study_id}/bindingconstraints",
json={
"name": "binding_constraint_700",
"enabled": True,
"time_step": "hourly",
"operator": "less",
"coeffs": {},
"comments": "New API",
"values": [[]],
},
headers=admin_headers,
)
assert res.status_code == 400
assert res.json()["description"] == "You cannot fill 'values' as it refers to the matrix before v8.7"

0 comments on commit 00880c5

Please sign in to comment.