Skip to content

Commit 0e3971f

Browse files
committed
Add service function for uncompleted and test
1 parent d9a42ac commit 0e3971f

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

testing/test003/services.py

+8
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ def get_todos():
1414
return response
1515
else:
1616
return None
17+
18+
def get_uncompleted_todos():
19+
response = get_todos()
20+
if response is None:
21+
return []
22+
else:
23+
todos = response.json()
24+
return [todo for todo in todos if todo.get('completed') == False]

testing/test003/tests/test_todos.py

+23-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
from unittest.mock import Mock, patch
2-
from nose.tools import assert_is_none, assert_list_equal
3-
from test003.services import get_todos
2+
from nose.tools import assert_list_equal, assert_true
3+
from test003.services import get_uncompleted_todos
44

55

6-
@patch('test003.services.requests.get')
7-
def test_getting_todos_when_response_is_ok(mock_get):
8-
todos = [{
6+
@patch('test003.services.get_todos')
7+
def test_getting_uncompleted_todos_when_todos_is_not_none(mock_get_todos):
8+
todo1 = {
99
'userId': 1,
1010
'id': 1,
1111
'title': 'Make the bed',
1212
'completed': False
13-
}]
13+
}
14+
todo2 = {
15+
'userId': 1,
16+
'id': 2,
17+
'title': 'Walk the dog',
18+
'completed': True
19+
}
1420

15-
mock_get.return_value = Mock(ok=True)
16-
mock_get.return_value.json.return_value = todos
17-
response = get_todos()
18-
assert_list_equal(response.json(), todos)
21+
mock_get_todos.return_value = Mock()
22+
mock_get_todos.return_value.json.return_value = [todo1, todo2]
23+
uncompleted_todos = get_uncompleted_todos()
24+
assert_true(mock_get_todos.called)
25+
assert_list_equal(uncompleted_todos, [todo1])
1926

2027

21-
@patch('test003.services.requests.get')
22-
def test_getting_todos_when_response_is_not_ok(mock_get):
23-
mock_get.return_value.ok = False
24-
response = get_todos()
25-
assert_is_none(response)
28+
@patch('test003.services.get_todos')
29+
def test_getting_uncompleted_todos_when_todos_is_none(mock_get_todos):
30+
mock_get_todos.return_value = None
31+
uncompleted_todos = get_uncompleted_todos()
32+
assert_true(mock_get_todos.called)
33+
assert_list_equal(uncompleted_todos, [])

0 commit comments

Comments
 (0)