def has_voted(uid, xid, comment=False): """Check if a user has voted on a post or a comment, if so return the vote. 'xid' can be either a pid or a cid. If it is a cid, the comment flag needs to be set too True. """ if comment: result = r.zscore(K.COMMENT_VOTES.format(xid), uid) else: result = r.zscore(K.POST_VOTES.format(xid), uid) return result
def subscription_reason(user_id, post_id): """Returns the reason a user is subscribed to a post. """ return r.zscore(k.POST_SUBSCRIBERS.format(post_id), user_id)
def has_voted(user_id, post_id): """Check if a user has voted on a post or a comment, if so return the vote. """ return r.zscore(k.POST_VOTES.format(post_id), user_id)
def test_subscriptions(self): """ Test the backend subscription system. This includes subscribe(), unsubscribe() and is_subscribed(). This will also test the correct subscriptions after a post, comment or tagging. """ # Create a couple of test accounts user1 = create_account('user1', '*****@*****.**', 'Password') user2 = create_account('user2', '*****@*****.**', 'Password') user3 = create_account('user3', '*****@*****.**', 'Password') # Post as user 1 and ensure user 1 exists in Redis post1 = create_post(user1, 'user1', 'Test post 1') self.assertIsNotNone(r.zrank(K.POST_SUBSCRIBERS.format(post1), user1)) # Even though it is exactly the same as the above ensure that # is_subscribed() returns True self.assertTrue(is_subscribed(user1, post1)) # Lets ensure this actually fails! And see if user2 is a subscriber self.assertFalse(is_subscribed(user2, post1)) # Ensure that the REASON (zset score) is set to the correct value self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user1), SubscriptionReasons.POSTER) # Post a comment as user 1 and ensure the reason does NOT changes create_post(user1, 'user1', 'Test comment', post1) # Ensure our reason did not change # If we were not subscribed we would be given reason 2 (COMMENTER) self.assertNotEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user1), SubscriptionReasons.COMMENTER) # Test unsubscribe self.assertTrue(unsubscribe(user1, post1)) # Ensure that is_subscribed shows correct self.assertFalse(is_subscribed(user1, post1)) # Test that if we unsubscribe again we get a False result self.assertFalse(unsubscribe(user1, post1)) # Check that unsubscribing some that was never subscribed returns false self.assertFalse(unsubscribe(user2, post1)) # Let's test that commenting subscribes us to the post with the correct # reason. Try tag yourself at the same time create_post(user1, 'user1', 'Test comment @user1', post1) # Ensure we are subscribed self.assertTrue(is_subscribed(user1, post1)) # Check that our reason HAS changed self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user1), SubscriptionReasons.COMMENTER) # Create a comment as test2 and ensure this user becomes subscribed for # the same reason create_post(user2, 'user2', 'Test comment', post1) self.assertTrue(is_subscribed(user2, post1)) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user2), SubscriptionReasons.COMMENTER) # Create a new post as test1 and tag test2 and test3 in it # ensure all 3 are subscribed and test2 and test3 have the correct # reason. # Also try and tag ourselves this should have no affect # Try tagging someone that does not even exist post2 = create_post(user1, 'user1', 'Test post @user1 @user2 @user3 @user4') self.assertTrue(is_subscribed(user2, post2)) self.assertTrue(is_subscribed(user3, post2)) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post2), user2), SubscriptionReasons.TAGEE) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post2), user3), SubscriptionReasons.TAGEE) # Test tagging user 3 in a comment on post1. This ensures that tags # in comments do work. create_post(user2, 'user2', 'Test comment @user3', post1) self.assertTrue(is_subscribed(user3, post1)) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user3), SubscriptionReasons.TAGEE) # Unsubscribe user2 and user3 self.assertTrue(unsubscribe(user2, post2)) self.assertFalse(is_subscribed(user2, post2)) self.assertTrue(unsubscribe(user3, post2)) self.assertFalse(is_subscribed(user3, post2)) # Ensure that subscribe doe not happen when it is not a valid post self.assertFalse(subscribe(user1, K.NIL_VALUE, SubscriptionReasons.POSTER))
def test_subscriptions(self): """ Test the backend subscription system. This includes subscribe(), unsubscribe() and is_subscribed(). This will also test the correct subscriptions after a post, comment or tagging. """ # Create a couple of test accounts user1 = create_account('user1', '*****@*****.**', 'Password') user2 = create_account('user2', '*****@*****.**', 'Password') user3 = create_account('user3', '*****@*****.**', 'Password') activate(user1) activate(user2) activate(user3) # Post as user 1 and ensure user 1 exists in Redis post1 = create_post(user1, 'user1', 'Test post 1') self.assertIsNotNone(r.zrank(K.POST_SUBSCRIBERS.format(post1), user1)) # Even though it is exactly the same as the above ensure that # is_subscribed() returns True self.assertTrue(is_subscribed(user1, post1)) # Lets ensure this actually fails! And see if user2 is a subscriber self.assertFalse(is_subscribed(user2, post1)) # Ensure that the REASON (zset score) is set to the correct value self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user1), SubscriptionReasons.POSTER) # Post a comment as user 1 and ensure the reason does NOT changes create_post(user1, 'user1', 'Test comment', post1) # Ensure our reason did not change # If we were not subscribed we would be given reason 2 (COMMENTER) self.assertNotEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user1), SubscriptionReasons.COMMENTER) # Test unsubscribe self.assertTrue(unsubscribe(user1, post1)) # Ensure that is_subscribed shows correct self.assertFalse(is_subscribed(user1, post1)) # Test that if we unsubscribe again we get a False result self.assertFalse(unsubscribe(user1, post1)) # Check that unsubscribing some that was never subscribed returns false self.assertFalse(unsubscribe(user2, post1)) # Let's test that commenting subscribes us to the post with the correct # reason. Try tag yourself at the same time create_post(user1, 'user1', 'Test comment @user1', post1) # Ensure we are subscribed self.assertTrue(is_subscribed(user1, post1)) # Check that our reason HAS changed self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user1), SubscriptionReasons.COMMENTER) # Create a comment as test2 and ensure this user becomes subscribed for # the same reason create_post(user2, 'user2', 'Test comment', post1) self.assertTrue(is_subscribed(user2, post1)) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user2), SubscriptionReasons.COMMENTER) # Create a new post as test1 and tag test2 and test3 in it # ensure all 3 are subscribed and test2 and test3 have the correct # reason. # Also try and tag ourselves this should have no affect # Try tagging someone that does not even exist post2 = create_post(user1, 'user1', 'Test post @user1 @user2 @user3 @user4') self.assertTrue(is_subscribed(user2, post2)) self.assertTrue(is_subscribed(user3, post2)) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post2), user2), SubscriptionReasons.TAGEE) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post2), user3), SubscriptionReasons.TAGEE) # Test tagging user 3 in a comment on post1. This ensures that tags # in comments do work. create_post(user2, 'user2', 'Test comment @user3', post1) self.assertTrue(is_subscribed(user3, post1)) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user3), SubscriptionReasons.TAGEE) # Unsubscribe user2 and user3 self.assertTrue(unsubscribe(user2, post2)) self.assertFalse(is_subscribed(user2, post2)) self.assertTrue(unsubscribe(user3, post2)) self.assertFalse(is_subscribed(user3, post2)) # Ensure that subscribe doe not happen when it is not a valid post self.assertFalse( subscribe(user1, K.NIL_VALUE, SubscriptionReasons.POSTER))