def test_get_url(self): api_url = self.loader.get_url() self.assertEqual( api_url, "http://gdata.youtube.com/feeds/api/videos/J_DV9b0x7v4?v=2&alt=json") loader = ApiLoader("http://www.youtube.com/watch?v=ZSh_c7-fZqQ") api_url = loader.get_url() self.assertEqual( api_url, "http://gdata.youtube.com/feeds/api/videos/ZSh_c7-fZqQ?v=2&alt=json")
def test_get_url(self): api_url = self.loader.get_url() self.assertEqual(api_url, "http://gdata.youtube.com/feeds/api/videos/J_DV9b0x7v4?v=2&alt=json") loader = ApiLoader("http://www.youtube.com/watch?v=ZSh_c7-fZqQ") api_url = loader.get_url() self.assertEqual(api_url, "http://gdata.youtube.com/feeds/api/videos/ZSh_c7-fZqQ?v=2&alt=json")
class YouTubeApiTestCase(YouTubeTestCase): def setUp(self): YouTubeTestCase.setUp(self) self.loader = ApiLoader(self.url) def test_get_headers__api_key(self): loader = ApiLoader(self.url, api_keys={'youtube_key': 'BLANK'}) self.assertEqual(loader.get_headers().get('X-GData-Key'), "key=BLANK") def test_get_url(self): api_url = self.loader.get_url() self.assertEqual( api_url, "http://gdata.youtube.com/feeds/api/videos/J_DV9b0x7v4?v=2&alt=json") loader = ApiLoader("http://www.youtube.com/watch?v=ZSh_c7-fZqQ") api_url = loader.get_url() self.assertEqual( api_url, "http://gdata.youtube.com/feeds/api/videos/ZSh_c7-fZqQ?v=2&alt=json") def test_get_video_data(self): api_file = self.get_data_file('youtube/api.json') response = self.get_response(api_file.read()) data = self.loader.get_video_data(response) self.assertEqual(set(data), self.loader.fields) data['tags'] = set(data['tags']) self.assertDictEqual(data, CARAMELL_DANSEN_API_DATA) def test_get_video_data__pretty_name(self): """ The author's "name" isn't the same as their username. We want their username. """ api_file = self.get_data_file('youtube/api_pretty_name.json') response = self.get_response(api_file.read()) data = self.loader.get_video_data(response) self.assertEqual(data['user'], 'marketingcoachmike') def test_get_video_data__forbidden(self): expected_data = {'is_embeddable': False} response = self.get_response('', code=requests.codes.forbidden) data = self.loader.get_video_data(response) self.assertDictEqual(data, expected_data) def test_get_video_data__unauthorized(self): expected_data = {'is_embeddable': False} response = self.get_response('', code=requests.codes.unauthorized) data = self.loader.get_video_data(response) self.assertDictEqual(data, expected_data) def test_get_video_data__restricted(self): api_file = self.get_data_file('youtube/restricted_api.json') response = self.get_response(api_file.read()) data = self.loader.get_video_data(response) self.assertTrue(isinstance(data, dict)) self.assertEqual(data['description'], "Like dolphins, whales communicate using sound. \ Humpbacks especially have extremely complex communication systems.") def test_get_video_data__missing_keywords(self): api_file = self.get_data_file('youtube/missing_keywords.json') response = self.get_response(api_file.read()) data = self.loader.get_video_data(response) self.assertTrue(isinstance(data, dict)) self.assertEqual(data['tags'], ['Nonprofit'])
class YouTubeApiTestCase(YouTubeTestCase): def setUp(self): YouTubeTestCase.setUp(self) self.loader = ApiLoader(self.url) def test_get_headers__api_key(self): loader = ApiLoader(self.url, api_keys={"youtube_key": "BLANK"}) self.assertEqual(loader.get_headers().get("X-GData-Key"), "key=BLANK") def test_get_url(self): api_url = self.loader.get_url() self.assertEqual(api_url, "http://gdata.youtube.com/feeds/api/videos/J_DV9b0x7v4?v=2&alt=json") loader = ApiLoader("http://www.youtube.com/watch?v=ZSh_c7-fZqQ") api_url = loader.get_url() self.assertEqual(api_url, "http://gdata.youtube.com/feeds/api/videos/ZSh_c7-fZqQ?v=2&alt=json") def test_get_video_data(self): api_file = self.get_data_file("youtube/api.json") response = self.get_response(api_file.read()) data = self.loader.get_video_data(response) self.assertEqual(set(data), self.loader.fields) data["tags"] = set(data["tags"]) self.assertDictEqual(data, CARAMELL_DANSEN_API_DATA) def test_get_video_data__pretty_name(self): """ The author's "name" isn't the same as their username. We want their username. """ api_file = self.get_data_file("youtube/api_pretty_name.json") response = self.get_response(api_file.read()) data = self.loader.get_video_data(response) self.assertEqual(data["user"], "marketingcoachmike") def test_get_video_data__forbidden(self): expected_data = {"is_embeddable": False} response = self.get_response("", code=requests.codes.forbidden) data = self.loader.get_video_data(response) self.assertDictEqual(data, expected_data) def test_get_video_data__unauthorized(self): expected_data = {"is_embeddable": False} response = self.get_response("", code=requests.codes.unauthorized) data = self.loader.get_video_data(response) self.assertDictEqual(data, expected_data) def test_get_video_data__restricted(self): api_file = self.get_data_file("youtube/restricted_api.json") response = self.get_response(api_file.read()) data = self.loader.get_video_data(response) self.assertTrue(isinstance(data, dict)) self.assertEqual( data["description"], "Like dolphins, whales communicate using sound. \ Humpbacks especially have extremely complex communication systems.", ) def test_get_video_data__missing_keywords(self): api_file = self.get_data_file("youtube/missing_keywords.json") response = self.get_response(api_file.read()) data = self.loader.get_video_data(response) self.assertTrue(isinstance(data, dict)) self.assertEqual(data["tags"], ["Nonprofit"])