Skip to content

Commit

Permalink
Update api test to new requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
BraunRudolf committed Aug 16, 2024
1 parent 5bb2ce0 commit 9b96899
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions tests/api_tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,43 @@

from api.app.api import app

expected_dict_key = "power_kw"

expected_prediction_key = "power_kw"
expected_dict_keys = ["timestamp", "predictions"]
prediction_key = expected_dict_keys[1]
expected_number_of_values = 192
expected_response_on_wrong_types = {
"detail": [
{
"loc": ["body", "latitude"],
"loc": ["body","site", "latitude"],
"msg": "Input should be less than or equal to 90",
"type": "less_than_equal",
"input": 91,
"ctx": {"le": 90.0},
},
{
"loc": ["body", "longitude"],
"loc": ["body", "site", "longitude"],
"msg": "Input should be greater than or equal to -180",
"type": "greater_than_equal",
"input": -181,
"ctx": {"ge": -180.0},
},
{
"loc": ["body", "capacity_kwp"],
"loc": ["body","site", "capacity_kwp"],
"msg": "Input should be greater than or equal to 0",
"type": "greater_than_equal",
"input": -1,
"ctx": {"ge": 0.0},
},
{
"loc": ["body", "tilt"],
"loc": ["body","site", "tilt"],
"msg": "Input should be less than or equal to 90",
"type": "less_than_equal",
"input": 91,
"ctx": {"le": 90.0},
},
{
"loc": ["body", "orientation"],
"loc": ["body","site", "orientation"],
"msg": "Input should be less than or equal to 360",
"type": "less_than_equal",
"input": 361,
Expand All @@ -54,57 +57,63 @@ def client():
@pytest.fixture
def body():
return {
"site":
{
"latitude": -90,
"longitude": -180,
"capacity_kwp": 0,
"tilt": 35,
"orientation": 180,
"inverter_type": "string",
},
"timestamp": "2021-01-26 01:15:00",
}


@pytest.fixture
def body_short():
return {
"latitude": -90,
"longitude": -180,
"capacity_kwp": 0,
"site":{
"latitude": -90,
"longitude": -180,
"capacity_kwp": 0,
},
"timestamp": "2021-01-26 01:15:00",
}


@pytest.fixture
def body_wrong():
return {
"latitude": 91,
"longitude": -181,
"capacity_kwp": -1,
"tilt": 91,
"orientation": 361,
"inverter_type": "string",
"site":{
"latitude": 91,
"longitude": -181,
"capacity_kwp": -1,
"tilt": 91,
"orientation": 361,
"inverter_type": "string",
},
"timestamp": "2021-01-26 01:15:00",
}


def test_api_ok(client, body):
response = client.post("/forecast/", json=body)
response_body = response.json()
assert response.status_code == 200
assert isinstance(response_body[expected_dict_key], dict)
assert any(
expected_dict_key in key for key in response.json().keys()
), f"No key contains power_kw"
assert len(response.json()[expected_dict_key]) == expected_number_of_values
assert isinstance(response_body[prediction_key][expected_prediction_key], dict)
assert all([key in response_body.keys() for key in expected_dict_keys]), "Expected dictonary key is missing"
assert len(response_body[prediction_key][expected_prediction_key]) == expected_number_of_values, "Expected number of values is wrong"


def test_api_ok_short(client, body_short):
response = client.post("/forecast/", json=body_short)
response_body = response.json()

assert response.status_code == 200
assert isinstance(response_body[expected_dict_key], dict)
assert any(
expected_dict_key in key for key in response_body.keys()
), f"No key contains power_kw"
assert len(response.json()[expected_dict_key]) == expected_number_of_values
assert isinstance(response_body[prediction_key][expected_prediction_key], dict)
assert all([key in response_body.keys() for key in expected_dict_keys]), "Expected dictonary key is missing"
assert len(response_body[prediction_key][expected_prediction_key]) == expected_number_of_values


def test_api_wrong_body(client, body_wrong):
Expand Down

0 comments on commit 9b96899

Please sign in to comment.