def test_update_from_api( self, mock_all_screen_names, mock_all_user_ids, mock_update_id_mapping, mock_requests, ): mock_requests.post.side_effect = fake_twitter_api_post twitter_data = TwitterAPIData() mock_all_user_ids.return_value = ["1234", "42"] mock_all_screen_names.return_value = ["mhl20", "struan", "symroe"] twitter_data.user_id_to_screen_name = {"1234": "mhl20"} twitter_data.update_from_api() self.assertEqual( mock_update_id_mapping.mock_calls, [ call({ "id": 1234, "screen_name": "mhl20" }), call({ "id": 5678, "screen_name": "struan" }), call({ "id": 9012, "screen_name": "symroe" }), call({ "id": 42, "screen_name": "FooBarBazQuux" }), ], )
def test_zero_results_for_screen_name_lookup(self, mock_requests): twitter_data = TwitterAPIData() mock_requests.post.side_effect = fake_twitter_api_post twitter_results = list( twitter_data.twitter_results("screen_name", ["onlynonexistent"]) ) self.assertEqual(twitter_results, [])
def test_makes_requests(self, mock_requests): TwitterAPIData.MAX_IN_A_REQUEST = 2 twitter_data = TwitterAPIData() mock_requests.post.side_effect = fake_twitter_api_post twitter_results = list( twitter_data.twitter_results( "screen_name", ["mhl20", "struan", "symroe"] ) ) self.assertEqual( mock_requests.post.mock_calls, [ call( "https://api.twitter.com/1.1/users/lookup.json", headers={"Authorization": "Bearer madeuptoken"}, data={"screen_name": "mhl20,struan"}, ), call( "https://api.twitter.com/1.1/users/lookup.json", headers={"Authorization": "Bearer madeuptoken"}, data={"screen_name": "symroe"}, ), ], ) self.assertEqual( twitter_results, [ {"id": 1234, "screen_name": "mhl20"}, {"id": 5678, "screen_name": "struan"}, {"id": 9012, "screen_name": "symroe"}, ], )
def test_zero_results_for_user_id_lookup(self, mock_requests): twitter_data = TwitterAPIData() mock_requests.post.side_effect = fake_twitter_api_post twitter_results = list( twitter_data.twitter_results("user_id", ["13984716923847632"]) ) self.assertEqual(twitter_results, [])
def tests_all_user_ids(self): joe = PersonFactory.create(id=1, name="Joe Bloggs") joe.identifiers.create(identifier="246", scheme="twitter") jane = PersonFactory.create(id=2, name="Jane Bloggs") jane.identifiers.create(identifier="357", scheme="twitter") twitter_data = TwitterAPIData() self.assertEqual(["246", "357"], sorted(twitter_data.all_user_ids))
def test_update_individual_data(self): twitter_data = TwitterAPIData() twitter_data.update_id_mapping({ "id": 42, "screen_name": "FooBarBazQuux", "profile_image_url_https": "https://example.com/foo.jpg", }) self.assertEqual(twitter_data.screen_name_to_user_id, {"foobarbazquux": "42"}) self.assertEqual(twitter_data.user_id_to_screen_name, {"42": "FooBarBazQuux"}) self.assertEqual( twitter_data.user_id_to_photo_url, {"42": "https://example.com/foo.jpg"}, )
def test_all_screen_names(self): joe = PersonFactory.create(id=1, name="Joe Bloggs") joe.tmp_person_identifiers.create(value="joenotreallyatwitteraccount", value_type="twitter_username") jane = PersonFactory.create(id=2, name="Jane Bloggs") jane.tmp_person_identifiers.create( value="janenotreallyatwitteraccount", value_type="twitter_username") twitter_data = TwitterAPIData() self.assertEqual( ["janenotreallyatwitteraccount", "joenotreallyatwitteraccount"], sorted(twitter_data.all_screen_names), )
def test_all_screen_names(self): joe = PersonFactory.create(id=1, name="Joe Bloggs") joe.contact_details.create( value="joenotreallyatwitteraccount", contact_type="twitter" ) jane = PersonFactory.create(id=2, name="Jane Bloggs") jane.contact_details.create( value="janenotreallyatwitteraccount", contact_type="twitter" ) twitter_data = TwitterAPIData() self.assertEqual( ["janenotreallyatwitteraccount", "joenotreallyatwitteraccount"], sorted(twitter_data.all_screen_names), )
def test_error_on_missing_token(self): with self.assertRaisesRegex( Exception, r"TWITTER_APP_ONLY_BEARER_TOKEN was not set"): TwitterAPIData()
def test_unfaked_urls_raise_exception(self, mock_requests): TwitterAPIData.MAX_IN_A_REQUEST = 2 twitter_data = TwitterAPIData() mock_requests.post.side_effect = fake_twitter_api_post with self.assertRaises(Exception): list(twitter_data.twitter_results("screen_name", ["foo", "bar"]))