-
Notifications
You must be signed in to change notification settings - Fork 0
/
api
167 lines (120 loc) · 5.67 KB
/
api
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
import unittest
import os
import json
try: from urllib.parse import urlparse
except ImportError: from urlparse import urlparse # Python 2 compatibility
# Configure our app to use the testing databse
os.environ["CONFIG_PATH"] = "posts.config.TestingConfig"
from posts import app
from posts import models
from posts.database import Base, engine, session
class TestAPI(unittest.TestCase):
""" Tests for the posts API """
def setUp(self):
""" Test setup """
self.client = app.test_client()
# Set up the tables in the database
Base.metadata.create_all(engine)
def tearDown(self):
""" Test teardown """
session.close()
# Remove the tables and their data from the database
Base.metadata.drop_all(engine)
def test_get_empty_posts(self):
""" Getting posts from an empty database """
response = self.client.get("/api/posts",
headers=[("Accept", "application/json")]
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.mimetype, "application/json")
data = json.loads(response.data.decode("ascii"))
self.assertEqual(data, [])
response = self.client.get("/api/posts",
headers=[("Accept", "application/json")]
)
# def test_get_posts(self):
# # Getting posts from a populated database
# postA = models.Post(title="Example Post A", body="Just a test")
# postB = models.Post(title="Example Post B", body="Still a test")
# session.add_all([postA, postB])
# session.commit()
# response = self.client.get("/api/posts")
# self.assertEqual(response.status_code, 200)
# self.assertEqual(response.mimetype, "application/json")
# data = json.loads(response.data.decode("ascii"))
# self.assertEqual(len(data), 2)
# postA = data[0]
# self.assertEqual(postA["title"], "Example Post A")
# self.assertEqual(postA["body"], "Just a test")
# postB = data[1]
# self.assertEqual(postB["title"], "Example Post B")
# self.assertEqual(postB["body"], "Still a test")
def test_get_post(self):
""" Getting a single post from a populated database """
postA = models.Post(title="Example Post A", body="Just a test")
postB = models.Post(title="Example Post B", body="Still a test")
session.add_all([postA, postB])
session.commit()
response = self.client.get("/api/posts/{}".format(postB.id))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.mimetype, "application/json")
post = json.loads(response.data.decode("ascii"))
self.assertEqual(post["title"], "Example Post B")
self.assertEqual(post["body"], "Still a test")
def test_get_non_existent_post(self):
""" Getting a single post which doesn't exist """
response = self.client.get("/api/posts/1")
self.assertEqual(response.status_code, 404)
self.assertEqual(response.mimetype, "application/json")
data = json.loads(response.data.decode("ascii"))
self.assertEqual(data["message"], "Could not find post with id 1")
def test_unsupported_accept_header(self):
response = self.client.get("/api/posts",
headers=[("Accept", "application/xml")]
)
self.assertEqual(response.status_code, 406)
self.assertEqual(response.mimetype, "application/json")
data = json.loads(response.data.decode("ascii"))
self.assertEqual(data["message"],
"Request must accept application/json data")
def test_delete_single_post(self):
postA = models.Post(title="Example Post A", body="Delete this")
postB = models.Post(title="Example Post B", body="Don't delete this")
session.add_all([postA, postB])
session.commit()
response = self.client.get(
"/api/posts/{}".format(postA.id),
headers=[("Accept", "application/json")])
session.delete(postA)
session.commit()
self.assertEqual(response.status_code, 200)
self.assertEqual(response.mimetype, "application/json")
# data = json.loads(response.data)
posts = session.query(models.Post).all()
self.assertEqual(len(posts), 1)
postB = posts[0]
self.assertEqual(postB.title, "Example Post B")
self.assertEqual(postB.body, "Don't delete this")
def test_get_posts_with_title(self):
""" Filtering posts by title and body """
postA = models.Post(title="Post with bells", body="Just a test")
postB = models.Post(title="Post with whistles", body="test and bells")
postC = models.Post(title="Post with bells and whistles",
body="Another and bells test")
session.add_all([postA, postB, postC])
session.commit()
response = self.client.get("/api/posts?title_like=whistles&body_like=bells",
headers=[("Accept", "application/json")]
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.mimetype, "application/json")
posts = json.loads(response.data.decode("ascii"))
self.assertEqual(len(posts), 2)
post = posts[0]
self.assertEqual(post["title"], "Post with whistles")
self.assertEqual(post["body"], "test and bells")
post = posts[1]
self.assertEqual(post["title"], "Post with bells and whistles")
self.assertEqual(post["body"], "Another and bells test")
if __name__ == "__main__":
unittest.main()