forked from BSuperbad/Springboard_SQLAlchemy_CodeAlong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_flask.py
62 lines (43 loc) · 1.74 KB
/
test_flask.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
from unittest import TestCase
from app import app
from models import db, Pet
# Use test database and don't clutter tests with SQL
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///sqla_intro_test'
app.config['SQLALCHEMY_ECHO'] = False
# Make Flask errors be real errors, rather than HTML pages with error info
app.config['TESTING'] = True
# This is a bit of hack, but don't use Flask DebugToolbar
app.config['DEBUG_TB_HOSTS'] = ['dont-show-debug-toolbar']
db.drop_all()
db.create_all()
class PetViewsTestCase(TestCase):
"""Tests for views for Pets."""
def setUp(self):
"""Add sample pet."""
Pet.query.delete()
pet = Pet(name="TestPet", species="dog", hunger=10)
db.session.add(pet)
db.session.commit()
self.pet_id = pet.id
def tearDown(self):
"""Clean up any fouled transaction."""
db.session.rollback()
def test_list_pets(self):
with app.test_client() as client:
resp = client.get("/")
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('TestPet', html)
def test_show_pet(self):
with app.test_client() as client:
resp = client.get(f"/{self.pet_id}")
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn('<h1>TestPet</h1>', html)
def test_add_pet(self):
with app.test_client() as client:
d = {"name": "TestPet2", "species": "cat", "hunger": 20}
resp = client.post("/", data=d, follow_redirects=True)
html = resp.get_data(as_text=True)
self.assertEqual(resp.status_code, 200)
self.assertIn("<h1>TestPet2</h1>", html)