def test_creates_users(self):
     self.add_response(body=self.make_response_body())
     result = UsersFetcher(screen_name='jill').fetch(
         [460060168, 26727655, 6795192])
     self.assertEqual(1, User.objects.filter(twitter_id=460060168).count())
     self.assertEqual(1, User.objects.filter(twitter_id=26727655).count())
     self.assertEqual(1, User.objects.filter(twitter_id=6795192).count())
 def test_updates_users(self):
     user = UserFactory.create(twitter_id=26727655, screen_name='bill')
     self.add_response(body=self.make_response_body())
     result = UsersFetcher(screen_name='jill').fetch(
         [460060168, 26727655, 6795192])
     self.assertEqual('Aiannucci',
                      User.objects.get(twitter_id=26727655).screen_name)
 def test_returns_correct_success_response(self):
     self.add_response(body=self.make_response_body())
     result = UsersFetcher(screen_name='jill').fetch(
         [460060168, 26727655, 6795192])
     self.assertEqual(result[0]['account'], 'jill')
     self.assertTrue(result[0]['success'])
     self.assertEqual(result[0]['fetched'], 3)
 def test_returns_error_if_api_call_fails(self):
     self.add_response(
         body='{"errors":[{"message":"Rate limit exceeded","code":88}]}',
         status=429)
     result = UsersFetcher(screen_name='jill').fetch(
         [460060168, 26727655, 6795192])
     self.assertFalse(result[0]['success'])
     self.assertIn('Rate limit exceeded', result[0]['messages'][0])
 def test_makes_correct_api_call(self):
     self.add_response(body=self.make_response_body())
     result = UsersFetcher(screen_name='jill').fetch(
         [460060168, 26727655, 6795192])
     self.assertIn('users/lookup.json?', responses.calls[0][0].url)
     self.assertIn('include_entities=true', responses.calls[0][0].url)
     self.assertIn('user_id=460060168%2C26727655%2C6795192',
                   responses.calls[0][0].url)
    def test_requests_all_users(self):
        "If no ids supplied, uses all users in the DB"
        users = UserFactory.create_batch(5)
        result = UsersFetcher(screen_name='jill').fetch()

        ids = User.objects.values_list('twitter_id',
                                       flat=True).order_by('fetch_time')
        ids = '%2C'.join(map(str, ids))
        self.assertIn(ids, responses.calls[0][0].url)
    def test_fetches_multiple_pages(self):
        # We're going to ask for 350 user 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 = {
                'user_id': '%2C'.join(map(str, ids[start:end])),
                'include_entities': 'true'
            }
            self.add_response(body=body,
                              querystring=qs,
                              match_querystring=True)
        # Then add the final 301-350 ids.
        qs['user_id'] = '%2C'.join(map(str, ids[-50:]))
        self.add_response(body=body, querystring=qs, match_querystring=True)

        with patch.object(FetchUsers, '_save_results'):
            with patch('time.sleep'):
                result = UsersFetcher(screen_name='jill').fetch(ids)
                self.assertEqual(4, len(responses.calls))
 def test_makes_one_api_call(self, fetch_avatar):
     self.add_response(body=self.make_response_body())
     result = UsersFetcher(screen_name='jill').fetch(
         [460060168, 26727655, 6795192])
     self.assertEqual(1, len(responses.calls))