Refactor SQL's data conversion (#155598)

This commit is contained in:
David Rapan
2025-11-02 18:49:18 +01:00
committed by GitHub
parent c67e005b2c
commit 144fc2a443
4 changed files with 48 additions and 22 deletions
+28 -1
View File
@@ -1,10 +1,17 @@
"""Test the sql utils."""
from datetime import UTC, date, datetime
from decimal import Decimal
import pytest
import voluptuous as vol
from homeassistant.components.recorder import Recorder, get_instance
from homeassistant.components.sql.util import resolve_db_url, validate_sql_select
from homeassistant.components.sql.util import (
convert_value,
resolve_db_url,
validate_sql_select,
)
from homeassistant.core import HomeAssistant
@@ -64,3 +71,23 @@ async def test_invalid_sql_queries(
"""Test that various invalid or disallowed SQL queries raise the correct exception."""
with pytest.raises(vol.Invalid, match=expected_error_message):
validate_sql_select(sql_query)
@pytest.mark.parametrize(
("input", "expected_output"),
[
(Decimal("199.99"), 199.99),
(date(2023, 1, 15), "2023-01-15"),
(datetime(2023, 1, 15, 12, 30, 45, tzinfo=UTC), "2023-01-15T12:30:45+00:00"),
(b"\xde\xad\xbe\xef", "0xdeadbeef"),
("deadbeef", "deadbeef"),
(199.99, 199.99),
(69, 69),
],
)
async def test_value_conversion(
input: Decimal | date | datetime | bytes | str | float,
expected_output: str | float,
) -> None:
"""Test value conversion."""
assert convert_value(input) == expected_output