This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_app.py
199 lines (157 loc) · 7.22 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
import random
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
from werkzeug.wrappers import request
from app import create_app
from models import setup_db, Question, Category
from settings import DB_NAME, DB_HOST, DB_USER, DB_PASSWORD
class TriviaTestCase(unittest.TestCase):
"""This class represents the trivia test case"""
def setUp(self):
"""Define test variables and initialize app."""
self.app = create_app()
self.client = self.app.test_client
self.database_path = 'postgresql://{}:{}@{}/{}_test'.format(DB_USER, DB_PASSWORD, DB_HOST, DB_NAME)
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_question = {
'question': 'What is this?',
'answer': 'test',
'category': 1,
'difficulty': 5,
'id': 99
}
def tearDown(self):
"""Executed after reach test"""
question = Question.query.get(self.new_question.get('id'))
if question:
question.delete()
# # Quiz questions Test
def test_quiz_questions_with_category(self):
categories = Category.query.all()
rand = random.randrange(0, len(categories))
category = categories[rand]
request_data = {'previos_questions': [], 'quiz_category': {'id': category.id, 'type': category.type}}
res = self.client().post(f'/quizzes', json=request_data)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['question'].get('category'), category.id)
def test_quiz_questions_without_category(self):
request_data = {'previos_questions': [], 'quiz_category': {'id': 0, 'type': 'click'}}
res = self.client().post(f'/quizzes', json=request_data)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['question'])
def test_question_with_wrong_data(self):
request_data = {'previos_questions': [], 'quiz_category': {'id': 'abc', 'type': 'click'}}
res = self.client().post(f'/quizzes', json=request_data)
data = json.loads(res.data)
self.assertEqual(res.status_code, 422)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'unprocessable')
# Questions for category Test
def test_questions_for_category(self):
categories = Category.query.all()
rand = random.randrange(0, len(categories))
category = categories[rand]
res = self.client().get(f'/categories/{category.id}/questions')
data = json.loads(res.data)
questions_count = Question.query.filter(Question.category == category.id).count()
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(len(data['questions']), questions_count)
self.assertEqual(data['current_category'], category.type)
self.assertTrue(data['total_questions'])
def test_404_sent_requesting_beyond_valid_page_for_categories(self):
res = self.client().get('/categories/1000/questions')
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'resource not found')
# Question List Test
def test_get_paginated_questions(self):
res = self.client().get('/questions')
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['total_questions'])
self.assertTrue(len(data['questions']))
self.assertTrue(isinstance(data['categories'], dict))
def test_404_sent_requesting_beyond_valid_page(self):
res = self.client().get('/questions?page=1000', json={'rating': 1})
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'resource not found')
# All categories List Test
def test_get_all_categories(self):
res = self.client().get('/categories')
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['categories'])
self.assertTrue(isinstance(data['categories'], dict))
# Delete question
def test_delete_question(self):
question = Question(**self.new_question)
question.insert()
question_id = question.id
res = self.client().delete(f'/questions/{question_id}')
data = json.loads(res.data)
question = Question.query.filter(Question.id == question_id).one_or_none()
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['deleted'], question_id)
self.assertTrue(data['total_questions'])
self.assertTrue(len(data['questions']))
self.assertEqual(question, None)
def test_422_if_question_does_not_exist(self):
res = self.client().delete('/questions/1000')
data = json.loads(res.data)
self.assertEqual(res.status_code, 422)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'unprocessable')
# Create question
def test_create_new_question(self):
res = self.client().post('/questions', json=self.new_question)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['created'])
self.assertTrue(len(data['questions']))
def test_405_if_question_creation_not_allowed(self):
res = self.client().post('/questions/45', json=self.new_question)
data = json.loads(res.data)
self.assertEqual(res.status_code, 405)
self.assertEqual(data['success'], False)
self.assertEqual(data['message'], 'method not allowed')
# Search question
def test_get_question_search_with_results(self):
res = self.client().post('/questions/search', json={'search': 'title'})
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertTrue(data['total_questions'])
self.assertEqual(len(data['questions']), 2)
def test_get_question_search_without_results(self):
res = self.client().post('/questions/search', json={'search': 'anonymous'})
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertEqual(data['success'], True)
self.assertEqual(data['total_questions'], 0)
self.assertEqual(len(data['questions']), 0)
"""
TODO
Write at least one test for each test for successful operation and for expected errors.
"""
# Make the tests conveniently executable
if __name__ == "__main__":
unittest.main()