Debian Codegraph @

tests/test_voice_api.py

raw
1 """Test the voice connection details API."""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING, Any
6
7 from aiohttp import ClientError
8 import pytest
9 from syrupy import SnapshotAssertion
10
11 from hass_nabucasa.voice_api import (
12 VoiceApiError,
13 VoiceConnectionDetails,
14 )
15 from tests.common import extract_log_messages
16
17 if TYPE_CHECKING:
18 from hass_nabucasa import Cloud
19 from tests.utils.aiohttp import AiohttpClientMocker
20
21
22 @pytest.mark.parametrize(
23 "exception,getmockargs,exception_msg",
24 [
25 [
26 VoiceApiError,
27 {"status": 500, "text": "Internal Server Error"},
28 "Failed to parse API response",
29 ],
30 [
31 VoiceApiError,
32 {"status": 429, "text": "Too fast"},
33 "Failed to parse API response",
34 ],
35 [
36 VoiceApiError,
37 {"exc": TimeoutError()},
38 "Timeout reached while calling API",
39 ],
40 [
41 VoiceApiError,
42 {"exc": ClientError("boom!")},
43 "Failed to fetch: boom!",
44 ],
45 [
46 VoiceApiError,
47 {"exc": Exception("boom!")},
48 "Unexpected error while calling API: boom!",
49 ],
50 ],
51 )
52 async def test_problems_getting_connection_details(
53 aioclient_mock: AiohttpClientMocker,
54 cloud: Cloud,
55 exception: Exception,
56 getmockargs,
57 exception_msg,
58 caplog: pytest.LogCaptureFixture,
59 snapshot: SnapshotAssertion,
60 ):
61 """Test problems getting connection details."""
62 aioclient_mock.get(
63 f"https://{cloud.api_server}/voice/connection_details",
64 **getmockargs,
65 )
66
67 with pytest.raises(exception, match=exception_msg):
68 await cloud.voice_api.connection_details()
69
70 assert extract_log_messages(caplog) == snapshot
71
72
73 @pytest.mark.parametrize(
74 "response",
75 [
76 {
77 "valid": "123456789",
78 "authorized_key": "test_key",
79 "endpoint_stt": "http://example.com/stt",
80 "endpoint_tts": "http://example.com/tts",
81 }
82 ],
83 )
84 async def test_getting_connection_details(
85 aioclient_mock: AiohttpClientMocker,
86 cloud: Cloud,
87 response: dict[str, Any],
88 caplog: pytest.LogCaptureFixture,
89 snapshot: SnapshotAssertion,
90 ):
91 """Test getting connection details."""
92 aioclient_mock.get(
93 f"https://{cloud.api_server}/voice/connection_details",
94 json=response,
95 )
96
97 details = await cloud.voice_api.connection_details()
98
99 assert details == VoiceConnectionDetails(**response)
100 assert extract_log_messages(caplog) == snapshot