|
1 | 1 | """Test the sql utils.""" |
2 | 2 |
|
| 3 | +from datetime import date |
| 4 | +from decimal import Decimal |
| 5 | + |
3 | 6 | import pytest |
4 | 7 | import voluptuous as vol |
5 | 8 |
|
6 | 9 | from homeassistant.components.recorder import Recorder, get_instance |
7 | | -from homeassistant.components.sql.util import resolve_db_url, validate_sql_select |
| 10 | +from homeassistant.components.sql.util import ( |
| 11 | + ensure_serializable, |
| 12 | + resolve_db_url, |
| 13 | + validate_sql_select, |
| 14 | +) |
8 | 15 | from homeassistant.core import HomeAssistant |
9 | 16 |
|
10 | 17 |
|
@@ -64,3 +71,22 @@ async def test_invalid_sql_queries( |
64 | 71 | """Test that various invalid or disallowed SQL queries raise the correct exception.""" |
65 | 72 | with pytest.raises(vol.Invalid, match=expected_error_message): |
66 | 73 | validate_sql_select(sql_query) |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.parametrize( |
| 77 | + ("input", "expected_output"), |
| 78 | + [ |
| 79 | + (Decimal("199.99"), 199.99), |
| 80 | + (date(2023, 1, 15), "2023-01-15"), |
| 81 | + (b"\xde\xad\xbe\xef", "0xdeadbeef"), |
| 82 | + ("deadbeef", "deadbeef"), |
| 83 | + (199.99, 199.99), |
| 84 | + (69, 69), |
| 85 | + ], |
| 86 | +) |
| 87 | +async def test_data_conversion( |
| 88 | + input: Decimal | date | bytes | str | float, |
| 89 | + expected_output: str | float, |
| 90 | +) -> None: |
| 91 | + """Test data conversion to serializable type.""" |
| 92 | + assert ensure_serializable(input) == expected_output |
0 commit comments