-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_lambda.py
147 lines (119 loc) · 5.16 KB
/
test_lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import json
import os
from pathlib import Path
import pytest
from lambda_function import lambda_handler
# Tests assume there are two doors and both are closed
# Set MOCK_MYQ_COMMANDS to False in order to test open/close commands
# Even when true, tests will still authenticate and get current state
MOCK_MYQ_COMMANDS = True
def slot_value(slot_type, spoken_value, name=None, value_id=None):
if name is None:
name = spoken_value
if value_id is None:
value_id = name
return {
'value': spoken_value,
'resolutions': {
'resolutionsPerAuthority': [
{
'authority': f'amzn1.er-authority.echo-sdk.<skill_id>.{slot_type}',
'status': {
'code': 'ER_SUCCESS_MATCH'
},
'values': [
{
'value': {
'name': name,
'id': value_id
}
}
]
}
]
}
}
left_door_name = slot_value('DoorName', 'the left door', 'left')
one_door_name = slot_value('DoorName', '1', 'left')
both_door_name = slot_value('DoorName', 'both doors', 'both')
closed_door_state = slot_value('DoorState', 'closed')
open_door_action = slot_value('DoorCommand', 'open')
close_door_action = slot_value('DoorCommand', 'shut', 'close')
@pytest.fixture()
def event():
with Path('event.json').open() as f:
return json.load(f)
def test_launch(event):
event['request']['type'] = 'LaunchRequest'
result = lambda_handler(event)
assert result['response']['outputSpeech']['text'].startswith('You can open or close the left or right door')
def test_state(event):
event['request']['type'] = 'IntentRequest'
event['request']['intent'] = {
'name': 'StateIntent',
'slots': {
'Name': left_door_name,
'State': closed_door_state
}}
result = lambda_handler(event)
assert result['response']['outputSpeech']['text'] == 'Yes, the left door is closed'
def test_all_states(event):
event['request']['type'] = 'IntentRequest'
event['request']['intent'] = {'name': 'AllStatesIntent'}
result = lambda_handler(event)
assert result['response']['outputSpeech']['text'] == 'Both doors are closed'
def test_only_close(event, mocker):
mocker.patch.dict(os.environ, {'ONLY_CLOSE': 'Y'})
event['request']['type'] = 'IntentRequest'
event['request']['intent'] = {'name': 'MoveIntent',
'slots': {
'Name': left_door_name,
'Command': open_door_action
}}
result = lambda_handler(event)
assert result['response']['outputSpeech']['text'] == 'Sorry, I can only close the door'
def test_open(event, mocker):
if MOCK_MYQ_COMMANDS:
mocker.patch('pymyq.garagedoor.MyQGaragedoor.open')
event['request']['type'] = 'IntentRequest'
event['request']['intent'] = {'name': 'MoveIntent',
'slots': {
'Name': left_door_name,
'Command': open_door_action
}}
result = lambda_handler(event)
assert result['response']['outputSpeech']['text'] == 'Ok, opening the left door now'
# WARNING: this will close the actual door if it is open
def test_close(event, mocker):
if MOCK_MYQ_COMMANDS:
mocker.patch('pymyq.garagedoor.MyQGaragedoor.close')
event['request']['type'] = 'IntentRequest'
event['request']['intent'] = {'name': 'MoveIntent',
'slots': {
'Name': one_door_name,
'Command': close_door_action
}}
result = lambda_handler(event)
assert result['response']['outputSpeech']['text'] == '1 is already closed'
# WARNING: this will close the actual doors if they are open, but test assumes they are closed
def test_close_all(event, mocker):
if MOCK_MYQ_COMMANDS:
mocker.patch('pymyq.garagedoor.MyQGaragedoor.close')
event['request']['type'] = 'IntentRequest'
event['request']['intent'] = {'name': 'MoveIntent',
'slots': {
'Name': both_door_name,
'Command': close_door_action
}}
result = lambda_handler(event)
assert result['response']['outputSpeech']['text'] == 'Both doors are closed'
def test_help(event):
event['request']['type'] = 'IntentRequest'
event['request']['intent'] = {'name': 'AMAZON.HelpIntent'}
result = lambda_handler(event)
assert result['response']['outputSpeech']['text'].startswith('You can open or close the left or right door')
def test_stop(event):
event['request']['type'] = 'IntentRequest'
event['request']['intent'] = {'name': 'AMAZON.StopIntent'}
result = lambda_handler(event)
assert result['response']['outputSpeech']['text'] == 'Goodbye'