44from flask import url_for
55import pytest
66
7- from firefly .models .user import User
8- from firefly .models .topic import Category , Post
7+ from firefly .models .topic import Category , Post , Comment
98
109
1110@pytest .mark .usefixtures ('client_class' )
1211class TestPost :
1312
1413 def setup (self ):
1514 c = Category .objects .create (
16- name = u 'python' , description = u '描述' , _slug = u 'python-slug'
15+ name = 'python' , description = '描述' , _slug = 'python-slug'
1716 )
1817 Post .objects .create (
19- title = u'标题test' , content = u'内容test' , category = c
20- )
21-
22- # login user
23- self .username = 'foo'
24- self .password = 'foobar'
25- self .email = 'foo@bar.com'
26- self .user = User .create_user (
27- username = self .username , password = self .password ,
28- email = self .email
18+ title = '标题test' , content = '内容test' , category = c
2919 )
20+ self ._login ()
3021
3122 def test_create (self ):
23+
3224 category = Category .objects .first ()
3325 url = url_for ('home.create' )
3426 form = {
3527 'title' : '标题' ,
3628 'content' : '内容喜喜喜喜喜喜' ,
3729 'category' : category .id ,
38- 'author' : self .user .id
3930 }
4031 rv = self .client .post (url , data = form )
41- assert rv .json ['ok' ] == 0
4232
33+ assert rv .json ['ok' ] == 0
4334 assert Post .objects .count () > 1
4435
4536 def test_detail (self ):
@@ -50,14 +41,15 @@ def test_detail(self):
5041 assert post .title in data
5142 assert post .content in data
5243
53- # def test_comment(self):
54- # post = Post.objects.first()
55- # url = url_for('post.detail', id=post.id)
56- # form = {
57- # 'content': u'评论测试',
58- # }
59- # rv = self.client.post(url, data=form, follow_redirects=False)
60-
61- # assert rv.status_code == 302
62- # assert Comment.objects.count() == 1
63- # assert len(post.comments) == 1
44+ def test_comment (self ):
45+ post = Post .objects .first ()
46+ url = url_for ('post.detail' , id = post .id )
47+ form = {
48+ 'content' : '评论测试' ,
49+ 'ref_id' : 0 ,
50+ }
51+ self .client .post (url , data = form , follow_redirects = False )
52+ post .reload ()
53+
54+ assert Comment .objects .count () == 1
55+ assert len (post .comments ) == 1
0 commit comments