-
Notifications
You must be signed in to change notification settings - Fork 616
/
test_sayhello.py
101 lines (81 loc) · 3.17 KB
/
test_sayhello.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
# -*- coding: utf-8 -*-
"""
:author: Grey Li (李辉)
:url: http://greyli.com
:copyright: © 2018 Grey Li <[email protected]>
:license: MIT, see LICENSE for more details.
"""
import unittest
from flask import abort
from sayhello import app, db
from sayhello.models import Message
from sayhello.commands import forge, initdb
class SayHelloTestCase(unittest.TestCase):
def setUp(self):
app.config.update(
TESTING=True,
WTF_CSRF_ENABLED=False,
SQLALCHEMY_DATABASE_URI='sqlite:///:memory:'
)
db.create_all()
self.client = app.test_client()
self.runner = app.test_cli_runner()
def tearDown(self):
db.session.remove()
db.drop_all()
def test_app_exist(self):
self.assertFalse(app is None)
def test_app_is_testing(self):
self.assertTrue(app.config['TESTING'])
def test_404_page(self):
response = self.client.get('/nothing')
data = response.get_data(as_text=True)
self.assertIn('404 Error', data)
self.assertIn('Go Back', data)
self.assertEqual(response.status_code, 404)
def test_500_page(self):
# create route to abort the request with the 500 Error
@app.route('/500')
def internal_server_error_for_test():
abort(500)
response = self.client.get('/500')
data = response.get_data(as_text=True)
self.assertEqual(response.status_code, 500)
self.assertIn('500 Error', data)
self.assertIn('Go Back', data)
def test_index_page(self):
response = self.client.get('/')
data = response.get_data(as_text=True)
self.assertIn('Say Hello', data)
def test_create_message(self):
response = self.client.post('/', data=dict(
name='Peter',
body='Hello, world.'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Your message have been sent to the world!', data)
self.assertIn('Hello, world.', data)
def test_form_validation(self):
response = self.client.post('/', data=dict(
name=' ',
body='Hello, world.'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('This field is required.', data)
def test_forge_command(self):
result = self.runner.invoke(forge)
self.assertIn('Created 20 fake messages.', result.output)
self.assertEqual(Message.query.count(), 20)
def test_forge_command_with_count(self):
result = self.runner.invoke(forge, ['--count', '50'])
self.assertIn('Created 50 fake messages.', result.output)
self.assertEqual(Message.query.count(), 50)
def test_initdb_command(self):
result = self.runner.invoke(initdb)
self.assertIn('Initialized database.', result.output)
def test_initdb_command_with_drop(self):
result = self.runner.invoke(initdb, ['--drop'], input='y\n')
self.assertIn('This operation will delete the database, do you want to continue?', result.output)
self.assertIn('Drop tables.', result.output)
if __name__ == '__main__':
unittest.main()