From 557cc792e90ce5b759833fccd5b73d8e84a30b45 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 8 Aug 2021 20:33:47 -0700 Subject: [PATCH] Fix SQLAlchemy test warnings (#54116) --- homeassistant/components/history/__init__.py | 2 +- homeassistant/components/recorder/models.py | 3 +-- homeassistant/components/recorder/util.py | 4 +++- tests/components/recorder/test_util.py | 10 ++++++++-- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/history/__init__.py b/homeassistant/components/history/__init__.py index 3651dd8295f..a1e0fd45167 100644 --- a/homeassistant/components/history/__init__.py +++ b/homeassistant/components/history/__init__.py @@ -393,7 +393,7 @@ class Filters: if includes and not excludes: return or_(*includes) - if not excludes and includes: + if not includes and excludes: return not_(or_(*excludes)) return or_(*includes) & not_(or_(*excludes)) diff --git a/homeassistant/components/recorder/models.py b/homeassistant/components/recorder/models.py index 929115bdf25..ff64deb60cd 100644 --- a/homeassistant/components/recorder/models.py +++ b/homeassistant/components/recorder/models.py @@ -20,8 +20,7 @@ from sqlalchemy import ( distinct, ) from sqlalchemy.dialects import mysql -from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import relationship +from sqlalchemy.orm import declarative_base, relationship from sqlalchemy.orm.session import Session from homeassistant.const import ( diff --git a/homeassistant/components/recorder/util.py b/homeassistant/components/recorder/util.py index 225eee6867f..e3af39b217a 100644 --- a/homeassistant/components/recorder/util.py +++ b/homeassistant/components/recorder/util.py @@ -10,6 +10,7 @@ import os import time from typing import TYPE_CHECKING, Callable +from sqlalchemy import text from sqlalchemy.exc import OperationalError, SQLAlchemyError from sqlalchemy.orm.session import Session @@ -332,4 +333,5 @@ def perodic_db_cleanups(instance: Recorder): if instance.engine.dialect.name == "sqlite": # Execute sqlite to create a wal checkpoint and free up disk space _LOGGER.debug("WAL checkpoint") - instance.engine.execute("PRAGMA wal_checkpoint(TRUNCATE);") + with instance.engine.connect() as connection: + connection.execute(text("PRAGMA wal_checkpoint(TRUNCATE);")) diff --git a/tests/components/recorder/test_util.py b/tests/components/recorder/test_util.py index 5b4b234fbbb..cb54f0404b9 100644 --- a/tests/components/recorder/test_util.py +++ b/tests/components/recorder/test_util.py @@ -6,6 +6,7 @@ from unittest.mock import MagicMock, patch import pytest from sqlalchemy import text +from sqlalchemy.sql.elements import TextClause from homeassistant.components.recorder import run_information_with_session, util from homeassistant.components.recorder.const import DATA_INSTANCE, SQLITE_URL_PREFIX @@ -253,6 +254,11 @@ def test_end_incomplete_runs(hass_recorder, caplog): def test_perodic_db_cleanups(hass_recorder): """Test perodic db cleanups.""" hass = hass_recorder() - with patch.object(hass.data[DATA_INSTANCE].engine, "execute") as execute_mock: + with patch.object(hass.data[DATA_INSTANCE].engine, "connect") as connect_mock: util.perodic_db_cleanups(hass.data[DATA_INSTANCE]) - assert execute_mock.call_args[0][0] == "PRAGMA wal_checkpoint(TRUNCATE);" + + text_obj = connect_mock.return_value.__enter__.return_value.execute.mock_calls[0][ + 1 + ][0] + assert isinstance(text_obj, TextClause) + assert str(text_obj) == "PRAGMA wal_checkpoint(TRUNCATE);"