-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_app.py
239 lines (185 loc) · 8.95 KB
/
test_app.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
from app import create_app
from models import setup_db, Actor, Movie
actors = [
{"name": "Chris", "age": 30, "gender": "male"},
{"name": "Lindsey", "age": 25, "gender": "female"},
{"name": "Leslie", "age": 27, "gender": "male"},
{"name": "Winston", "age": 30, "gender": "male"},
{"name": "Jessica", "age": 26, "gender": "female"}
]
movies = [
{"title": "Black Widow", "release_date": "07-10-2021"},
{"title": "The Boss Baby", "release_date": "03-31-2017"},
{"title": "Jurassic Park", "release_date": "06-11-1993"},
{"title": "Wrath of Man", "release_date": "05-07-2021"},
]
CASTING_ASSISTANT = os.environ['CASTING_ASSISTANT']
CASTING_DIRECTOR = os.environ['CASTING_DIRECTOR']
EXECUTIVE_PRODUCER = os.environ['EXECUTIVE_PRODUCER']
def get_token_header(token):
return {"Authorization": "Bearer {0}".format(token)}
class CapstoneTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app()
self.client = self.app.test_client
self.database_path = os.environ['DATABASE_URL_TEST'].replace('postgres://', 'postgresql://')
database = setup_db(self.app, self.database_path)
database.create_all()
self.new_actor = {
'name': "Karen",
'age': 22,
'gender': "female"
}
self.new_movie = {
'title': 'Tome & Jerry',
'release_date': '02-26-2021'
}
with self.app.app_context():
self.db = SQLAlchemy()
self.db.init_app(self.app)
self.db.session.execute('ALTER SEQUENCE "movie_id_seq" RESTART WITH 1')
self.db.session.execute('ALTER SEQUENCE "actor_id_seq" RESTART WITH 1')
self.db.create_all()
for movie in movies:
obj = Movie(title=movie['title'], release_date=movie['release_date'])
self.db.session.add(obj)
self.db.session.commit()
for actor in actors:
obj = Actor(name=actor['name'], age=actor['age'], gender=actor['gender'])
self.db.session.add(obj)
self.db.session.commit()
self.db.session.close()
def tearDown(self) -> None:
with self.app.app_context():
self.db.session.query(Actor).delete()
self.db.session.query(Movie).delete()
self.db.session.commit()
self.db.session.close()
self.db.drop_all()
"""
Write Tests
"""
def test_get_actors(self):
res = self.client().get('/actors', headers=get_token_header(CASTING_ASSISTANT))
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['total_actors'], 5)
self.assertTrue(data['actors'])
def test_404_invalid_page_actors(self):
res = self.client().get('/actors?page=2', headers=get_token_header(CASTING_ASSISTANT))
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['message'], "resource not found")
self.assertFalse(data['success'])
def test_405_making_post_request_on_get_actors(self):
res = self.client().post('/actors', headers=get_token_header(CASTING_DIRECTOR))
data = json.loads(res.data)
self.assertEqual(res.status_code, 405)
self.assertEqual(data['message'], 'method not allowed')
self.assertFalse(data['success'])
def test_get_movies(self):
res = self.client().get('/movies', headers=get_token_header(CASTING_DIRECTOR))
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['total_movies'], 4)
self.assertTrue(data['movies'])
def test_404_invalid_page_movies(self):
res = self.client().get('/movies?page=2', headers=get_token_header(CASTING_DIRECTOR))
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['message'], "resource not found")
self.assertFalse(data['success'])
def test_405_making_delete_request_on_get_movies(self):
res = self.client().delete('/movies', headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 405)
self.assertEqual(data['message'], 'method not allowed')
self.assertFalse(data['success'])
def test_delete_actors(self):
res = self.client().delete('/actors/2/delete', headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['deleted'], 2)
self.assertTrue(data['success'])
def test_404_delete_none_existing_actor(self):
res = self.client().delete('/actors/10/delete', headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['message'], "resource not found")
self.assertFalse(data['success'])
def test_delete_movies(self):
res = self.client().delete('/movies/2/delete', headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['deleted'], 2)
self.assertTrue(data['success'])
def test_405_making_get_request_for_delete(self):
res = self.client().get('/movies/3/delete', headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 405)
self.assertEqual(data['message'], "method not allowed")
self.assertFalse(data['success'])
def test_create_actor(self):
res = self.client().post('/actors/create', json=self.new_actor, headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['actor'], 6)
self.assertTrue(data['success'])
def test_422_invalid_json_for_actor(self):
res = self.client().post('/actors/create', json={"title": "Romeo"}, headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 422)
self.assertEqual(data['message'], "unprocessable")
self.assertFalse(data['success'])
def test_create_movie(self):
res = self.client().post('/movies/create', json=self.new_movie , headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['movie'], 5)
self.assertTrue(data['success'])
def test_422_invalid_json_for_movie(self):
res = self.client().post('/movies/create', json={"titles": "titles"}, headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 422)
self.assertEqual(data['message'], 'unprocessable')
self.assertFalse(data['success'])
def test_edit_actor(self):
res = self.client().patch('/actors/1/edit', json={'name':"Santa", "age":11}, headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(len(data['actor']), 4)
self.assertEqual(data['actor']['id'], 1)
self.assertEqual(data['actor']['name'], "Santa")
self.assertEqual(data['actor']['age'], 11)
self.assertEqual(data['actor']['gender'], "male")
self.assertTrue(data['actor'])
self.assertTrue(data['success'])
def test_404_edit_invalid_actor(self):
res = self.client().patch('/actors/11/edit', json={'name':"Santa", "age":11}, headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['message'], 'resource not found')
self.assertFalse(data['success'])
def test_edit_movie(self):
res = self.client().patch('/movies/4/edit', json={'title':"Jurassic Park 2"}, headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(len(data['movie']), 3)
self.assertEqual(data['movie']['id'], 4)
self.assertEqual(data['movie']['title'], "Jurassic Park 2")
self.assertEqual(data['movie']['release_date'], "Fri, 07 May 2021 00:00:00 GMT")
self.assertTrue(data['success'])
def test_404_edit_invalid_movie(self):
res = self.client().patch('/movies/40/edit', json={'title':"Jurassic Park 2"}, headers=get_token_header(EXECUTIVE_PRODUCER))
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['message'], "resource not found")
self.assertFalse(data['success'])
if __name__ == '__main__':
unittest.main()