1- import toml , pytest , os , sys , tempfile , mock , json
1+ import toml
2+ import pytest
3+ import os
4+ import sys
5+ import tempfile
6+ import mock
7+ import json
28from flask import Flask
39
10+
411@pytest .fixture
512def client ():
613 with mock .patch ('flask.Flask' , lambda x : Flask (x )):
@@ -16,13 +23,16 @@ def client():
1623 os .close (db_fd )
1724 os .unlink (app .config ['DATABASE' ])
1825
26+
1927@pytest .mark .it ("Folder src must exist" )
2028def test_src_folder ():
21- assert os .path .isdir ("./src/" )
29+ assert os .path .isdir ("./src/" )
30+
2231
2332@pytest .mark .it ("File app.py must exist" )
2433def test_pipfile_exists ():
25- assert os .path .isfile ("src/app.py" )
34+ assert os .path .isfile ("src/app.py" )
35+
2636
2737@pytest .mark .it ("Declare a global variable todos, outside any function" )
2838def test_todos_exist ():
@@ -32,44 +42,51 @@ def test_todos_exist():
3242 except AttributeError :
3343 raise AttributeError ("The variable 'todos' should exist on app.py" )
3444
45+
3546@pytest .mark .it ("Variable todos must be a list" )
3647def test_todos_should_be_list ():
3748 from src import app
3849 assert isinstance (app .todos , list )
3950
51+
4052@pytest .mark .it ("The global todos list needs to have at least one dummy todo with the required format" )
4153def test_one_dummy ():
4254 from src import app
4355 assert len (app .todos ) > 0
4456
57+
4558@pytest .mark .it ('Each item inside the global todos list should have the following format: { "label": "Sample", "done": True }' )
4659def test_items_format ():
4760 from src import app
4861 for task in app .todos :
4962 assert "label" in task
5063 assert "done" in task
5164
65+
5266@pytest .mark .it ('Each item inside the global todos variable should be a dictionary with two keys: "label" and "done"' )
5367def test_label_and_done ():
5468 from src import app
5569 for task in app .todos :
5670 assert "label" in task
5771 assert "done" in task
5872
73+
5974@pytest .mark .it ('The value of the "label" of each of the todos should be a string' )
6075def test_labels_string ():
6176 from src import app
6277 for task in app .todos :
6378 if "label" in task :
6479 assert isinstance (task ["label" ], str )
6580
81+
6682@pytest .mark .it ('The value of the "done" key on each todo should be a boolean' )
6783def test_done_bool ():
6884 from src import app
6985 for task in app .todos :
7086 if "done" in task :
7187 assert isinstance (task ["done" ], bool )
7288
89+
7390@pytest .mark .it ("The response of the hello_world function should be a json, remember to use jsonify" )
7491def test_return (client ):
7592 response = client .get ('/todos' )
@@ -86,16 +103,19 @@ def test_return(client):
86103 assert "label" in task
87104 assert "done" in task
88105
106+
89107@pytest .mark .it ("The function add_new_todo should be declared" )
90108def test_add_new_todo ():
91109 from src import app
92110 try :
93111 app .add_new_todo
94112 assert callable (app .add_new_todo )
95113 except AttributeError :
96- raise AttributeError ("The function 'add_new_todo' should exist on app.py" )
114+ raise AttributeError (
115+ "The function 'add_new_todo' should exist on app.py" )
116+
97117
98118@pytest .mark .it ("The endpoint POST /todos should exist" )
99119def test_return (client ):
100- response = client .post ('/todos' )
120+ response = client .post ('/todos' , json = json . dumps ({}) )
101121 assert response .status_code in [200 , 201 ]
0 commit comments