This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_item.py
272 lines (197 loc) · 8.28 KB
/
test_item.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""
Tests for ShoppingListService item methods.
"""
import grpc
import pytest
import shopping_list_pb2
@pytest.fixture
def add_item(service, context):
"""
Fixture for factory function to add an item to a shopping list.
"""
def _add_item(shopping_list, name, description=None):
item = shopping_list_pb2.Item(
name=name,
description=description
)
request = shopping_list_pb2.AddItemRequest(
shopping_list_id=shopping_list.id,
item=item
)
response = service.AddItem(request, context)
return response
return _add_item
@pytest.fixture
def remove_item(service, context):
"""
Fixture for factory function to remove an item from a shopping list.
"""
def _remove_item(shopping_list, item_id):
request = shopping_list_pb2.RemoveItemRequest(
shopping_list_id=shopping_list.id,
item_id=item_id
)
response = service.RemoveItem(request, context)
return response
return _remove_item
@pytest.fixture
def set_item_checked(service, context):
"""
Fixture for factory function to set whether an item is checked.
"""
def _set_item_checked(shopping_list, item, checked):
request = shopping_list_pb2.SetItemCheckedRequest(
shopping_list_id=shopping_list.id,
item_id=item.id,
checked=checked
)
response = service.SetItemChecked(request, context)
return response
return _set_item_checked
@pytest.fixture
def list_items(service, context):
"""
Fixture for factory function to list the items in a shopping list.
"""
def _list_items(shopping_list, filter=None):
request = shopping_list_pb2.ListItemsRequest(
shopping_list_id=shopping_list.id,
filter=filter
)
response = service.ListItems(request, context)
return response
return _list_items
@pytest.fixture
def list_items_stream(service, context):
"""
Fixture for factory function to list the items in a shopping list, returning
results as a stream.
"""
def _list_items_stream(shopping_list, filter=None):
request = shopping_list_pb2.ListItemsStreamRequest(
shopping_list_id=shopping_list.id,
filter=filter
)
response = service.ListItemsStream(request, context)
return response
return _list_items_stream
def test_add_item(create_shopping_list, add_item):
shopping_list = create_shopping_list('Groceries')
response = add_item(shopping_list, 'Bread', 'Whole Wheat')
item1 = response.item
assert len(item1.id) > 0
assert item1.name == 'Bread'
assert item1.description == 'Whole Wheat'
response = add_item(shopping_list, 'Yogurt', 'Full Fat')
item2 = response.item
assert len(item2.id) > 0
assert item2.id != item1.id
assert item2.name == 'Yogurt'
assert item2.description == 'Full Fat'
def test_add_item_invalid_shopping_list(context, create_shopping_list, add_item):
shopping_list = shopping_list_pb2.ShoppingList()
add_item(shopping_list, 'Bread', 'Whole Wheat')
context.set_code.assert_called_once_with(grpc.StatusCode.NOT_FOUND)
context.set_details.assert_called_once_with('Not found')
def test_remove_item(create_shopping_list, add_item, remove_item):
shopping_list = create_shopping_list('Groceries')
item1 = add_item(shopping_list, 'Bread', 'Whole Wheat').item
item2 = add_item(shopping_list, 'Bread', 'Sourdough').item
response = remove_item(shopping_list, item1.id)
assert response.item.id == item1.id
response = remove_item(shopping_list, item2.id)
assert response.item.id == item2.id
def test_remove_item_invalid_shopping_list(context, remove_item):
shopping_list = shopping_list_pb2.ShoppingList()
remove_item(shopping_list, '')
context.set_code.assert_called_once_with(grpc.StatusCode.NOT_FOUND)
context.set_details.assert_called_once_with('Shopping list not found')
def test_remove_invalid_item(context, create_shopping_list, add_item, remove_item):
shopping_list = create_shopping_list('Groceries')
add_item(shopping_list, 'Bread', 'Whole Wheat')
remove_item(shopping_list, 'invalid_id')
context.set_code.assert_called_once_with(grpc.StatusCode.NOT_FOUND)
context.set_details.assert_called_once_with('Item not found')
def test_set_item_checked(create_shopping_list, add_item, set_item_checked):
shopping_list = create_shopping_list('Groceries')
response = add_item(shopping_list, 'Bread', 'Whole Wheat')
item = response.item
assert not item.checked
item = set_item_checked(shopping_list, item, False).item
assert not item.checked
item = set_item_checked(shopping_list, item, True).item
assert item.checked
def test_set_item_checked_invalid_shopping_list(context, set_item_checked):
shopping_list = shopping_list_pb2.ShoppingList()
item = shopping_list_pb2.Item()
set_item_checked(shopping_list, item, True)
context.set_code.assert_called_once_with(grpc.StatusCode.NOT_FOUND)
context.set_details.assert_called_once_with('Shopping list not found')
def test_set_invalid_item_checked(context, create_shopping_list, add_item, set_item_checked):
shopping_list = create_shopping_list('Groceries')
item = shopping_list_pb2.Item()
set_item_checked(shopping_list, item, True)
context.set_code.assert_called_once_with(grpc.StatusCode.NOT_FOUND)
context.set_details.assert_called_once_with('Item not found')
def test_list_items(create_shopping_list, add_item, remove_item, list_items):
shopping_list = create_shopping_list('Groceries')
response = list_items(shopping_list)
assert not response.items
item1 = add_item(shopping_list, 'Salt').item
item2 = add_item(shopping_list, 'Pepper').item
response = list_items(shopping_list)
assert len(response.items) == 2
assert response.items[0].name == 'Salt'
assert response.items[1].name == 'Pepper'
remove_item(shopping_list, item1.id)
response = list_items(shopping_list)
assert len(response.items) == 1
assert response.items[0].name == 'Pepper'
remove_item(shopping_list, item2.id)
response = list_items(shopping_list)
assert not response.items
def test_list_items_filter(create_shopping_list, add_item, list_items):
shopping_list = create_shopping_list('Groceries')
add_item(shopping_list, 'Salt')
add_item(shopping_list, 'Pepper')
add_item(shopping_list, 'Pepper Jack Cheese')
add_item(shopping_list, 'Cheddar Cheese')
response = list_items(shopping_list, filter='Salt')
assert len(response.items) == 1
response = list_items(shopping_list, filter='Jack')
assert len(response.items) == 1
response = list_items(shopping_list, filter='Pepper')
assert len(response.items) == 2
response = list_items(shopping_list, filter='Cheese')
assert len(response.items) == 2
def test_list_items_stream(create_shopping_list, add_item, remove_item, list_items_stream):
shopping_list = create_shopping_list('Groceries')
response = list(list_items_stream(shopping_list))
assert not response
item1 = add_item(shopping_list, 'Salt').item
item2 = add_item(shopping_list, 'Pepper').item
items = list(list_items_stream(shopping_list))
assert len(items) == 2
assert items[0].name == 'Salt'
assert items[1].name == 'Pepper'
remove_item(shopping_list, item1.id)
items = list(list_items_stream(shopping_list))
assert len(items) == 1
assert items[0].name == 'Pepper'
remove_item(shopping_list, item2.id)
items = list(list_items_stream(shopping_list))
assert not items
def test_list_items_stream_filter(create_shopping_list, add_item, list_items_stream):
shopping_list = create_shopping_list('Groceries')
add_item(shopping_list, 'Salt')
add_item(shopping_list, 'Pepper')
add_item(shopping_list, 'Pepper Jack Cheese')
add_item(shopping_list, 'Cheddar Cheese')
items = list(list_items_stream(shopping_list, filter='Salt'))
assert len(items) == 1
items = list(list_items_stream(shopping_list, filter='Jack'))
assert len(items) == 1
items = list(list_items_stream(shopping_list, filter='Pepper'))
assert len(items) == 2
items = list(list_items_stream(shopping_list, filter='Cheese'))
assert len(items) == 2