From 1fd58b6cb7597d6738b955e6e371ac9d0334b485 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 2 Feb 2023 00:47:09 -0600 Subject: [PATCH] Fix a few cartesian products in recorder tests (#87106) Fixes ``` recorder/test_init.py:251: SAWarning: SELECT statement has a cartesian product between FROM element(s) "states" and FROM element "state_attributes". Apply join condition(s) between each element to resolve. ``` --- .../recorder/test_filters_with_entityfilter.py | 8 ++++---- tests/components/recorder/test_init.py | 12 ++++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/tests/components/recorder/test_filters_with_entityfilter.py b/tests/components/recorder/test_filters_with_entityfilter.py index 89a271dac02..ef19c3c407e 100644 --- a/tests/components/recorder/test_filters_with_entityfilter.py +++ b/tests/components/recorder/test_filters_with_entityfilter.py @@ -5,7 +5,7 @@ from sqlalchemy import select from sqlalchemy.engine.row import Row from homeassistant.components.recorder import get_instance -from homeassistant.components.recorder.db_schema import EventData, States +from homeassistant.components.recorder.db_schema import EventData, Events, States from homeassistant.components.recorder.filters import ( Filters, extract_include_exclude_filter_conf, @@ -54,9 +54,9 @@ async def _async_get_states_and_events_with_filter( def _get_events_with_session(): with session_scope(hass=hass) as session: return session.execute( - select(EventData.shared_data).filter( - sqlalchemy_filter.events_entity_filter() - ) + select(EventData.shared_data) + .outerjoin(Events, EventData.data_id == Events.data_id) + .filter(sqlalchemy_filter.events_entity_filter()) ).all() filtered_events_entity_ids = set() diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index 1a47f4f17ff..b8300a18480 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -214,7 +214,11 @@ async def test_saving_state(recorder_mock, hass: HomeAssistant): with session_scope(hass=hass) as session: db_states = [] - for db_state, db_state_attributes in session.query(States, StateAttributes): + for db_state, db_state_attributes in session.query( + States, StateAttributes + ).outerjoin( + StateAttributes, States.attributes_id == StateAttributes.attributes_id + ): db_states.append(db_state) state = db_state.to_native() state.attributes = db_state_attributes.to_native() @@ -248,7 +252,11 @@ async def test_saving_state_with_nul( with session_scope(hass=hass) as session: db_states = [] - for db_state, db_state_attributes in session.query(States, StateAttributes): + for db_state, db_state_attributes in session.query( + States, StateAttributes + ).outerjoin( + StateAttributes, States.attributes_id == StateAttributes.attributes_id + ): db_states.append(db_state) state = db_state.to_native() state.attributes = db_state_attributes.to_native()