From 753b9ffc96e0bb5fbe69dd8ed6c9fb51bbc9f240 Mon Sep 17 00:00:00 2001 From: androiddrew Date: Thu, 7 Sep 2017 16:31:38 -0400 Subject: [PATCH] Added new uncompleted service with tests --- README.md | 2 +- apitesting/services.py | 9 +++++++++ tests/test_todos.py | 32 +++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0664704..5fefeb9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # apitesting -A sample project illustrating how to write tests against nd external APIa \ No newline at end of file +A sample project illustrating how to write tests against an external API \ No newline at end of file diff --git a/apitesting/services.py b/apitesting/services.py index 45949b3..9469225 100644 --- a/apitesting/services.py +++ b/apitesting/services.py @@ -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] diff --git a/tests/test_todos.py b/tests/test_todos.py index c9b9499..5b74128 100644 --- a/tests/test_todos.py +++ b/tests/test_todos.py @@ -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 == []