-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_local_app.py
148 lines (127 loc) · 5.26 KB
/
test_local_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
import datetime
import json
import os
import unittest
from flask import (
abort,
jsonify,
request
)
from flask_sqlalchemy import SQLAlchemy
from app import create_app
from models import setup_db, Actor, Movie
EXECUTIVE_PRODUCER_TOKEN = os.environ.get('EXECUTIVE_PRODUCER_TOKEN')
database_path = os.environ.get('DATABASE_URL',
"postgres://localhost:5432/casting_agency")
class CastingAgencyTestCase(unittest.TestCase):
"""This class represents the Casting Agency test case"""
def setUp(self):
"""Define test variables and initialize app."""
self.app = create_app()
self.client = self.app.test_client
self.database_path = database_path
setup_db(self.app, self.database_path)
# binds the app to the current context
with self.app.app_context():
self.db = SQLAlchemy()
self.db.init_app(self.app)
# create all tables
self.db.create_all()
self.new_actor = {
'name': 'George Clooney',
'age': 59,
'gender': "Male"
}
self.new_movie = {
"title": "Ocean's Eleven",
"release_date": "2001-12-07"
}
# Executive Producer Token, used for testing
self.ex_producer_token = {
'authorization': "Bearer %s" % EXECUTIVE_PRODUCER_TOKEN
}
def tearDown(self):
"""Executed after reach test"""
all_actors = Actor.query.all()
for actor in all_actors:
actor.delete()
all_movies = Movie.query.all()
for movie in all_movies:
movie.delete()
pass
# ------------------------------------------------------------
# Testing '/actors' and '/movies' GET endpoint
# ------------------------------------------------------------
def test_basic_get_succeeds(self):
res = self.client().get('/actors')
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue('actors' in data)
res = self.client().get('/movies')
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue('movies' in data)
# ------------------------------------------------------------
# Testing '/actors' POST and DELETE endpoint
# ------------------------------------------------------------
def test_basic_create_and_delete_actor_succeeds(self):
res = self.client().post('/actors',
headers=self.ex_producer_token,
json={
'name': self.new_actor['name'],
'age': self.new_actor['age'],
'gender': self.new_actor['gender']
})
data = json.loads(res.data)
new_actor_id = data['actors'][0]['id']
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['actors'][0]['name'], self.new_actor['name'])
self.assertEqual(data['actors'][0]['age'], self.new_actor['age'])
self.assertEqual(data['actors'][0]['gender'], self.new_actor['gender'])
# Assert the new actor was inserted
res = self.client().get('/actors')
data = json.loads(res.data)
actor_exists = False
for actor in data['actors']:
if actor['id'] == new_actor_id:
actor_exists = True
self.assertTrue(actor_exists)
# Clean up DB after creating new actor
res = self.client().delete('/actors/%d' % new_actor_id,
headers=self.ex_producer_token)
self.assertEqual(res.status_code, 200)
# ------------------------------------------------------------
# Testing '/movies' POST endpoint
# ------------------------------------------------------------
def test_basic_create_and_delete_movie_succeeds(self):
res = self.client().post(
'/movies',
headers=self.ex_producer_token,
json={
'title': self.new_movie['title'],
'release_date': self.new_movie['release_date']})
data = json.loads(res.data)
new_movie_id = data['movies'][0]['id']
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['movies'][0]['title'], self.new_movie['title'])
self.assertEqual(data['movies'][0]['release_date'],
self.new_movie['release_date'])
# Assert the new movie was inserted
res = self.client().get('/movies')
data = json.loads(res.data)
movie_exists = False
for movie in data['movies']:
if movie['id'] == new_movie_id:
movie_exists = True
self.assertTrue(movie_exists)
# Clean up DB after creating new movie
res = self.client().delete('/movies/%d' % new_movie_id,
headers=self.ex_producer_token)
self.assertEqual(res.status_code, 200)
# Make the tests conveniently executable
if __name__ == "__main__":
unittest.main()