-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
131 lines (115 loc) · 4.72 KB
/
test.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
from flask_testing import TestCase
from flaskr import create_app, db
from flask import url_for
from flaskr.models import User
import os
import unittest
from flask_login import current_user
app = create_app()
class TestConfig:
TESTING = True
WTF_CSRF_ENABLED = False
basedir = os.path.abspath(os.path.dirname(__name__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'test.db')
class MyTest(TestCase):
def create_app(self):
# app.config['TESTING'] = True
# app.config['WTF_CSRF_ENABLED'] = False
# basedir = os.path.abspath(os.path.dirname(__name__))
# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db')
app.config.from_object('test.TestConfig') # 設定を読み込む
return app
def setUp(self): # 各テスト毎に呼ばれる
db.create_all() # テーブル全作成
def tearDown(self): # 各テスト毎に呼ばれる
db.session.remove() # セッション削除
db.drop_all() # テーブル全削除
def test_register(self):
with self.client as client:
self.assertEqual(User.query.count(), 0)
response = client.post(url_for('app.register'),
data = {
'username': 'test',
'email': 'test@mail.com',
'password': 'password',
'password_confirm': 'password',
}
)
self.assertEqual(User.query.count(), 1)
self.assertEqual(User.query.all()[-1:][0].username, 'test')
self.assert_status(response, 302) # ステータスコード(redirectの場合302)
self.assert_redirects(response, url_for('app.login')) # リダイレクト先の確認
def test_register_error(self):
with self.client as client:
self.assertEqual(User.query.count(), 0)
response = client.post(url_for('app.register'),
data = {
'username': 'test',
'email': 'test@mail.com',
'password': 'passwor',
'password_confirm': 'password',
}
)
self.assertEqual(User.query.count(), 0)
self.assert_status(response, 200)
def test_register_error2(self):
with self.client as client:
self.assertEqual(User.query.count(), 0)
response = client.post(url_for('app.register'),
data = {
'username': 'test',
'email': 'test@mail.com',
'password': 'password',
'password_confirm': 'password',
}
)
self.assertEqual(User.query.count(), 1)
response = client.post(url_for('app.register'),
data = {
'username': 'test',
'email': 'test@mail.com',
'password': 'password',
'password_confirm': 'password',
}
)
self.assertEqual(User.query.count(), 1)
def test_login(self):
with self.client as client:
response = client.post(url_for('app.register'),
data = {
'username': 'test',
'email': 'test@mail.com',
'password': 'password',
'password_confirm': 'password',
}
)
self.assertTrue(current_user.is_anonymous)
response = client.post(url_for('app.login'),
data = {
'email': 'test@mail.com',
'password': 'password',
}
)
self.assert_status(response, 302)
self.assert_redirects(response, url_for('app.welcome'))
self.assertEqual(current_user.username, 'test')
def test_logout(self):
with self.client as client:
response = client.post(url_for('app.register'),
data = {
'username': 'test',
'email': 'test@mail.com',
'password': 'password',
'password_confirm': 'password',
}
)
response = client.post(url_for('app.login'),
data = {
'email': 'test@mail.com',
'password': 'password',
}
)
response = client.get(url_for('app.logout'))
self.assertTrue(current_user.is_anonymous)
if __name__ == '__main__':
unittest.main()