Added new uncompleted service with tests

master
androiddrew 7 years ago
parent 7ed582f8c8
commit 753b9ffc96

@ -1,3 +1,3 @@
# apitesting
A sample project illustrating how to write tests against nd external APIa
A sample project illustrating how to write tests against an external API

@ -14,3 +14,12 @@ def get_todos():
return response
else:
return None
def get_uncompleted_todos():
response = get_todos()
if response is None:
return []
else:
todos = response.json()
return [todo for todo in todos if todo.get('completed') is False]

@ -1,7 +1,7 @@
from unittest.mock import Mock, patch
import pytest
from apitesting.services import get_todos
from apitesting.services import get_todos, get_uncompleted_todos
def test_request_response():
@ -37,3 +37,33 @@ def testing_getting_todos_when_response_is_not_ok():
response = get_todos()
assert response is None
def test_getting_uncompleted_todos_when_todos_is_not_none():
todo1 = {
'userId': 1,
'id': 1,
'title': 'Make the bed',
'completed': False
}
todo2 = {
'userId': 1,
'id': 2,
'title': 'Walk the dog',
'completed': True
}
with patch('apitesting.services.get_todos') as mock_get_todos:
mock_get_todos.return_value = Mock()
mock_get_todos.return_value.json.return_value = [todo1, todo2]
uncompleted_todos = get_uncompleted_todos()
assert mock_get_todos.called
def testing_getting_uncompleted_todos_when_todos_is_none():
with patch('apitesting.services.get_todos') as mock_get_todos:
mock_get_todos.return_value = None
uncompleted_todos = get_uncompleted_todos()
assert mock_get_todos.called
assert uncompleted_todos == []

Loading…
Cancel
Save