forked from mjsir911/flasktodo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
148 lines (130 loc) · 4.85 KB
/
tests.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
import json
import os
import unittest
from todo.tasks import get_connection
from todo.tasks import drop_testing_database
from todo.tasks import make_tasks_list
from todo.todo import app
os.environ['TESTING'] = 'True'
class AppTests(unittest.TestCase):
def setUp(self):
self.connection = get_connection()
schema = '''CREATE TABLE items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NUll,
date TEXT NOT NULL,
description TEXT,
done BOOLEAN
);'''
self.connection.execute(schema)
self.client = app.test_client()
def tearDown(self):
self.connection.close()
drop_testing_database()
def _add_items(self):
# Add a couple of items
sql = '''INSERT INTO "items" VALUES(
1,
'Buy groceries',
'2017-03-15 11:51',
'Milk, Cheese, Pizza, Fruit, Tylenol',
0)'''
self.connection.execute(sql)
sql = '''INSERT INTO "items" VALUES(
2,
'Learn SQL',
'2017-03-15 11:54',
'Complete Khan Academy SQL tutorial.',
0)'''
self.connection.execute(sql)
self.connection.commit()
def test_make_tasks_list(self):
# Add a couple of items
self._add_items()
# Get the items
cursor = self.connection.execute('SELECT * FROM items ORDER BY id')
items = cursor.fetchall()
tasklist = make_tasks_list(items)
# A task is created for each item
self.assertEqual(len(tasklist), 2)
# Check the attributes of the task for the first item
first_task = tasklist[0]
self.assertEqual(first_task['id'], 1)
self.assertEqual(first_task['title'], 'Buy groceries')
self.assertEqual(first_task['date'], '2017-03-15 11:51')
self.assertEqual(
first_task['description'],
'Milk, Cheese, Pizza, Fruit, Tylenol'
)
self.assertEqual(first_task['done'], False)
def test_get_tasks_endpoint(self):
# Add a couple of items
self._add_items()
url = '/todo/api/v1.0/tasks'
response = self.client.get(url)
result = response.get_data(as_text=True)
tasks = json.loads(result)['tasks']
# A task is created for each item
self.assertEqual(len(tasks), 2)
# Check the attributes of the task for the first item
first_task = sorted(tasks, key=lambda task: task['uri'])[0]
self.assertEqual(
first_task['uri'],
'http://localhost/todo/api/v1.0/tasks/1'
)
self.assertEqual(first_task['title'], 'Buy groceries')
self.assertEqual(
first_task['description'],
'Milk, Cheese, Pizza, Fruit, Tylenol'
)
self.assertEqual(first_task['date'], '2017-03-15 11:51')
self.assertEqual(first_task['done'], False)
def test_get_task_endpoint(self):
# Add a couple of items
self._add_items()
# Get the first one
url = '/todo/api/v1.0/tasks/1'
response = self.client.get(url)
result = response.get_data(as_text=True)
task = json.loads(result)
# There should be a single task
self.assertEqual(len(task), 1)
# with the title 'Buy groceries'
self.assertEqual(task['task']['title'], 'Buy groceries')
def test_delete_task_endpoint(self):
# Get the number of tasks in tasklist before delete
self._add_items()
url = '/todo/api/v1.0/tasks'
response = self.client.get(url)
result = response.get_data(as_text=True)
tasks = json.loads(result)['tasks']
num_tasks_before = len(tasks)
# Delete item 2
url = '/todo/api/v1.0/tasks/2'
response = self.client.delete(url)
# Get the number of tasks in tasklist after delete
url = '/todo/api/v1.0/tasks'
response = self.client.get(url)
result = response.get_data(as_text=True)
tasks = json.loads(result)['tasks']
num_tasks_after = len(tasks)
self.assertEqual(num_tasks_before-1, num_tasks_after)
def test_update_task_endpoint(self):
# Get task and confirm it's not done
self._add_items()
url = '/todo/api/v1.0/tasks/1'
response = self.client.get(url)
result = response.get_data(as_text=True)
task = json.loads(result)
self.assertEqual(task['task']['done'], False)
# Set done to true
data = {'done': True}
headers = {'content-type': 'application/json'}
self.client.put(url, headers=headers, data=json.dumps(data))
# Now confirm it is set in the database
response = self.client.get(url)
result = response.get_data(as_text=True)
task = json.loads(result)
self.assertEqual(task['task']['done'], True)
if __name__ == '__main__':
unittest.main()