예제 #1
0
    def test_fetches_multiple_pages(self):
        """
        The mocked requests work without specifying the data we're POSTing,
        so... not doing anything with it.
        """
        # We're going to ask for 350 tweet IDs which will be split over 4 pages.
        ids = [id for id in range(1, 351)]
        body = json.dumps([{'id': id} for id in range(1, 100)])

        for n in range(3):
            # First time, add ids 1-100. Then 101-200. Then 201-300.
            start = n * 100
            end = (n + 1) * 100
            #qs = {
            #'id': ','.join(map(str, ids[start:end])),
            #'include_entities': 'true',
            #'trim_user': '******',
            #'map': 'false'}
            self.add_response(body=body, method='POST')
        # Then add the final 301-350 ids.
        #qs['id'] = ','.join(map(str, ids[-50:]))
        self.add_response(body=body, method='POST')

        with patch.object(FetchTweets, '_save_results'):
            with patch('time.sleep'):
                result = TweetsFetcher(screen_name='jill').fetch(ids)
                self.assertEqual(4, len(responses.calls))
예제 #2
0
 def test_returns_error_if_api_call_fails(self):
     self.add_response(
         body='{"errors":[{"message":"Rate limit exceeded","code":88}]}',
         status=429,
         method='POST')
     result = TweetsFetcher(screen_name='jill').fetch([300, 200, 100])
     self.assertFalse(result[0]['success'])
     self.assertIn('Rate limit exceeded', result[0]['messages'][0])
예제 #3
0
 def test_updates_tweets(self):
     tweets = TweetFactory.create(twitter_id=200, text='Will change')
     self.add_response(body=self.make_response_body(), method='POST')
     result = TweetsFetcher(screen_name='jill').fetch([300, 200, 100])
     self.assertEqual(
         Tweet.objects.get(twitter_id=200).text,
         "@rooreynolds I've read stories of people travelling abroad who were mistaken for military/security because of that kind of thing… careful :)"
     )
예제 #4
0
    def test_requests_all_tweets(self):
        "If no ids supplied, uses all tweets in the DB"
        tweets = TweetFactory.create_batch(5)
        result = TweetsFetcher(screen_name='jill').fetch()

        ids = Tweet.objects.values_list('twitter_id',
                                        flat=True).order_by('fetch_time')

        params = QueryDict(responses.calls[0][0].body)
        self.assertIn('id', params)
        self.assertEqual(','.join(map(str, ids)), params['id'])
예제 #5
0
    def test_makes_correct_api_call(self):
        self.add_response(body=self.make_response_body(), method='POST')
        result = TweetsFetcher(screen_name='jill').fetch([300, 200, 100])
        self.assertIn('statuses/lookup.json', responses.calls[0][0].url)

        # To get the POST params:
        params = QueryDict(responses.calls[0][0].body)

        self.assertIn('include_entities', params)
        self.assertEqual(params['include_entities'], 'true')
        self.assertIn('trim_user', params)
        self.assertEqual(params['trim_user'], 'false')
        self.assertIn('map', params)
        self.assertEqual(params['map'], 'false')
        self.assertIn('id', params)
        self.assertEqual(params['id'], '300,200,100')
예제 #6
0
 def test_creates_tweets(self):
     self.add_response(body=self.make_response_body(), method='POST')
     result = TweetsFetcher(screen_name='jill').fetch([300, 200, 100])
     self.assertEqual(1, Tweet.objects.filter(twitter_id=300).count())
     self.assertEqual(1, Tweet.objects.filter(twitter_id=200).count())
     self.assertEqual(1, Tweet.objects.filter(twitter_id=100).count())
예제 #7
0
 def test_returns_correct_success_response(self):
     self.add_response(body=self.make_response_body(), method='POST')
     result = TweetsFetcher(screen_name='jill').fetch([300, 200, 100])
     self.assertEqual(result[0]['account'], 'jill')
     self.assertTrue(result[0]['success'])
     self.assertEqual(result[0]['fetched'], 3)
예제 #8
0
 def test_makes_one_api_call(self, download):
     # This will just stop us requesting avatars from Twitter:
     download.side_effect = DownloadException('Ooops')
     self.add_response(body=self.make_response_body(), method='POST')
     result = TweetsFetcher(screen_name='jill').fetch([300, 200, 100])
     self.assertEqual(1, len(responses.calls))