From 4ca0a24f87b3af4a71e3f39e1db430b71eae1751 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 13 Feb 2023 22:58:13 +0100 Subject: [PATCH] Increase test scope of MariaDB + PostgreSQL tests (#87019) Co-authored-by: J. Nick Koston --- .core_files.yaml | 3 +++ .github/workflows/ci.yaml | 10 +++++++-- tests/components/history/test_init.py | 18 ++++++++++++---- .../history/test_init_db_schema_30.py | 21 ++++++++++++++----- .../components/logbook/test_websocket_api.py | 7 ++++--- tests/components/recorder/test_statistics.py | 10 ++++----- tests/components/sensor/test_recorder.py | 2 +- 7 files changed, 51 insertions(+), 20 deletions(-) diff --git a/.core_files.yaml b/.core_files.yaml index 12ce182e5dd281..7bf7a09b36bf49 100644 --- a/.core_files.yaml +++ b/.core_files.yaml @@ -114,7 +114,10 @@ tests: &tests - tests/auth/** - tests/backports/** - tests/common.py + - tests/components/history/** + - tests/components/logbook/** - tests/components/recorder/** + - tests/components/sensor/** - tests/conftest.py - tests/hassfest/** - tests/helpers/** diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 23b5b175004751..d3438f0ccf20d4 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1012,7 +1012,10 @@ jobs: --durations=10 \ -p no:sugar \ --dburl=mysql://root:password@127.0.0.1/homeassistant-test \ - tests/components/recorder + tests/components/history \ + tests/components/logbook \ + tests/components/recorder \ + tests/components/sensor - name: Upload coverage artifact uses: actions/upload-artifact@v3.1.2 with: @@ -1116,7 +1119,10 @@ jobs: --durations-min=10 \ -p no:sugar \ --dburl=postgresql://postgres:password@127.0.0.1/homeassistant-test \ - tests/components/recorder + tests/components/history \ + tests/components/logbook \ + tests/components/recorder \ + tests/components/sensor - name: Upload coverage artifact uses: actions/upload-artifact@v3.1.0 with: diff --git a/tests/components/history/test_init.py b/tests/components/history/test_init.py index ea04b436d71cff..a5c3919505ecf9 100644 --- a/tests/components/history/test_init.py +++ b/tests/components/history/test_init.py @@ -166,10 +166,15 @@ def test_get_significant_states_without_initial(hass_history) -> None: hass = hass_history zero, four, states = record_states(hass) one = zero + timedelta(seconds=1) + one_with_microsecond = zero + timedelta(seconds=1, microseconds=1) one_and_half = zero + timedelta(seconds=1.5) for entity_id in states: states[entity_id] = list( - filter(lambda s: s.last_changed != one, states[entity_id]) + filter( + lambda s: s.last_changed != one + and s.last_changed != one_with_microsecond, + states[entity_id], + ) ) del states["media_player.test2"] @@ -587,9 +592,6 @@ def set_state(entity_id, state, **kwargs): states[mp].append( set_state(mp, "idle", attributes={"media_title": str(sentinel.mt1)}) ) - states[mp].append( - set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)}) - ) states[mp2].append( set_state(mp2, "YouTube", attributes={"media_title": str(sentinel.mt2)}) ) @@ -600,6 +602,14 @@ def set_state(entity_id, state, **kwargs): set_state(therm, 20, attributes={"current_temperature": 19.5}) ) + with patch( + "homeassistant.components.recorder.core.dt_util.utcnow", + return_value=one + timedelta(microseconds=1), + ): + states[mp].append( + set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)}) + ) + with patch( "homeassistant.components.recorder.core.dt_util.utcnow", return_value=two ): diff --git a/tests/components/history/test_init_db_schema_30.py b/tests/components/history/test_init_db_schema_30.py index 889f7aef7e1adf..392c06f84337b2 100644 --- a/tests/components/history/test_init_db_schema_30.py +++ b/tests/components/history/test_init_db_schema_30.py @@ -176,12 +176,13 @@ def test_get_significant_states_with_initial(hass_history) -> None: hass = hass_history zero, four, states = record_states(hass) one = zero + timedelta(seconds=1) + one_with_microsecond = zero + timedelta(seconds=1, microseconds=1) one_and_half = zero + timedelta(seconds=1.5) for entity_id in states: if entity_id == "media_player.test": states[entity_id] = states[entity_id][1:] for state in states[entity_id]: - if state.last_changed == one: + if state.last_changed == one or state.last_changed == one_with_microsecond: state.last_changed = one_and_half state.last_updated = one_and_half @@ -205,10 +206,15 @@ def test_get_significant_states_without_initial(hass_history) -> None: hass = hass_history zero, four, states = record_states(hass) one = zero + timedelta(seconds=1) + one_with_microsecond = zero + timedelta(seconds=1, microseconds=1) one_and_half = zero + timedelta(seconds=1.5) for entity_id in states: states[entity_id] = list( - filter(lambda s: s.last_changed != one, states[entity_id]) + filter( + lambda s: s.last_changed != one + and s.last_changed != one_with_microsecond, + states[entity_id], + ) ) del states["media_player.test2"] @@ -626,9 +632,6 @@ def set_state(entity_id, state, **kwargs): states[mp].append( set_state(mp, "idle", attributes={"media_title": str(sentinel.mt1)}) ) - states[mp].append( - set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)}) - ) states[mp2].append( set_state(mp2, "YouTube", attributes={"media_title": str(sentinel.mt2)}) ) @@ -639,6 +642,14 @@ def set_state(entity_id, state, **kwargs): set_state(therm, 20, attributes={"current_temperature": 19.5}) ) + with patch( + "homeassistant.components.recorder.core.dt_util.utcnow", + return_value=one + timedelta(microseconds=1), + ): + states[mp].append( + set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)}) + ) + with patch( "homeassistant.components.recorder.core.dt_util.utcnow", return_value=two ): diff --git a/tests/components/logbook/test_websocket_api.py b/tests/components/logbook/test_websocket_api.py index 66f720f35b80b7..f07bbfa740cf67 100644 --- a/tests/components/logbook/test_websocket_api.py +++ b/tests/components/logbook/test_websocket_api.py @@ -2324,15 +2324,16 @@ async def test_recorder_is_far_behind( assert msg["id"] == 7 assert msg["type"] == "event" assert msg["event"]["events"] == [] - - hass.bus.async_fire("mock_event", {"device_id": device.id, "message": "1"}) - await hass.async_block_till_done() + await async_wait_recording_done(hass) msg = await asyncio.wait_for(websocket_client.receive_json(), 2) assert msg["id"] == 7 assert msg["type"] == "event" assert msg["event"]["events"] == [] + hass.bus.async_fire("mock_event", {"device_id": device.id, "message": "1"}) + await hass.async_block_till_done() + msg = await asyncio.wait_for(websocket_client.receive_json(), 2) assert msg["id"] == 7 assert msg["type"] == "event" diff --git a/tests/components/recorder/test_statistics.py b/tests/components/recorder/test_statistics.py index a8ba5cef8b90ab..67d26420865c95 100644 --- a/tests/components/recorder/test_statistics.py +++ b/tests/components/recorder/test_statistics.py @@ -1438,7 +1438,7 @@ def test_delete_metadata_duplicates_many(caplog, tmpdir): session.add( recorder.db_schema.StatisticsMeta.from_meta(external_energy_metadata_1) ) - for _ in range(3000): + for _ in range(1100): session.add( recorder.db_schema.StatisticsMeta.from_meta( external_energy_metadata_1 @@ -1468,15 +1468,15 @@ def test_delete_metadata_duplicates_many(caplog, tmpdir): wait_recording_done(hass) wait_recording_done(hass) - assert "Deleted 3002 duplicated statistics_meta rows" in caplog.text + assert "Deleted 1102 duplicated statistics_meta rows" in caplog.text with session_scope(hass=hass) as session: tmp = session.query(recorder.db_schema.StatisticsMeta).all() assert len(tmp) == 3 - assert tmp[0].id == 3001 + assert tmp[0].id == 1101 assert tmp[0].statistic_id == "test:total_energy_import_tariff_1" - assert tmp[1].id == 3003 + assert tmp[1].id == 1103 assert tmp[1].statistic_id == "test:total_energy_import_tariff_2" - assert tmp[2].id == 3005 + assert tmp[2].id == 1105 assert tmp[2].statistic_id == "test:fossil_percentage" hass.stop() diff --git a/tests/components/sensor/test_recorder.py b/tests/components/sensor/test_recorder.py index 5eee2175626234..545f0df8daf17c 100644 --- a/tests/components/sensor/test_recorder.py +++ b/tests/components/sensor/test_recorder.py @@ -3094,7 +3094,7 @@ def _sum(seq, last_state, last_sum): sum_attributes, seq[j : j + 1], ) - start_meter = start + timedelta(minutes=1) + start_meter += timedelta(minutes=1) states["sensor.test4"] += _states["sensor.test4"] last_state = last_states["sensor.test4"] expected_states["sensor.test4"].append(seq[-1])