def test_caption_query_get_by_language_code_when_not_exists(): caption1 = Caption( {"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"} ) caption2 = Caption( {"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"} ) caption_query = CaptionQuery(captions=[caption1, caption2]) assert caption_query.get_by_language_code("hello") is None
def test_download_caption_with_language_found(youtube): youtube.title = "video title" caption = Caption( {"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"} ) caption.download = MagicMock(return_value="file_path") youtube.captions = CaptionQuery([caption]) cli.download_caption(youtube, "en") caption.download.assert_called_with(title="video title", output_path=None)
def test_caption_query_all(): caption1 = Caption( {"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"} ) caption2 = Caption( {"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"} ) caption_query = CaptionQuery(captions=[caption1, caption2]) assert caption_query.captions == [caption1, caption2]
def test_download_caption_with_lang_not_found(youtube, print_available): # Given caption = Caption( {"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"} ) youtube.captions = CaptionQuery([caption]) # When cli.download_caption(youtube, "blah") # Then print_available.assert_called_with(youtube.captions)
def test_repr(): caption = Caption({ "url": "url1", "name": { "simpleText": "name1" }, "languageCode": "en" }) assert str(caption) == '<Caption lang="name1" code="en">' caption_query = CaptionQuery(captions=[caption]) assert repr(caption_query) == '{\'en\': <Caption lang="name1" code="en">}'
def test_download_caption_with_language_not_found(youtube): caption = Caption({ "url": "url1", "name": { "simpleText": "name1" }, "languageCode": "en" }) youtube.captions = CaptionQuery([caption]) with patch.object(youtube.captions, "all", wraps=youtube.captions.all) as wrapped_all: cli.download_caption(youtube, "blah") wrapped_all.assert_called()
def test_print_available_captions(capsys): # Given caption1 = Caption( {"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"} ) caption2 = Caption( {"url": "url2", "name": {"simpleText": "name2"}, "languageCode": "fr"} ) query = CaptionQuery([caption1, caption2]) # When cli._print_available_captions(query) # Then captured = capsys.readouterr() assert captured.out == "Available caption codes are: en, fr\n"
def test_caption_query_get_by_language_code_when_not_exists(): caption1 = Caption({ "url": "url1", "name": { "simpleText": "name1" }, "languageCode": "en" }) caption2 = Caption({ "url": "url2", "name": { "simpleText": "name2" }, "languageCode": "fr" }) caption_query = CaptionQuery(captions=[caption1, caption2]) with pytest.raises(KeyError): assert caption_query["hello"] is not None
def test_caption_query_get_by_language_code_when_exists(): caption1 = Caption({ "url": "url1", "name": { "simpleText": "name1" }, "languageCode": "en", "vssId": ".en" }) caption2 = Caption({ "url": "url2", "name": { "simpleText": "name2" }, "languageCode": "fr", "vssId": ".fr" }) caption_query = CaptionQuery(captions=[caption1, caption2]) assert caption_query["en"] == caption1
def test_caption_query_sequence(): caption1 = Caption({ "url": "url1", "name": { "simpleText": "name1" }, "languageCode": "en" }) caption2 = Caption({ "url": "url2", "name": { "simpleText": "name2" }, "languageCode": "fr" }) caption_query = CaptionQuery(captions=[caption1, caption2]) assert len(caption_query) == 2 assert caption_query["en"] == caption1 assert caption_query["fr"] == caption2 with pytest.raises(KeyError): assert caption_query["nada"] is not None
def captions(self): """Interface to query caption tracks. :rtype: :class:`CaptionQuery <CaptionQuery>`. """ return CaptionQuery([c for c in self.caption_tracks])
def captions(self) -> CaptionQuery: """Interface to query caption tracks. :rtype: :class:`CaptionQuery <CaptionQuery>`. """ return CaptionQuery(self.caption_tracks)
def _print_available_captions(captions: CaptionQuery) -> None: print("Available caption codes are: {}".format(", ".join( c.code for c in captions.all())))