def test_detail_post(self): post = Post() post.title = 'this is a test' post.text = 'test ' * 20 db.session.add(post) db.session.commit() response = self.client.post('/post/1/detail') self.assertEqual(response.status_code, 302) user = User() user.username = '******' user.password = '******' db.session.add(user) db.session.commit() self.client.post('/login', data={ 'username': '******', 'password': '******' }) response = self.client.post('/post/1/detail', data={'comment': ''}, follow_redirects=True) self.assertEqual(response.status_code, 403) self.assertTrue('评论不能为空' == response.get_data(as_text=True)) response = self.client.post('/post/1/detail', data={'comment': 'comment for test'}, follow_redirects=True) self.assertEqual(response.status_code, 200) self.assertTrue('comment for test' in response.get_data(as_text=True))
def test_detail_get(self): post = Post() post.title = 'this is a test' post.text = 'test ' * 20 db.session.add(post) db.session.commit() response = self.client.get('/post/1/detail') self.assertEqual(response.status_code, 200) self.assertTrue('this is a test' in response.get_data(as_text=True)) self.assertTrue('test ' * 20 in response.get_data(as_text=True))
def test_int_func_with_posts(self): post = Post() post.title = 'this is a test' post.text = 'test test' * 20 db.session.add(post) db.session.commit() response = self.client.get('/') self.assertTrue(response.status_code, 200) self.assertTrue('this is a test' in response.get_data(as_text=True)) response = self.client.get('/posts') self.assertTrue(response.status_code, 200) self.assertTrue('this is a test' in response.get_data(as_text=True))
def test_show_tag(self): tag = Tag() tag.name = 'test' db.session.add(tag) db.session.commit() post = Post() post.title = 'this is a test' post.text = 'test ' * 20 post.tags.append(tag) db.session.add(post) db.session.commit() response = self.client.get('/tag/test/1') self.assertTrue('test' in response.get_data(as_text=True)) self.assertTrue('this is a test' in response.get_data(as_text=True))
def test_show_category(self): category = Category() category.name = 'test' db.session.add(category) db.session.commit() post = Post() post.title = 'this is a test' post.text = 'test ' * 20 post.category = category db.session.add(post) db.session.commit() response = self.client.get('/category/test/1') self.assertTrue('test' in response.get_data(as_text=True)) self.assertTrue('this is a test' in response.get_data(as_text=True))
def test_my_posts_with_login(self): user = User() user.username = '******' user.password = '******' user.email = '*****@*****.**' db.session.add(user) db.session.commit() post = Post() post.title = 'this is a test' post.text = 'test test' * 20 post.users = user db.session.add(post) db.session.commit() self.client.post('/login', data={ 'username': '******', 'password': '******' }, follow_redirects=True) response = self.client.get('/my_posts') self.assertEqual(response.status_code, 200) self.assertTrue('this is a test' in response.get_data(as_text=True))