def test_result_is_zero(self): video_id = 'we342oij234' ydl = Mock() ydl.download = Mock(return_value=0) with patch('youtube_dl.YoutubeDL.__enter__', return_value=ydl): download = Download() self.assertEqual(0, download.get_result(video_id)) ydl.download.assert_called_with( ['http://www.youtube.com/watch?v=we342oij234'])
def test_caption(self): tests = [{'name': '1 video, caption found', 'urls': ['https://www.swag.com/'], 'pattern': 'vision', 'regex': False, 'links': False, 'expected': '[00:00:17.350 --> 00:00:18.752] we have this ' + red('vision') + ' of einstein'}, {'name': '1 video, caption not found', 'urls': ['https://www.swag.com/'], 'pattern': 'iwontbefound', 'regex': False, 'links': False, 'expected': '', }, {'name': '1 video, caption found more than once', 'urls': ['https://www.swag.com/'], 'pattern': 'light', 'regex': False, 'links': False, 'expected': '[00:00:33.666 --> 00:00:38.138] actor as einstein: what ' + red('light') + ' would i see if i rode on a beam of ' + red('light') + '?', }, {'name': '1 video, regular expression', 'urls': ['https://www.swag.com/'], 'pattern': 'actor|light', 'regex': True, 'links': False, 'expected': '[00:00:33.666 --> 00:00:38.138] ' + red('actor') + ' as einstein: what ' + red('light') + ' would i see if i rode on a beam of ' + red('light') + '?', }, {'name': '1 video, 1 link', 'urls': ['https://www.swag.com/'], 'pattern': 'actor|light', 'regex': True, 'links': True, 'expected': '[00:00:33.666 --> 00:00:38.138] ' + red('actor') + ' as einstein: what ' + red('light') + ' would i see if i rode on a beam of ' + red('light') + '? (https://www.swag.com/&t=33s)', }, ] for test in tests: download = Download({'urls': test['urls'], 'pattern': test['pattern'], 'e': test['regex'], 'v': False, 'links': test['links']}) m = mock_open(read_data=FIXTURE_WEBVTT) with patch('ytcc.download.open', m, create=True): with patch('ytcc.storage.Storage.remove_file', Mock()): download.get_result = Mock(return_value=0) actual = download.get_captions() expected = test['expected'] self.assertEqual(actual, expected)
def test_caption_captions_do_not_exist(self): test = { 'name': 'captions do not exist', 'urls': ['https://www.swag.com/'], 'pattern': 'my pattern', 'regex': False, 'links': False, } download = Download({'urls': test['urls'], 'pattern': test['pattern'], 'e': test['regex'], 'v': False, 'links': test['links']}) m = mock_open(read_data=FIXTURE_WEBVTT) m.side_effect = FileNotFoundError with patch('ytcc.download.open', m, create=True): with patch('ytcc.storage.Storage.remove_file', Mock()): download.get_result = Mock(return_value=0) with self.assertRaises(NoCaptionsException): download.get_captions()