Debian Codegraph @

tests/test_google_report_state_sync.py

raw
1 """Tests for Google Actions functionality in GoogleReportState."""
2
3 from __future__ import annotations
4
5 import re
6 from typing import TYPE_CHECKING, Any
7
8 from aiohttp import ClientError
9 import pytest
10 from syrupy import SnapshotAssertion
11
12 from hass_nabucasa.google_report_state import GoogleReportState, GoogleReportStateError
13 from tests.common import extract_log_messages
14
15 if TYPE_CHECKING:
16 from hass_nabucasa import Cloud
17 from tests.utils.aiohttp import AiohttpClientMocker
18
19
20 @pytest.mark.parametrize(
21 "exception,postmockargs,exception_msg",
22 [
23 [
24 GoogleReportStateError,
25 {"exc": TimeoutError()},
26 "Timeout reached while calling API",
27 ],
28 [
29 GoogleReportStateError,
30 {"exc": ClientError("boom!")},
31 "Failed to fetch: boom!",
32 ],
33 [
34 GoogleReportStateError,
35 {"exc": Exception("boom!")},
36 "Unexpected error while calling API: boom!",
37 ],
38 ],
39 ids=["timeout", "client-error", "unexpected-error"],
40 )
41 async def test_problems_requesting_sync(
42 aioclient_mock: AiohttpClientMocker,
43 cloud: Cloud,
44 exception: Exception,
45 postmockargs: dict[str, Any],
46 exception_msg: str,
47 caplog: pytest.LogCaptureFixture,
48 snapshot: SnapshotAssertion,
49 ):
50 """Test problems requesting Google Actions sync."""
51 google_report_state = GoogleReportState(cloud)
52 aioclient_mock.post(
53 f"https://{cloud.remotestate_server}/request_sync",
54 **postmockargs,
55 )
56
57 with pytest.raises(exception, match=re.escape(exception_msg)):
58 await google_report_state.request_sync()
59
60 assert extract_log_messages(caplog) == snapshot
61
62
63 @pytest.mark.parametrize(
64 "status,text",
65 [
66 [500, "Internal Server Error"],
67 [429, "Too fast"],
68 ],
69 ids=["500-error", "429-error"],
70 )
71 async def test_request_sync_error_status(
72 aioclient_mock: AiohttpClientMocker,
73 cloud: Cloud,
74 status: int,
75 text: str,
76 caplog: pytest.LogCaptureFixture,
77 snapshot: SnapshotAssertion,
78 ):
79 """Test Google Actions sync request with error status codes."""
80 google_report_state = GoogleReportState(cloud)
81 aioclient_mock.post(
82 f"https://{cloud.remotestate_server}/request_sync",
83 status=status,
84 text=text,
85 )
86
87 response = await google_report_state.request_sync()
88
89 assert response.status == status
90 assert extract_log_messages(caplog) == snapshot
91
92
93 async def test_request_sync_success(
94 aioclient_mock: AiohttpClientMocker,
95 cloud: Cloud,
96 caplog: pytest.LogCaptureFixture,
97 snapshot: SnapshotAssertion,
98 ):
99 """Test successful Google Actions sync request."""
100 google_report_state = GoogleReportState(cloud)
101 aioclient_mock.post(
102 f"https://{cloud.remotestate_server}/request_sync",
103 status=200,
104 )
105
106 response = await google_report_state.request_sync()
107
108 assert response.status == 200
109 assert extract_log_messages(caplog) == snapshot