def test_upvote_non_existent_post(self): # Try to upvote a post that doesn't exist user = create_test_user() self.assertRaises(ValueError, post_functions.upvote, user.id, -1) self.assertRaises(ValueError, post_functions.upvote, user.id, None) self.assertRaises(exc.InterfaceError, post_functions.upvote, user.id, []) cleanup(User)
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_join(self): # a user joins the community user = create_test_user() community = create_test_community() community_functions.join(user.id, community.id) self.assertIn(user, community.users) # Cleanup cleanup(User, Community)
def test_create_post_non_existent_community(self): # Test create_post() with a non existent community. # Should raise ValueError for int and None, # exc.InterfaceError for weird not-None and not-int objects user = create_test_user() self.assertRaises(ValueError, post_functions.create_post, user.id, None, "Title", "Body") self.assertRaises(ValueError, post_functions.create_post, user.id, -1, "Title", "Body") self.assertRaises(exc.InterfaceError, post_functions.create_post, user.id, [], "Title", "Body") # Cleanup cleanup(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_leave_non_existent_community(self): # Test leave() with a non existent community user = create_test_user() self.assertRaises(AttributeError, community_functions.leave, user.id, None) self.assertRaises(AttributeError, community_functions.leave, user.id, -1) self.assertRaises(exc.InterfaceError, community_functions.leave, user.id, []) # Cleanup cleanup(User)
def test_create_comment_non_existent_post(self): # Trying to create a comment on a post that doesn't exist user = create_test_user() # No need to create a community or post. self.assertRaises(AttributeError, comment_functions.create_comment, user.id, -1, "Test") self.assertRaises(AttributeError, comment_functions.create_comment, user.id, None, "Test") self.assertRaises(exc.InterfaceError, comment_functions.create_comment, user.id, [], "test") cleanup(User)
def test_create_post_banned_user(self): # Test create_post() with a banned user as the author # the user should not be able to post, and the function # should raise PermissionError. user = create_test_user() community = create_test_community() community.banned_users.append(user) db.session.commit() # Test self.assertRaises(PermissionError, post_functions.create_post, user.id, community.id, "Title", "post body") # Cleanup cleanup(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_join_non_existent_community(self): # a user tries to join a non existent community # should raise AttributeError because community is None user = create_test_user() # Pass actual None self.assertRaises(AttributeError, community_functions.join, user.id, None) # Pass an ID that won't be in the database self.assertRaises(AttributeError, community_functions.join, user.id, -1) # Pass some weird not-None and not-int object - raises InterfaceError self.assertRaises(exc.InterfaceError, community_functions.join, user.id, []) # Cleanup cleanup(User)
def test_create_community_already_exists(self): # Create a community with a name that already exists in the database. # This should result in sqlalchemy.exc.IntegrityError due to a # violation of the unique constraint for the name column user = create_test_user() # Create a mock community without adding users test_com = create_test_community() name = test_com.name self.assertRaises(exc.IntegrityError, community_functions.create_community, name=name, description="Testing", owner_user_id=user.id) # Clean up db.session.rollback() cleanup(User, Community)
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_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_create_post(self): # Test create_post() user = create_test_user() community = create_test_community() community.users.append(user) db.session.commit() # Now create a post and verify that it exists title = "The first ever test post!" body = "Some long ass f*****g text just for testing and all that shit lolcrap." post_functions.create_post(user.id, community.id, title, body) # Now verify post = Post.query.filter(User.id == user.id).first() self.assertIsNotNone(post) self.assertIn(post, user.posts) self.assertIn(post, community.posts) # Cleanup cleanup(Post, Community, User)
def test_create_community(self): # Test the create_community() function # Expected exceptions: # exc.IntegrityError in case the unique constraint is violated due to duplicate communities # exc.FlushError if the owner user is a None object # AttributeError if trying to add a non-User object to the community user = create_test_user() community_functions.create_community(name="TestCommunity", description="For testing", owner_user_id=user.id) # Now verify that the community was created com_obj = Community.query.filter( Community.name == "TestCommunity").first() self.assertIsNotNone(com_obj) self.assertIn(user, com_obj.users) self.assertIn(user, com_obj.owners) # Clean up cleanup(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_community(self): community = create_test_community() cid = community.id user = create_test_user() community.users.append(user) community.admins.append(user) db.session.commit() # Now assert that the user has these relationships self.assertIn(community, user.communities) self.assertIn(community, user.admin) # Now delete the community and assert that the relationship have been deleted community_functions.delete_community(cid) self.assertNotIn(community, user.communities) self.assertNotIn(community, user.admin) # Assert that the community object is None community = Community.query.filter(Community.id == cid).first() self.assertIsNone(community) # Cleanup cleanup(User)
def test_leave(self): # Test that the leave() function clears the user from all the relationship groups user = create_test_user() community = create_test_community() # First add the user to all the relationship groups subgroups = ("users", "owners", "admins", "moderators") for subgroup in subgroups: obj = getattr(community, subgroup) obj.append(user) db.session.commit() # Now verify that the user is in there for subgroup in subgroups: obj = getattr(community, subgroup) self.assertIn(user, obj) # Now leave and then assert that the user isn't in any of groups community_functions.leave(user.id, community.id) for subgroup in subgroups: obj = getattr(community, subgroup) self.assertNotIn(user, obj) # Cleanup cleanup(Community, User)
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)
def test_downvote_non_existent_post(self): user = create_test_user() self.assertRaises(ValueError, post_functions.downvote, user.id, -1) self.assertRaises(ValueError, post_functions.downvote, user.id, None) self.assertRaises(exc.InterfaceError, post_functions.downvote, user.id, []) cleanup(User)