-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_flask-tdd-todo.py
76 lines (64 loc) · 2.68 KB
/
test_flask-tdd-todo.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
import unittest
import json
from app import create_app, db
class TodoAPPTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app(config_name="testing")
self.client = self.app.test_client
self.bucketlist = {'name': 'buy pinneapples for hell pizza.'}
self.db_uri = 'sqlite:///test.db'
self.app.config['TESTING'] = True
self.app.config['WTF_CSRF_ENABLED'] = False
self.app.config['SQLALCHEMY_DATABASE_URI'] = self.db_uri
db.create_all()
def test_bucketlist_creation(self):
"""Test API can create"""
res = self.client().post('/todolist/', data=self.bucketlist)
self.assertEqual(res.status_code, 201)
self.assertIn('buy pinneapples', str(res.data))
def test_api_can_get_all_todolist(self):
"""Test API can get a list of todos."""
res = self.client().post('/todolist/', data=self.bucketlist)
self.assertEqual(res.status_code, 201)
res = self.client().get('/todolist/')
self.assertEqual(res.status_code, 200)
self.assertIn('buy pinneapples', str(res.data))
def test_api_can_get_bucketlist_by_id(self):
"""Test API can get an item by it's id."""
rv = self.client().post('/todolist/', data=self.bucketlist)
self.assertEqual(rv.status_code, 201)
result_in_json = json.loads(rv.data.decode('utf-8').replace("'", "\""))
result = self.client().get(
'/todolist/{}'.format(result_in_json['id']))
self.assertEqual(result.status_code, 200)
self.assertIn('buy pinneapples', str(result.data))
def test_bucketlist_can_be_edited(self):
"""Test API can edit an existing item"""
rv = self.client().post(
'/todolist/',
data={'name': 'Get tickets.'})
self.assertEqual(rv.status_code, 201)
rv = self.client().put(
'/todolist/1',
data={
"name": "Get tickets and apples."
})
self.assertEqual(rv.status_code, 200)
results = self.client().get('/todolist/1')
self.assertIn('apples', str(results.data))
def test_bucketlist_deletion(self):
"""Test API can delete"""
rv = self.client().post(
'/todolist/',
data={'name': 'a thing to delete'})
self.assertEqual(rv.status_code, 201)
res = self.client().delete('/todolist/1')
self.assertEqual(res.status_code, 200)
result = self.client().get('/todolist/1')
self.assertEqual(result.status_code, 404)
def tearDown(self):
with self.app.app_context():
db.session.remove()
db.drop_all()
if __name__ == "__main__":
unittest.main()