-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main.py
129 lines (101 loc) · 4.34 KB
/
test_main.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
import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
import main
from models import setup_db, Question, Category
class TriviaTestCase(unittest.TestCase):
"""This class represents the trivia test case"""
def setUp(self):
"""Define test variables and initialize app."""
self.app = main.app
self.app.config['TESTING']=True
self.client = self.app.test_client
self.database_name = "trivia_test"
self.database_path = "postgres://{}/{}".format(
'postgres:fokou2014@localhost:5432', self.database_name)
setup_db(self.app, self.database_path)
self.new_question = {
'question': 'Here is a new question title',
'answer': 'Here is a new answer string',
'difficulty': 1,
'category': 3,
}
self.new_search={'searchTerm': 'title'}
self.quiz_param = {'previous_questions': [],'quiz_category': {'type': 'Entertainment', 'id': 5}}
# 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()
def tearDown(self):
"""Executed after reach test"""
pass
"""
TODO
Write at least one test for each test for successful operation and for expected errors.
"""
def test_get_categories(self):
res = self.client().get("/categories")
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertTrue(data["categories"])
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(data["current_category"])
self.assertTrue(len(data["questions"]))
self.assertTrue(len(data["categories"]))
def test_404_sent_requesting_questions_beyond_valid_page(self):
res = self.client().get("/questions?page=1000")
data = json.loads(res.data)
self.assertEqual(res.status_code, 404)
self.assertEqual(data["success"], False)
self.assertEqual(data["message"], "resource not found")
def test_delete_question(self):
res = self.client().delete("/questions/6")
data = json.loads(res.data)
question = Question.query.filter(Question.id == 6).one_or_none()
self.assertEqual(res.status_code, 200)
self.assertEqual(data["success"], True)
self.assertEqual(data["deleted"], 6)
self.assertEqual(question, None)
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"])
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")
def test_get_questions_search_with_result(self):
res = self.client().post('/questions', json=self.new_search)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertTrue(data['questions'])
self.assertTrue(data['total_questions'])
self.assertTrue(data['current_category'])
self.assertEqual(data['success'], True)
def test_get_questions_per_category(self):
res = self.client().get(f'/categories/{6}/questions')
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertTrue(data['questions'])
self.assertTrue(data['total_questions'])
self.assertTrue(data['current_category'])
def test_get_next_quiz(self):
res = self.client().post('/quizzes', json=self.quiz_param)
data = json.loads(res.data)
self.assertEqual(res.status_code, 200)
self.assertTrue(data['question'])
# Make the tests conveniently executable
if __name__ == "__main__":
unittest.main()