]> git.datanom.net - flask-test.git/blob - tests.py
final
[flask-test.git] / tests.py
1 #!flask/bin/python
2 # -*- coding: utf8 -*-
3
4 from coverage import coverage
5 cov = coverage(branch=True, omit=['flask/*', 'tests.py'])
6 cov.start()
7
8 import os
9 import unittest
10 from datetime import datetime, timedelta
11
12 from config import basedir
13 from app import app, db
14 from app.models import User, Post
15
16 class TestCase(unittest.TestCase):
17 def setUp(self):
18 app.config['TESTING'] = True
19 app.config['WTF_CSRF_ENABLED'] = False
20 app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
21 os.path.join(basedir, 'test.db')
22 db.create_all()
23
24 def tearDown(self):
25 db.session.remove()
26 db.drop_all()
27
28 def test_user(self):
29 # make valid nicknames
30 n = User.make_valid_nickname('John_123')
31 assert n == 'John_123'
32 n = User.make_valid_nickname('John_[123]\n')
33 assert n == 'John_123'
34 # create a user
35 u = User(nickname='john', email='john@example.com')
36 db.session.add(u)
37 db.session.commit()
38 assert u.is_authenticated is True
39 assert u.is_active is True
40 assert u.is_anonymous is False
41 assert u.id == int(u.get_id())
42
43 def test_avatar(self):
44 # create a user
45 u = User(nickname='john', email='john@example.com')
46 avatar = u.avatar(128)
47 expected = 'http://www.gravatar.com/avatar/' + \
48 'd4c74594d841139328695756648b6bd6'
49 assert avatar[0:len(expected)] == expected
50
51 def test_make_unique_nickname(self):
52 # create a user and write it to the database
53 u = User(nickname='john', email='john@example.com')
54 db.session.add(u)
55 db.session.commit()
56 nickname = User.make_unique_nickname('susan')
57 assert nickname == 'susan'
58 nickname = User.make_unique_nickname('john')
59 assert nickname != 'john'
60 # make another user with the new nickname
61 u = User(nickname=nickname, email='susan@example.com')
62 db.session.add(u)
63 db.session.commit()
64 nickname2 = User.make_unique_nickname('john')
65 assert nickname2 != 'john'
66 assert nickname2 != nickname
67
68 def test_follow(self):
69 u1 = User(nickname='john', email='john@example.com')
70 u2 = User(nickname='susan', email='susan@example.com')
71 db.session.add(u1)
72 db.session.add(u2)
73 db.session.commit()
74 assert u1.unfollow(u2) is None
75 u = u1.follow(u2)
76 db.session.add(u)
77 db.session.commit()
78 assert u1.follow(u2) is None
79 assert u1.is_following(u2)
80 assert u1.followed.count() == 1
81 assert u1.followed.first().nickname == 'susan'
82 assert u2.followers.count() == 1
83 assert u2.followers.first().nickname == 'john'
84 u = u1.unfollow(u2)
85 assert u is not None
86 db.session.add(u)
87 db.session.commit()
88 assert not u1.is_following(u2)
89 assert u1.followed.count() == 0
90 assert u2.followers.count() == 0
91
92 def test_follow_posts(self):
93 # make four users
94 u1 = User(nickname='john', email='john@example.com')
95 u2 = User(nickname='susan', email='susan@example.com')
96 u3 = User(nickname='mary', email='mary@example.com')
97 u4 = User(nickname='david', email='david@example.com')
98 db.session.add(u1)
99 db.session.add(u2)
100 db.session.add(u3)
101 db.session.add(u4)
102 # make four posts
103 utcnow = datetime.utcnow()
104 p1 = Post(body="post from john", author=u1,
105 timestamp=utcnow + timedelta(seconds=1))
106 p2 = Post(body="post from susan", author=u2,
107 timestamp=utcnow + timedelta(seconds=2))
108 p3 = Post(body="post from mary", author=u3,
109 timestamp=utcnow + timedelta(seconds=3))
110 p4 = Post(body="post from david", author=u4,
111 timestamp=utcnow + timedelta(seconds=4))
112 db.session.add(p1)
113 db.session.add(p2)
114 db.session.add(p3)
115 db.session.add(p4)
116 db.session.commit()
117 # setup the followers
118 u1.follow(u1) # john follows himself
119 u1.follow(u2) # john follows susan
120 u1.follow(u4) # john follows david
121 u2.follow(u2) # susan follows herself
122 u2.follow(u3) # susan follows mary
123 u3.follow(u3) # mary follows herself
124 u3.follow(u4) # mary follows david
125 u4.follow(u4) # david follows himself
126 db.session.add(u1)
127 db.session.add(u2)
128 db.session.add(u3)
129 db.session.add(u4)
130 db.session.commit()
131 # check the followed posts of each user
132 f1 = u1.followed_posts().all()
133 f2 = u2.followed_posts().all()
134 f3 = u3.followed_posts().all()
135 f4 = u4.followed_posts().all()
136 assert len(f1) == 3
137 assert len(f2) == 2
138 assert len(f3) == 2
139 assert len(f4) == 1
140 assert f1[0].id == p4.id
141 assert f1[1].id == p2.id
142 assert f1[2].id == p1.id
143 assert f2[0].id == p3.id
144 assert f2[1].id == p2.id
145 assert f3[0].id == p4.id
146 assert f3[1].id == p3.id
147 assert f4[0].id == p4.id
148
149 def test_delete_post(self):
150 # create a user and a post
151 u = User(nickname='john', email='john@example.com')
152 p = Post(body='test post', author=u, timestamp=datetime.utcnow())
153 db.session.add(u)
154 db.session.add(p)
155 db.session.commit()
156 # query the post and destroy the session
157 p = Post.query.get(1)
158 db.session.remove()
159 # delete the post using a new session
160 db.session = db.create_scoped_session()
161 db.session.delete(p)
162 db.session.commit()
163
164 if __name__ == '__main__':
165 try:
166 unittest.main()
167 except:
168 pass
169 cov.stop()
170 cov.save()
171 print ("\n\nCoverage Report:\n")
172 cov.report()
173 s = "\nHTML version: " + os.path.join(basedir, "tmp/coverage/index.html")
174 print(s)
175 cov.html_report(directory='tmp/coverage')
176 cov.erase()
This page took 0.074459 seconds and 6 git commands to generate.