@@ -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
391397def test_asset_sensors_metadata (
0 commit comments