-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_app.py
143 lines (106 loc) · 6.91 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
import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from models import Actor, Movie, setup_db, db_drop_and_create_all
from app import create_app
from models import db
import datetime
casting_director_auth_header = {
'Authorization': "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6InJEbG5pelUyVVA2RjJiajdsWXI2QSJ9.eyJpc3MiOiJodHRwczovL2Rldi0xYjFrMWl4ei51cy5hdXRoMC5jb20vIiwic3ViIjoiZ29vZ2xlLW9hdXRoMnwxMTI2MTc2NjM0MjUxODI4NDk3NTMiLCJhdWQiOlsiY2FzdGluZy1jYXBzdG9uZSIsImh0dHBzOi8vZGV2LTFiMWsxaXh6LnVzLmF1dGgwLmNvbS91c2VyaW5mbyJdLCJpYXQiOjE1OTc4ODExNTgsImV4cCI6MTU5Nzk2NzU1OCwiYXpwIjoiV1VkaTlsOTJHT0thQ1V6cVJhMm00M1hNNVBBWlR3WXciLCJzY29wZSI6Im9wZW5pZCBwcm9maWxlIGVtYWlsIiwicGVybWlzc2lvbnMiOlsiY3JlYXRlOmFjdG9ycyIsImNyZWF0ZTptb3ZpZXMiLCJkZWxldGU6YWN0b3JzIiwiZGVsZXRlOm1vdmllcyIsImVkaXQ6YWN0b3JzIiwiZWRpdDptb3ZpZXMiLCJ2aWV3OmFjdG9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28GPhe3iWR5xVE2XnxmyRr5gFz1AI-8DS4hvYJcbpYI7phViM1-t64GO22M03XeCAVnddVsupsftE2vJfDcMuCAwGiBqBzeh12Abb1bCnTbO-TbqczX9Ydbr-y8_lz7W96f3uj8LnCJA8BCADu5TFiFp3LHxk5OrZjuRcPQaDcTXUO5vU3L7jhfMl5kwkJFlB1MIAvmvHZFg15V2er8OTqayyzSM9R5FxJ2uk5kpMzdyfUzH9-gtaDZ_MUSRyqRaycmnVX4-0GxsZilnYJVQ4g"
}
class CastingTestCase(unittest.TestCase):
def setUp(self):
'''define test variables and initialize app'''
self.app = create_app()
self.client = self.app.test_client
setup_db(self.app)
db.create_all()
db_drop_and_create_all()
self.new_movie = {
'title': 'New Movie',
'release_date' : datetime.date(2019, 3, 13),
}
self.new_actor = {
'name': 'John Doe',
'age': 22,
'gender': 'Male',
'movie_id': Movie.query.filter(Movie.title == "Matthew first Movie").one_or_none().id
}
with self.app.app_context():
self.db = SQLAlchemy()
self.db.init_app(self.app)
# create all tables
self.db.create_all()
def tearDown(self):
pass
def test_get_healthy(self):
res = self.client().get('/', headers = executive_producer_auth_header)
self.assertEqual(res.status_code, 200)
def test_get_movies(self):
res = self.client().get('/movies', headers = executive_producer_auth_header)
self.assertEqual(res.status_code, 200)
def test_get_movies_fail(self):
res = self.client().get('/moviess', headers = executive_producer_auth_header)
self.assertEqual(res.status_code, 404)
def test_get_actors(self):
res = self.client().get('/actors', headers = executive_producer_auth_header)
self.assertEqual(res.status_code, 200)
def test_get_actors_fail(self):
res = self.client().get('/actorss', headers = executive_producer_auth_header)
self.assertEqual(res.status_code, 404)
def test_create_movie(self):
res = self.client().post('/movies/create', json=self.new_movie, headers = executive_producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['new_movie']['title'], 'New Movie')
def test_create_actor(self):
Movie.query.filter(Movie.title == "Matthew first Movie").one_or_none().id
res = self.client().post('/actors/create', json=self.new_actor, headers = executive_producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['new_actor']['name'], 'John Doe')
def test_delete_movie(self):
movie_id = Movie.query.filter(Movie.title == "Random Movie").one_or_none().id
res = self.client().delete(f'/movies/delete/{movie_id}', headers = executive_producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
def test_delete_movie_fail(self):
res = self.client().delete('/movies/delete/1000' , headers = executive_producer_auth_header)
self.assertEqual(res.status_code, 404)
def test_delete_actor(self):
actor_id = Actor.query.filter(Actor.name == "Matthew").one_or_none().id
res = self.client().delete(f'/actors/delete/{actor_id}', headers = executive_producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
def test_delete_actor_fail(self):
res = self.client().delete('/actors/delete/1000', headers = executive_producer_auth_header)
self.assertEqual(res.status_code, 404)
def test_patch_movie(self):
movie_id = Movie.query.filter(Movie.title == "Random Movie").one_or_none().id
res = self.client().patch(f'/movies/patch/{movie_id}', json=self.new_movie, headers = executive_producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
def test_patch_movie_fail(self):
res = self.client().patch('/movies/patch/2000', json=self.new_movie, headers = executive_producer_auth_header)
self.assertEqual(res.status_code, 404)
def test_patch_actor(self):
actor_id = Actor.query.filter(Actor.name == "Matthew").one_or_none().id
res = self.client().patch(f'/actors/patch/{actor_id}', json=self.new_actor, headers = executive_producer_auth_header)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
def test_patch_actor_fail(self):
res = self.client().patch('/actors/patch/2000', json=self.new_actor, headers = executive_producer_auth_header)
self.assertEqual(res.status_code, 404)
if __name__ == "__main__":
unittest.main()