/
test_shopping_list.py
194 lines (151 loc) · 5.3 KB
/
test_shopping_list.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""Test shopping list component."""
import asyncio
from unittest.mock import patch
import pytest
from homeassistant.bootstrap import async_setup_component
from homeassistant.helpers import intent
@pytest.fixture(autouse=True)
def mock_shopping_list_save():
"""Stub out the persistence."""
with patch('homeassistant.components.shopping_list.ShoppingData.save'):
yield
@asyncio.coroutine
def test_add_item(hass):
"""Test adding an item intent."""
yield from async_setup_component(hass, 'shopping_list', {})
response = yield from intent.async_handle(
hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'beer'}}
)
assert response.speech['plain']['speech'] == \
"I've added beer to your shopping list"
@asyncio.coroutine
def test_recent_items_intent(hass):
"""Test recent items."""
yield from async_setup_component(hass, 'shopping_list', {})
yield from intent.async_handle(
hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'beer'}}
)
yield from intent.async_handle(
hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'wine'}}
)
yield from intent.async_handle(
hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'soda'}}
)
response = yield from intent.async_handle(
hass, 'test', 'HassShoppingListLastItems'
)
assert response.speech['plain']['speech'] == \
"These are the top 3 items on your shopping list: soda, wine, beer"
@asyncio.coroutine
def test_api_get_all(hass, test_client):
"""Test the API."""
yield from async_setup_component(hass, 'shopping_list', {})
yield from intent.async_handle(
hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'beer'}}
)
yield from intent.async_handle(
hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'wine'}}
)
client = yield from test_client(hass.http.app)
resp = yield from client.get('/api/shopping_list')
assert resp.status == 200
data = yield from resp.json()
assert len(data) == 2
assert data[0]['name'] == 'beer'
assert not data[0]['complete']
assert data[1]['name'] == 'wine'
assert not data[1]['complete']
@asyncio.coroutine
def test_api_update(hass, test_client):
"""Test the API."""
yield from async_setup_component(hass, 'shopping_list', {})
yield from intent.async_handle(
hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'beer'}}
)
yield from intent.async_handle(
hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'wine'}}
)
beer_id = hass.data['shopping_list'].items[0]['id']
wine_id = hass.data['shopping_list'].items[1]['id']
client = yield from test_client(hass.http.app)
resp = yield from client.post(
'/api/shopping_list/item/{}'.format(beer_id), json={
'name': 'soda'
})
assert resp.status == 200
data = yield from resp.json()
assert data == {
'id': beer_id,
'name': 'soda',
'complete': False
}
resp = yield from client.post(
'/api/shopping_list/item/{}'.format(wine_id), json={
'complete': True
})
assert resp.status == 200
data = yield from resp.json()
assert data == {
'id': wine_id,
'name': 'wine',
'complete': True
}
beer, wine = hass.data['shopping_list'].items
assert beer == {
'id': beer_id,
'name': 'soda',
'complete': False
}
assert wine == {
'id': wine_id,
'name': 'wine',
'complete': True
}
@asyncio.coroutine
def test_api_update_fails(hass, test_client):
"""Test the API."""
yield from async_setup_component(hass, 'shopping_list', {})
yield from intent.async_handle(
hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'beer'}}
)
client = yield from test_client(hass.http.app)
resp = yield from client.post(
'/api/shopping_list/non_existing', json={
'name': 'soda'
})
assert resp.status == 404
beer_id = hass.data['shopping_list'].items[0]['id']
client = yield from test_client(hass.http.app)
resp = yield from client.post(
'/api/shopping_list/item/{}'.format(beer_id), json={
'name': 123,
})
assert resp.status == 400
@asyncio.coroutine
def test_api_clear_completed(hass, test_client):
"""Test the API."""
yield from async_setup_component(hass, 'shopping_list', {})
yield from intent.async_handle(
hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'beer'}}
)
yield from intent.async_handle(
hass, 'test', 'HassShoppingListAddItem', {'item': {'value': 'wine'}}
)
beer_id = hass.data['shopping_list'].items[0]['id']
wine_id = hass.data['shopping_list'].items[1]['id']
client = yield from test_client(hass.http.app)
# Mark beer as completed
resp = yield from client.post(
'/api/shopping_list/item/{}'.format(beer_id), json={
'complete': True
})
assert resp.status == 200
resp = yield from client.post('/api/shopping_list/clear_completed')
assert resp.status == 200
items = hass.data['shopping_list'].items
assert len(items) == 1
assert items[0] == {
'id': wine_id,
'name': 'wine',
'complete': False
}