Update test_api.py

Fixed an encoding issue for Windows machines receiving an error due to Lines 24-27:
`UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 119434: character maps to <undefined>`.

Adjusted test_api.py to fix an issue when the format=None isnt passed with mock test raising:
`AssertionError: get_transcript('video_id_1', ['de', 'en'], None, None) call not found`

Added `import json` to convert test data for json data into a string for 1 or many json transcripts to keep in line with how the formatters are eventually returned as strings.
This commit is contained in:
Chris Howell 2020-07-08 15:11:54 -07:00
parent dca4021dd7
commit ac75f906e6
1 changed files with 18 additions and 15 deletions

View File

@ -1,6 +1,6 @@
from unittest import TestCase from unittest import TestCase
from mock import patch from mock import patch
import json
import os import os
import requests import requests
@ -21,7 +21,10 @@ from youtube_transcript_api import (
def load_asset(filename): def load_asset(filename):
with open('{dirname}/assets/{filename}'.format(dirname=os.path.dirname(__file__), filename=filename)) as file: filepath = '{dirname}/assets/{filename}'.format(
dirname=os.path.dirname(__file__), filename=filename)
with open(filepath, 'r', encoding='utf-8') as file:
return file.read() return file.read()
@ -47,11 +50,11 @@ class TestYouTubeTranscriptApi(TestCase):
self.assertEqual( self.assertEqual(
transcript, transcript,
[ json.dumps([
{'text': 'Hey, this is just a test', 'start': 0.0, 'duration': 1.54}, {'text': 'Hey, this is just a test', 'start': 0.0, 'duration': 1.54},
{'text': 'this is not the original transcript', 'start': 1.54, 'duration': 4.16}, {'text': 'this is not the original transcript', 'start': 1.54, 'duration': 4.16},
{'text': 'just something shorter, I made up for testing', 'start': 5.7, 'duration': 3.239} {'text': 'just something shorter, I made up for testing', 'start': 5.7, 'duration': 3.239}
] ])
) )
def test_list_transcripts(self): def test_list_transcripts(self):
@ -158,15 +161,15 @@ class TestYouTubeTranscriptApi(TestCase):
def test_get_transcript__with_proxy(self): def test_get_transcript__with_proxy(self):
proxies = {'http': '', 'https:': ''} proxies = {'http': '', 'https:': ''}
transcript = YouTubeTranscriptApi.get_transcript( transcript = YouTubeTranscriptApi.get_transcript(
'GJLlxj_dtq8', proxies=proxies 'GJLlxj_dtq8', proxies=proxies, format=None
) )
self.assertEqual( self.assertEqual(
transcript, transcript,
[ json.dumps([
{'text': 'Hey, this is just a test', 'start': 0.0, 'duration': 1.54}, {'text': 'Hey, this is just a test', 'start': 0.0, 'duration': 1.54},
{'text': 'this is not the original transcript', 'start': 1.54, 'duration': 4.16}, {'text': 'this is not the original transcript', 'start': 1.54, 'duration': 4.16},
{'text': 'just something shorter, I made up for testing', 'start': 5.7, 'duration': 3.239} {'text': 'just something shorter, I made up for testing', 'start': 5.7, 'duration': 3.239}
] ])
) )
def test_get_transcript__with_cookies(self): def test_get_transcript__with_cookies(self):
@ -176,11 +179,11 @@ class TestYouTubeTranscriptApi(TestCase):
self.assertEqual( self.assertEqual(
transcript, transcript,
[ json.dumps([
{'text': 'Hey, this is just a test', 'start': 0.0, 'duration': 1.54}, {'text': 'Hey, this is just a test', 'start': 0.0, 'duration': 1.54},
{'text': 'this is not the original transcript', 'start': 1.54, 'duration': 4.16}, {'text': 'this is not the original transcript', 'start': 1.54, 'duration': 4.16},
{'text': 'just something shorter, I made up for testing', 'start': 5.7, 'duration': 3.239} {'text': 'just something shorter, I made up for testing', 'start': 5.7, 'duration': 3.239}
] ])
) )
@patch('youtube_transcript_api.YouTubeTranscriptApi.get_transcript') @patch('youtube_transcript_api.YouTubeTranscriptApi.get_transcript')
@ -191,8 +194,8 @@ class TestYouTubeTranscriptApi(TestCase):
YouTubeTranscriptApi.get_transcripts([video_id_1, video_id_2], languages=languages) YouTubeTranscriptApi.get_transcripts([video_id_1, video_id_2], languages=languages)
mock_get_transcript.assert_any_call(video_id_1, languages, None, None) mock_get_transcript.assert_any_call(video_id_1, languages, None, None, format=None)
mock_get_transcript.assert_any_call(video_id_2, languages, None, None) mock_get_transcript.assert_any_call(video_id_2, languages, None, None, format=None)
self.assertEqual(mock_get_transcript.call_count, 2) self.assertEqual(mock_get_transcript.call_count, 2)
@patch('youtube_transcript_api.YouTubeTranscriptApi.get_transcript', side_effect=Exception('Error')) @patch('youtube_transcript_api.YouTubeTranscriptApi.get_transcript', side_effect=Exception('Error'))
@ -207,20 +210,20 @@ class TestYouTubeTranscriptApi(TestCase):
YouTubeTranscriptApi.get_transcripts(['video_id_1', 'video_id_2'], continue_after_error=True) YouTubeTranscriptApi.get_transcripts(['video_id_1', 'video_id_2'], continue_after_error=True)
mock_get_transcript.assert_any_call(video_id_1, ('en',), None, None) mock_get_transcript.assert_any_call(video_id_1, ('en',), None, None, format=None)
mock_get_transcript.assert_any_call(video_id_2, ('en',), None, None) mock_get_transcript.assert_any_call(video_id_2, ('en',), None, None, format=None)
@patch('youtube_transcript_api.YouTubeTranscriptApi.get_transcript') @patch('youtube_transcript_api.YouTubeTranscriptApi.get_transcript')
def test_get_transcripts__with_cookies(self, mock_get_transcript): def test_get_transcripts__with_cookies(self, mock_get_transcript):
cookies = '/example_cookies.txt' cookies = '/example_cookies.txt'
YouTubeTranscriptApi.get_transcripts(['GJLlxj_dtq8'], cookies=cookies) YouTubeTranscriptApi.get_transcripts(['GJLlxj_dtq8'], cookies=cookies)
mock_get_transcript.assert_any_call('GJLlxj_dtq8', ('en',), None, cookies) mock_get_transcript.assert_any_call('GJLlxj_dtq8', ('en',), None, cookies, format=None)
@patch('youtube_transcript_api.YouTubeTranscriptApi.get_transcript') @patch('youtube_transcript_api.YouTubeTranscriptApi.get_transcript')
def test_get_transcripts__with_proxies(self, mock_get_transcript): def test_get_transcripts__with_proxies(self, mock_get_transcript):
proxies = {'http': '', 'https:': ''} proxies = {'http': '', 'https:': ''}
YouTubeTranscriptApi.get_transcripts(['GJLlxj_dtq8'], proxies=proxies) YouTubeTranscriptApi.get_transcripts(['GJLlxj_dtq8'], proxies=proxies)
mock_get_transcript.assert_any_call('GJLlxj_dtq8', ('en',), proxies, None) mock_get_transcript.assert_any_call('GJLlxj_dtq8', ('en',), proxies, None, format=None)
def test_load_cookies(self): def test_load_cookies(self):
dirname, filename = os.path.split(os.path.abspath(__file__)) dirname, filename = os.path.split(os.path.abspath(__file__))