-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pokemon_views.py
133 lines (83 loc) · 3.91 KB
/
test_pokemon_views.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
"""Pokemon View tests."""
# run these tests like:
#
# FLASK_ENV=production python -m unittest test_pokemon_views.py
import os
from unittest import TestCase
from models import db, connect_db, User, Favorite, Pokemon
# BEFORE we import our app, set an environmental variable
# to use a different database for tests (we need to do this
# before we import our app, since that will have already
# connected to the database
os.environ['DATABASE_URL'] = "postgresql:///pokedex-test"
# Now we can import app
from app import app, CURR_USER_KEY
# Create tables (we do this here, so we only create the tables
# once for all tests --- in each test, we'll delete the data
# and create fresh new clean test data
db.create_all()
# Don't have WTForms use CSRF at all, since it's a pain to test
app.config['WTF_CSRF_ENABLED'] = False
class PokemonViewTestCase(TestCase):
"""Test views for pokemon."""
def setUp(self):
"""Create test client, add sample data."""
db.drop_all()
db.create_all()
self.client = app.test_client()
self.testuser = User.signup(email="[email protected]",
username="testuser",
password="testuser")
self.testuser.id = 8888
db.session.commit()
#######################################################################
# SHOW POKEMON SEARCH RESULTS
def test_show_valid_search_results(self):
"""Show valid pokemon results from a user's search input"""
with self.client as client:
res = client.get('/pokemon?search=pikachu',
follow_redirects=True)
self.assertEqual(res.status_code, 200)
# should show heading indicating number of results for that search
self.assertIn("pikachu ", str(res.data))
def test_show_invalid_pokemon_result(self):
"""Try to route to a pokemon that doesn't exist in database"""
with self.client as client:
res = client.get('/pokemon/99999',
follow_redirects = True)
self.assertEqual(res.status_code, 200)
self.assertIn("Page Not Found", str(res.data))
# #######################################################################
# # SHOW AN INDIVIDUAL POKEMON'S DETAILS
def test_pokemon_show(self):
"""Show the details for a valid individual pokemon"""
with self.client as client:
res = client.get(f'/pokemon/pikachu')
self.assertEqual(res.status_code, 200)
self.assertIn("lightning-rod", str(res.data))
def test_invalid_pokemon_show(self):
"""Does pokemon exist?"""
with self.client as client:
with client.session_transaction() as sess:
sess[CURR_USER_KEY] = self.testuser.id
resp = client.get('/pokemon/99999',
follow_redirects = True)
self.assertEqual(resp.status_code, 200)
self.assertIn("Invalid path", str(resp.data))
# #######################################################################
# Test Invalid Routes (404, 405 errors)
def test_invalid_route_404(self):
"""Try to route to a page that doesn't exist in the app"""
with self.client as client:
res = client.get('/neopets',
follow_redirects = True)
self.assertEqual(res.status_code, 404)
self.assertIn("404: Page Not Found", str(res.data))
def test_invalid_route_405(self):
"""Try to route a method which is not allowed"""
with self.client as client:
res = client.get('/pokemon/91730/fav',
follow_redirects = True)
self.assertEqual(res.status_code, 405)
# Method not allowed since pokemon (id 91730) does not exist in the database
self.assertIn("405: Method Not Allowed", str(res.data))