-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
168 lines (133 loc) · 4.88 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from unittest import TestCase
from app import app
from models import db, Cupcake
# Use test database and don't clutter tests with SQL
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///cupcakes_test'
app.config['SQLALCHEMY_ECHO'] = False
# Make Flask errors be real errors, rather than HTML pages with error info
app.config['TESTING'] = True
db.drop_all()
db.create_all()
CUPCAKE_DATA = {
"flavor": "TestFlavor",
"size": "TestSize",
"rating": 5,
"image": "http://test.com/cupcake.jpg"
}
CUPCAKE_DATA_2 = {
"flavor": "TestFlavor2",
"size": "TestSize2",
"rating": 10,
"image": "http://test.com/cupcake2.jpg"
}
class CupcakeViewsTestCase(TestCase):
"""Tests for views of API."""
def setUp(self):
"""Make demo data."""
Cupcake.query.delete()
cupcake = Cupcake(**CUPCAKE_DATA)
db.session.add(cupcake)
db.session.commit()
self.cupcake = cupcake
def tearDown(self):
"""Clean up fouled transactions."""
db.session.rollback()
def test_list_cupcakes(self):
with app.test_client() as client:
resp = client.get("/api/cupcakes")
self.assertEqual(resp.status_code, 200)
data = resp.json
self.assertEqual(data, {
"cupcakes": [
{
"id": self.cupcake.id,
"flavor": "TestFlavor",
"size": "TestSize",
"rating": 5,
"image": "http://test.com/cupcake.jpg"
}
]
})
def test_get_cupcake(self):
with app.test_client() as client:
url = f"/api/cupcakes/{self.cupcake.id}"
resp = client.get(url)
self.assertEqual(resp.status_code, 200)
data = resp.json
self.assertEqual(data, {
"cupcake": {
"id": self.cupcake.id,
"flavor": "TestFlavor",
"size": "TestSize",
"rating": 5,
"image": "http://test.com/cupcake.jpg"
}
})
def test_create_cupcake(self):
with app.test_client() as client:
url = "/api/cupcakes"
resp = client.post(url, json=CUPCAKE_DATA_2)
self.assertEqual(resp.status_code, 201)
data = resp.json
# don't know what ID we'll get, make sure it's an int & normalize
self.assertIsInstance(data['cupcake']['id'], int)
del data['cupcake']['id']
self.assertEqual(data, {
"cupcake": {
"flavor": "TestFlavor2",
"size": "TestSize2",
"rating": 10,
"image": "http://test.com/cupcake2.jpg"
}
})
self.assertEqual(Cupcake.query.count(), 2)
def test_update_cupcake(self):
with app.test_client() as client:
url = f"/api/cupcakes/{self.cupcake.id}"
cupcake = {
"flavor": "TestFlavorUpdate",
"size": "TestSizeUpdate",
"rating": 6,
"image": "http://test.com/cupcakeupdate.jpg"
}
resp = client.patch(url, json=cupcake)
self.assertEqual(resp.status_code, 200)
data = resp.json
self.assertEqual(data, {
"cupcake": {
"id": self.cupcake.id,
"flavor": "TestFlavorUpdate",
"size": "TestSizeUpdate",
"rating": 6,
"image": "http://test.com/cupcakeupdate.jpg"
}
})
def test_delete_cupcake(self):
with app.test_client() as client:
url = f"/api/cupcakes/{self.cupcake.id}"
resp = client.delete(url)
self.assertEqual(resp.status_code, 200)
data = resp.json
self.assertEqual(data, {"message": "Deleted"})
self.assertEqual(Cupcake.query.count(), 0)
def test_get_cupcake_404(self):
with app.test_client() as client:
url = "/api/cupcakes/100"
resp = client.get(url)
self.assertEqual(resp.status_code, 404)
def test_patch_cupcake_404(self):
with app.test_client() as client:
url = "/api/cupcakes/100"
cupcake = {
"flavor": "TestFlavorUpdate",
"size": "TestSizeUpdate",
"rating": 6,
"image": "http://test.com/cupcakeupdate.jpg"
}
resp = client.patch(url, json=cupcake)
self.assertEqual(resp.status_code, 404)
def test_delete_cupcake_404(self):
with app.test_client() as client:
url = "/api/cupcakes/100"
resp = client.delete(url)
self.assertEqual(resp.status_code, 404)