def test_unvote_with_upvote(self): # Upvote a post and then unvote it and verify that it has been unvoted user = create_test_user() community = create_test_community() post = create_test_post(community, user) # Now manually upvote the post and verify that it has been upvoted vote = PostVote(user_id=user.id, post_id=post.id, vote_type=True) user.post_votes.append(vote) post.votes.append(vote) post.karma += 1 db.session.add(vote) db.session.commit() vote = PostVote.query.filter(PostVote.user_id == user.id, PostVote.post_id == post.id).first() self.assertIsNotNone(vote) self.assertTrue(vote.vote_type) self.assertEqual(post.karma, 1) self.assertEqual(len(user.post_votes), 1) self.assertEqual(len(post.votes), 1) # Now unvote it and verify it no longer exists and the post's karma is reset to 0 post_functions.unvote(user.id, post.id) vote = PostVote.query.filter(PostVote.user_id == user.id, PostVote.post_id == post.id).first() self.assertIsNone(vote) self.assertEqual(post.karma, 0) self.assertEqual(len(user.post_votes), 0) self.assertEqual(len(post.votes), 0) # cleanup cleanup(Post, PostVote, Community, User)
def test_edit_post_empty_title_and_body(self): # Edit a post, but supply an empty title and body as parameters. user, community = create_test_user(), create_test_community() post = create_test_post(community, user) # Edit the post, but provide no title and then no body self.assertRaises(ValueError, post_functions.edit_post, post.id, None, "test") self.assertRaises(ValueError, post_functions.edit_post, post.id, "test", None) self.assertRaises(ValueError, post_functions.edit_post, post.id, "", "test") self.assertRaises(ValueError, post_functions.edit_post, post.id, "test", "") cleanup(Post, Community, User)
def test_edit_post(self): user, community = create_test_user(), create_test_community() post = create_test_post(community, user) # Now edit the post edit_title, edit_body = "Edited title", "Edited body" post_functions.edit_post(post.id, edit_title, edit_body) self.assertEqual(post.title, edit_title) self.assertEqual(post.body, edit_body) # cleanup cleanup(Post, Community, User)
def test_delete_post(self): # First, real user, real community, real post user = create_test_user() community = create_test_community() post = create_test_post(community, user) pid = post.id # Assert that the post exists self.assertIsNotNone(Post.query.filter(Post.id == pid).first()) # Delete and assert that the post doesn't exist post_functions.delete_post(pid) self.assertIsNone(Post.query.filter(Post.id == pid).first()) # Cleanup community and user cleanup(Community, User)
def test_create_comment(self): # Create a comment and verify it exists user = create_test_user() community = create_test_community() post = create_test_post(user, community) # Create it comment_functions.create_comment(user.id, post.id, "Testing comment!") # Verify that it exists comment = Comment.query.filter(Comment.id == 1).first() self.assertIsNotNone(comment) self.assertEqual(comment.post, post) self.assertEqual(comment.user, user) # Cleanup cleanup(User, Community, Post, Comment)
def test_upvote(self): user = create_test_user() community = create_test_community() post = create_test_post(community, user) # Upvote it and check the karma post_functions.upvote(user.id, post.id) # Should be 1 self.assertEqual(post.karma, 1) vote_obj = PostVote.query.filter(PostVote.user_id == user.id, PostVote.post_id == post.id).first() self.assertIsNotNone(vote_obj) self.assertTrue(vote_obj.vote_type) # True vote_type means upvote self.assertIn(vote_obj, user.post_votes) self.assertIn(vote_obj, post.votes) self.assertEqual(len(user.post_votes), 1) self.assertEqual(len(post.votes), 1) # Cleanup cleanup(Post, PostVote, Community, User)
def test_downvote(self): user = create_test_user() community = create_test_community() post = create_test_post(community, user) # Start at 0, should be downvoted to -1 post_functions.downvote(user.id, post.id) self.assertEqual(post.karma, -1) # Get the vote object vote_obj = PostVote.query.filter(PostVote.user_id == user.id, PostVote.post_id == post.id).first() self.assertIsNotNone(vote_obj) self.assertFalse(vote_obj.vote_type) # Assert that it's a downvote type self.assertIn(vote_obj, user.post_votes) self.assertIn(vote_obj, post.votes) self.assertEqual(len(user.post_votes), 1) self.assertEqual(len(post.votes), 1) # Cleanup cleanup(Post, PostVote, Community, User)
def test_create_comment_non_existent_user(self): # Create a comment with a non existent user # We still have to create one real user at least, # so that we can create the post user = create_test_user() community = create_test_community() post = create_test_post(user, community) # Should raise ValueError when the user is None self.assertRaises(ValueError, comment_functions.create_comment, None, post.id, "Test comment") self.assertRaises(ValueError, comment_functions.create_comment, -1, post.id, "Test") # Should raise InterfaceError when passed some weird object like [], {}, etc self.assertRaises(exc.InterfaceError, comment_functions.create_comment, [], post.id, "Test comment") # Cleanup cleanup(User, Community, Post, Comment)
def test_delete_comment(self): user = create_test_user() community = create_test_community() post = create_test_post(user, community) # Manually create the comment comment = Comment(user_id=user.id, post_id=post.id, text="F*****g comment lol shit") db.session.add(comment) db.session.commit() cid = comment.id # Now verify first that it exists, and then delete it and verify that it doesn't comment = Comment.query.filter(Comment.id == cid).first() self.assertIsNotNone(comment) # Delete comment_functions.delete_comment(cid) # verify it's None comment = Comment.query.filter(Comment.id == cid).first() self.assertIsNone(comment) # Cleanup cleanup(Post, Community, User)
def test_unvote_with_downvote(self): # Downvote a post and then unvote it user = create_test_user() community = create_test_community() post = create_test_post(community, user) vote = PostVote(user_id=user.id, post_id=post.id, vote_type=False) # False means -1 user.post_votes.append(vote) post.votes.append(vote) post.karma -= 1 db.session.commit() # Now verify vote = PostVote.query.filter(PostVote.user_id == user.id, PostVote.post_id == post.id).first() self.assertIsNotNone(vote) self.assertEqual(post.karma, -1) # Unvote it post_functions.unvote(user.id, post.id) vote = PostVote.query.filter(PostVote.user_id == user.id, PostVote.post_id == post.id).first() self.assertIsNone(vote) self.assertEqual(post.karma, 0) self.assertEqual(len(user.post_votes), 0) self.assertEqual(len(post.votes), 0) # cleanup cleanup(Post, PostVote, Community, User)