Example #1
0
    def test_create_tweet_before_get_cached_tweets(self):
        tweet1 = self.create_tweet(user=self.user1)

        RedisClient.clear()
        conn = RedisClient.get_connection()
        name = USER_TWEET_PATTERN.format(user_id=self.user1.id)
        self.assertFalse(conn.exists(name))
        tweet2 = self.create_tweet(user=self.user1)
        self.assertTrue(conn.exists(name))

        tweets = TweetService.load_tweets_through_cache(user_id=self.user1.id)
        self.assertEqual([t.id for t in tweets], [tweet2.id, tweet1.id])
Example #2
0
    def test_tweet_cache_limit_pagination(self):
        limit_size = settings.REDIS_LIST_LENGTH_LIMIT
        page_size = 20

        tweets = [
            self.create_tweet(user=self.user1)
            for i in range(limit_size + page_size)
        ]
        tweets = tweets[::-1]

        # cached tweets
        cached_tweets = TweetService.load_tweets_through_cache(
            user_id=self.user1.id)
        self.assertEqual(len(cached_tweets), limit_size)
        db_tweets = Tweet.objects.filter(user_id=self.user1.id)
        self.assertEqual(len(db_tweets), limit_size + page_size)

        # paginate all tweets
        paginated_tweets = self._paginate_to_get_tweets(
            client=self.user2_client,
            user_id=self.user1.id,
        )
        self.assertEqual(len(paginated_tweets), limit_size + page_size)
        self.assertEqual([t['id'] for t in paginated_tweets],
                         [t.id for t in tweets])

        # create new tweet
        new_tweet = self.create_tweet(user=self.user1)

        def _test_tweets_after_pushed_new_tweet():
            new_paginated_tweets = self._paginate_to_get_tweets(
                client=self.user2_client,
                user_id=self.user1.id,
            )
            self.assertEqual(len(new_paginated_tweets),
                             limit_size + page_size + 1)
            self.assertEqual(new_paginated_tweets[0]['id'], new_tweet.id)
            for i in range(limit_size + page_size):
                self.assertEqual(tweets[i].id,
                                 new_paginated_tweets[i + 1]['id'])

        _test_tweets_after_pushed_new_tweet()

        # cache expire
        self.clear_cache()
        _test_tweets_after_pushed_new_tweet()
Example #3
0
 def list(self, request):
     # select out all tweets of a specific user
     cached_tweets = TweetService.load_tweets_through_cache(
         user_id=request.query_params['user_id']
     )
     page = self.paginator.paginate_cached_list(cached_tweets, request)
     # cache not enough
     if not page:
         tweets = Tweet.objects.filter(
             user_id=request.query_params['user_id']
         )
         page = self.paginate_queryset(tweets)
     serializer = TweetSerializer(
         page,
         context={'request': request},
         many=True,
     )
     # wrap the list of tweet contents in endless pagination
     return self.get_paginated_response(serializer.data)