]>
git.datanom.net - flask-test.git/blob - tests.py
4 from coverage
import coverage
5 cov
= coverage(branch
=True, omit
=['flask/*', 'tests.py'])
10 from datetime
import datetime
, timedelta
12 from config
import basedir
13 from app
import app
, db
14 from app
.models
import User
, Post
16 class TestCase(unittest
.TestCase
):
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')
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'
35 u
= User(nickname
='john', email
='john@example.com')
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())
43 def test_avatar(self
):
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
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')
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')
64 nickname2
= User
.make_unique_nickname('john')
65 assert nickname2
!= 'john'
66 assert nickname2
!= nickname
68 def test_follow(self
):
69 u1
= User(nickname
='john', email
='john@example.com')
70 u2
= User(nickname
='susan', email
='susan@example.com')
74 assert u1
.unfollow(u2
) is None
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'
88 assert not u1
.is_following(u2
)
89 assert u1
.followed
.count() == 0
90 assert u2
.followers
.count() == 0
92 def test_follow_posts(self
):
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')
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))
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
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()
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
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())
156 # query the post and destroy the session
157 p
= Post
.query
.get(1)
159 # delete the post using a new session
160 db
.session
= db
.create_scoped_session()
164 if __name__
== '__main__':
171 print ("\n\nCoverage Report:\n")
173 s
= "\nHTML version: " + os
.path
.join(basedir
, "tmp/coverage/index.html")
175 cov
.html_report(directory
='tmp/coverage')
This page took 0.074459 seconds and 6 git commands to generate.