Migrate alexa tests from coroutine to async/await (#30332)

pull/30341/head
Franck Nijhof 2019-12-31 23:48:17 +01:00 committed by Andrew Sayre
parent 0280862780
commit 2ac5537495
2 changed files with 41 additions and 56 deletions

View File

@ -1,6 +1,5 @@
"""The tests for the Alexa component."""
# pylint: disable=protected-access
import asyncio
import datetime
import pytest
@ -67,21 +66,19 @@ def _flash_briefing_req(client, briefing_id):
return client.get("/api/alexa/flash_briefings/{}".format(briefing_id))
@asyncio.coroutine
def test_flash_briefing_invalid_id(alexa_client):
async def test_flash_briefing_invalid_id(alexa_client):
"""Test an invalid Flash Briefing ID."""
req = yield from _flash_briefing_req(alexa_client, 10000)
req = await _flash_briefing_req(alexa_client, 10000)
assert req.status == 404
text = yield from req.text()
text = await req.text()
assert text == ""
@asyncio.coroutine
def test_flash_briefing_date_from_str(alexa_client):
async def test_flash_briefing_date_from_str(alexa_client):
"""Test the response has a valid date parsed from string."""
req = yield from _flash_briefing_req(alexa_client, "weather")
req = await _flash_briefing_req(alexa_client, "weather")
assert req.status == 200
data = yield from req.json()
data = await req.json()
assert isinstance(
datetime.datetime.strptime(
data[0].get(const.ATTR_UPDATE_DATE), const.DATE_FORMAT
@ -90,8 +87,7 @@ def test_flash_briefing_date_from_str(alexa_client):
)
@asyncio.coroutine
def test_flash_briefing_valid(alexa_client):
async def test_flash_briefing_valid(alexa_client):
"""Test the response is valid."""
data = [
{
@ -104,9 +100,9 @@ def test_flash_briefing_valid(alexa_client):
}
]
req = yield from _flash_briefing_req(alexa_client, "news_audio")
req = await _flash_briefing_req(alexa_client, "news_audio")
assert req.status == 200
json = yield from req.json()
json = await req.json()
assert isinstance(
datetime.datetime.strptime(
json[0].get(const.ATTR_UPDATE_DATE), const.DATE_FORMAT

View File

@ -1,6 +1,5 @@
"""The tests for the Alexa component."""
# pylint: disable=protected-access
import asyncio
import json
import pytest
@ -115,8 +114,7 @@ def _intent_req(client, data=None):
)
@asyncio.coroutine
def test_intent_launch_request(alexa_client):
async def test_intent_launch_request(alexa_client):
"""Test the launch of a request."""
data = {
"version": "1.0",
@ -133,15 +131,14 @@ def test_intent_launch_request(alexa_client):
"timestamp": "2015-05-13T12:34:56Z",
},
}
req = yield from _intent_req(alexa_client, data)
req = await _intent_req(alexa_client, data)
assert req.status == 200
data = yield from req.json()
data = await req.json()
text = data.get("response", {}).get("outputSpeech", {}).get("text")
assert text == "LaunchRequest has been received."
@asyncio.coroutine
def test_intent_launch_request_not_configured(alexa_client):
async def test_intent_launch_request_not_configured(alexa_client):
"""Test the launch of a request."""
data = {
"version": "1.0",
@ -160,15 +157,14 @@ def test_intent_launch_request_not_configured(alexa_client):
"timestamp": "2015-05-13T12:34:56Z",
},
}
req = yield from _intent_req(alexa_client, data)
req = await _intent_req(alexa_client, data)
assert req.status == 200
data = yield from req.json()
data = await req.json()
text = data.get("response", {}).get("outputSpeech", {}).get("text")
assert text == "This intent is not yet configured within Home Assistant."
@asyncio.coroutine
def test_intent_request_with_slots(alexa_client):
async def test_intent_request_with_slots(alexa_client):
"""Test a request with slots."""
data = {
"version": "1.0",
@ -195,15 +191,14 @@ def test_intent_request_with_slots(alexa_client):
},
},
}
req = yield from _intent_req(alexa_client, data)
req = await _intent_req(alexa_client, data)
assert req.status == 200
data = yield from req.json()
data = await req.json()
text = data.get("response", {}).get("outputSpeech", {}).get("text")
assert text == "You told us your sign is virgo."
@asyncio.coroutine
def test_intent_request_with_slots_and_synonym_resolution(alexa_client):
async def test_intent_request_with_slots_and_synonym_resolution(alexa_client):
"""Test a request with slots and a name synonym."""
data = {
"version": "1.0",
@ -249,15 +244,14 @@ def test_intent_request_with_slots_and_synonym_resolution(alexa_client):
},
},
}
req = yield from _intent_req(alexa_client, data)
req = await _intent_req(alexa_client, data)
assert req.status == 200
data = yield from req.json()
data = await req.json()
text = data.get("response", {}).get("outputSpeech", {}).get("text")
assert text == "You told us your sign is Virgo."
@asyncio.coroutine
def test_intent_request_with_slots_and_multi_synonym_resolution(alexa_client):
async def test_intent_request_with_slots_and_multi_synonym_resolution(alexa_client):
"""Test a request with slots and multiple name synonyms."""
data = {
"version": "1.0",
@ -303,15 +297,14 @@ def test_intent_request_with_slots_and_multi_synonym_resolution(alexa_client):
},
},
}
req = yield from _intent_req(alexa_client, data)
req = await _intent_req(alexa_client, data)
assert req.status == 200
data = yield from req.json()
data = await req.json()
text = data.get("response", {}).get("outputSpeech", {}).get("text")
assert text == "You told us your sign is V zodiac."
@asyncio.coroutine
def test_intent_request_with_slots_but_no_value(alexa_client):
async def test_intent_request_with_slots_but_no_value(alexa_client):
"""Test a request with slots but no value."""
data = {
"version": "1.0",
@ -338,15 +331,14 @@ def test_intent_request_with_slots_but_no_value(alexa_client):
},
},
}
req = yield from _intent_req(alexa_client, data)
req = await _intent_req(alexa_client, data)
assert req.status == 200
data = yield from req.json()
data = await req.json()
text = data.get("response", {}).get("outputSpeech", {}).get("text")
assert text == "You told us your sign is ."
@asyncio.coroutine
def test_intent_request_without_slots(hass, alexa_client):
async def test_intent_request_without_slots(hass, alexa_client):
"""Test a request without slots."""
data = {
"version": "1.0",
@ -370,9 +362,9 @@ def test_intent_request_without_slots(hass, alexa_client):
"intent": {"name": "WhereAreWeIntent"},
},
}
req = yield from _intent_req(alexa_client, data)
req = await _intent_req(alexa_client, data)
assert req.status == 200
json = yield from req.json()
json = await req.json()
text = json.get("response", {}).get("outputSpeech", {}).get("text")
assert text == "Anne Therese is at unknown and Paulus is at unknown"
@ -380,15 +372,14 @@ def test_intent_request_without_slots(hass, alexa_client):
hass.states.async_set("device_tracker.paulus", "home")
hass.states.async_set("device_tracker.anne_therese", "home")
req = yield from _intent_req(alexa_client, data)
req = await _intent_req(alexa_client, data)
assert req.status == 200
json = yield from req.json()
json = await req.json()
text = json.get("response", {}).get("outputSpeech", {}).get("text")
assert text == "You are both home, you silly"
@asyncio.coroutine
def test_intent_request_calling_service(alexa_client):
async def test_intent_request_calling_service(alexa_client):
"""Test a request for calling a service."""
data = {
"version": "1.0",
@ -410,7 +401,7 @@ def test_intent_request_calling_service(alexa_client):
},
}
call_count = len(calls)
req = yield from _intent_req(alexa_client, data)
req = await _intent_req(alexa_client, data)
assert req.status == 200
assert call_count + 1 == len(calls)
call = calls[-1]
@ -419,15 +410,14 @@ def test_intent_request_calling_service(alexa_client):
assert call.data.get("entity_id") == ["switch.test"]
assert call.data.get("hello") == "virgo"
data = yield from req.json()
data = await req.json()
assert data["response"]["card"]["title"] == "Card title for virgo"
assert data["response"]["card"]["content"] == "Card content: virgo"
assert data["response"]["outputSpeech"]["type"] == "PlainText"
assert data["response"]["outputSpeech"]["text"] == "Service called for virgo"
@asyncio.coroutine
def test_intent_session_ended_request(alexa_client):
async def test_intent_session_ended_request(alexa_client):
"""Test the request for ending the session."""
data = {
"version": "1.0",
@ -452,14 +442,13 @@ def test_intent_session_ended_request(alexa_client):
},
}
req = yield from _intent_req(alexa_client, data)
req = await _intent_req(alexa_client, data)
assert req.status == 200
text = yield from req.text()
text = await req.text()
assert text == ""
@asyncio.coroutine
def test_intent_from_built_in_intent_library(alexa_client):
async def test_intent_from_built_in_intent_library(alexa_client):
"""Test intents from the Built-in Intent Library."""
data = {
"request": {
@ -490,8 +479,8 @@ def test_intent_from_built_in_intent_library(alexa_client):
"application": {"applicationId": APPLICATION_ID},
},
}
req = yield from _intent_req(alexa_client, data)
req = await _intent_req(alexa_client, data)
assert req.status == 200
data = yield from req.json()
data = await req.json()
text = data.get("response", {}).get("outputSpeech", {}).get("text")
assert text == "Playing the shins."