-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
109 lines (83 loc) · 2.91 KB
/
tests.py
File metadata and controls
109 lines (83 loc) · 2.91 KB
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
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)