Skip to content

Commit 6980c7a

Browse files
CopilotFlix6x
andauthored
Fix test: flush session before reading sensor IDs, reset sensors_to_show after test
Agent-Logs-Url: https://github.com/FlexMeasures/flexmeasures/sessions/5ce56f2a-9e55-408b-9659-f12da4d6800a Co-authored-by: Flix6x <30658763+Flix6x@users.noreply.github.com>
1 parent 4bc5233 commit 6980c7a

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

flexmeasures/api/common/schemas/tests/test_sensor_data_schema.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,11 @@ def test_asset_sensors_metadata_old_sensors_to_show_format(db, add_weather_senso
367367
contained the old format with a singular 'sensor' key, e.g. {"title": "Prices", "sensor": 42}.
368368
"""
369369
asset = add_weather_sensors["asset"]
370-
wind_sensor, temperature_sensor = (
371-
add_weather_sensors["wind"],
372-
add_weather_sensors["temperature"],
373-
)
370+
wind_sensor = add_weather_sensors["wind"]
371+
temperature_sensor = add_weather_sensors["temperature"]
372+
373+
# Flush so sensor IDs are assigned before we embed them in sensors_to_show
374+
db.session.flush()
374375

375376
# Use the old format: one entry with plural "sensors" and one with singular "sensor"
376377
asset.sensors_to_show = [
@@ -380,12 +381,17 @@ def test_asset_sensors_metadata_old_sensors_to_show_format(db, add_weather_senso
380381
db.session.add(asset)
381382
db.session.flush()
382383

383-
# Should not raise a KeyError
384-
status_data = get_asset_sensors_metadata(asset=asset)
385-
386-
sensor_ids = [s["id"] for s in status_data]
387-
assert wind_sensor.id in sensor_ids
388-
assert temperature_sensor.id in sensor_ids
384+
try:
385+
# Should not raise a KeyError
386+
status_data = get_asset_sensors_metadata(asset=asset)
387+
388+
sensor_ids = [s["id"] for s in status_data]
389+
assert wind_sensor.id in sensor_ids
390+
assert temperature_sensor.id in sensor_ids
391+
finally:
392+
# Reset to avoid polluting shared module-scoped fixture state
393+
asset.sensors_to_show = []
394+
db.session.flush()
389395

390396

391397
def test_asset_sensors_metadata(

0 commit comments

Comments
 (0)