Skip to content

Commit

Permalink
tests(sensor): test for updating fields that are not allowed and impr…
Browse files Browse the repository at this point in the history
…oved docs

Signed-off-by: GustaafL <guus@seita.nl>
  • Loading branch information
GustaafL committed Aug 2, 2023
1 parent 1cdf476 commit 8c684ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
25 changes: 19 additions & 6 deletions flexmeasures/api/v3_0/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,20 @@ def post(self, sensor_data: dict):
"generic_asset_id": 1,
}
**Example response**
The whole sensor is returned in the response:
The newly posted sensor is returned in the response.
.. sourcecode:: json
{
"name": "power",
"unit": "kWh",
"entity_address": "ea1.2023-08.localhost:fm1.1",
"event_resolution": "PT1H",
"generic_asset_id": 1,
"timezone": "UTC",
}
:reqheader Authorization: The authentication token
:reqheader Content-Type: application/json
Expand Down Expand Up @@ -595,7 +607,6 @@ def patch(self, sensor_data: dict, id: int, db_sensor: Sensor):
- id
- generic_asset_id
- entity_address
- timezone
**Example request**
Expand All @@ -612,10 +623,12 @@ def patch(self, sensor_data: dict, id: int, db_sensor: Sensor):
.. sourcecode:: json
{
"name": "POWER",
"event_resolution": "PT1H",
"unit": "kWh",
"generic_asset_id": 1,
"name": "some gas sensor",
"unit": "m³/h",
"entity_address": "ea1.2023-08.localhost:fm1.1",
"event_resolution": "PT10M",
"generic_asset_id": 4,
"timezone": "UTC",
}
:reqheader Authorization: The authentication token
Expand Down
11 changes: 8 additions & 3 deletions flexmeasures/api/v3_0/tests/test_sensors_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ def test_patch_sensor(client, setup_api_test_data):


@pytest.mark.parametrize(
"attribute", ["generic_asset_id", "timezone", "entity_address"]
"attribute, value",
[("generic_asset_id", 8), ("entity_address", "ea1.2025-01.io.flexmeasures:fm1.1")],
)
def test_patch_sensor_for_excluded_attribute(client, setup_api_test_data, attribute):
def test_patch_sensor_for_excluded_attribute(
client, setup_api_test_data, attribute, value
):
"""Test to change the generic_asset_id that should not be allowed.
The generic_asset_id is excluded in the partial_sensor_schema"""
auth_token = get_auth_token(client, "test_admin_user@seita.nl", "testtest")
Expand All @@ -138,12 +141,14 @@ def test_patch_sensor_for_excluded_attribute(client, setup_api_test_data, attrib
url_for("SensorAPI:patch", id=sensor.id),
headers={"content-type": "application/json", "Authorization": auth_token},
json={
attribute: 8,
attribute: value,
},
)

print(response.json)
assert response.status_code == 422
assert response.json["status"] == "UNPROCESSABLE_ENTITY"
assert response.json["message"]["json"][attribute] == ["Unknown field."]


def test_patch_sensor_from_unrelated_account(client, setup_api_test_data):
Expand Down

0 comments on commit 8c684ab

Please sign in to comment.