Exemplo n.º 1
0
def test_download_subtitles(monkeypatch, tmpdir, video, tvshow, create_sub,
                            create_tvepisode, p):
    # Given
    subtitles = [
        create_sub(language=code, id=sid, content=content)
        for sid, code, content in p['list_subtitles']
    ]
    best_subtitles = [
        create_sub(language=code, id=sid, content=content)
        for sid, code, content in p['best_subtitles']
    ]
    video_path = str(tmpdir.ensure(video.name))
    tvepisode = create_tvepisode(show=tvshow,
                                 season=3,
                                 episode=4,
                                 subtitles=p['existing_subtitles'])
    external_subtitles = p['external_subtitles']
    embedded_subtitles = p['embedded_subtitles'] if p[
        'embedded_subtitles'] is not None else True
    refine = Mock()
    compute_score = Mock(return_value=1)
    list_subtitles = Mock(return_value=subtitles)
    download_best_subtitles = Mock(return_value=best_subtitles)
    popen = Mock()
    monkeypatch.setattr(app, 'SYS_ENCODING', 'utf-8')
    monkeypatch.setattr(app, 'SUBTITLES_MULTI', p['multiple_subtitles'])
    monkeypatch.setattr(app, 'SUBTITLES_LANGUAGES', p['wanted_languages'])
    monkeypatch.setattr(app, 'SUBTITLES_PRE_SCRIPTS', p['pre_scripts'])
    monkeypatch.setattr(app, 'SUBTITLES_EXTRA_SCRIPTS', p['post_scripts'])
    monkeypatch.setattr(app, 'SUBTITLES_HEARING_IMPAIRED',
                        p['hearing_impaired'])
    monkeypatch.setattr(sut, 'refine', refine)
    monkeypatch.setattr(sut, 'compute_score', compute_score)
    monkeypatch.setattr(ProviderPool, 'list_subtitles', list_subtitles)
    monkeypatch.setattr(ProviderPool, 'download_best_subtitles',
                        download_best_subtitles)
    monkeypatch.setattr(subprocess, 'Popen', popen)

    # When
    actual = sut.download_subtitles(tv_episode=tvepisode,
                                    video_path=video_path,
                                    subtitles=external_subtitles,
                                    embedded_subtitles=embedded_subtitles)

    # Then
    assert p['expected'] == actual
    if p['expected']:
        assert len(p['pre_scripts']) + len(p['post_scripts']) * len(
            p['best_subtitles']) == popen.call_count
    if refine.called:
        assert embedded_subtitles == refine.call_args[1]['embedded_subtitles']
        assert tvepisode == refine.call_args[1]['tv_episode']
Exemplo n.º 2
0
def test_download_subtitles(monkeypatch, tmpdir, video, tvshow, create_sub, create_tvepisode, p):
    # Given
    subtitles = [create_sub(language=code, id=sid, content=content) for sid, code, content in p['list_subtitles']]
    best_subtitles = [create_sub(language=code, id=sid, content=content) for sid, code, content in p['best_subtitles']]
    video_path = text_type(tmpdir.ensure(video.name))
    tvepisode = create_tvepisode(series=tvshow, season=3, episode=4, subtitles=p['existing_subtitles'])
    external_subtitles = p['external_subtitles']
    embedded_subtitles = p['embedded_subtitles'] if p['embedded_subtitles'] is not None else True
    refine = Mock()
    compute_score = Mock(return_value=1)
    list_subtitles = Mock(return_value=subtitles)
    download_best_subtitles = Mock(return_value=best_subtitles)
    popen = Mock()
    monkeypatch.setattr(app, 'SYS_ENCODING', 'utf-8')
    monkeypatch.setattr(app, 'SUBTITLES_MULTI', p['multiple_subtitles'])
    monkeypatch.setattr(app, 'SUBTITLES_LANGUAGES', p['wanted_languages'])
    monkeypatch.setattr(app, 'SUBTITLES_PRE_SCRIPTS', p['pre_scripts'])
    monkeypatch.setattr(app, 'SUBTITLES_EXTRA_SCRIPTS', p['post_scripts'])
    monkeypatch.setattr(app, 'SUBTITLES_HEARING_IMPAIRED', p['hearing_impaired'])
    monkeypatch.setattr(sut, 'refine', refine)
    monkeypatch.setattr(sut, 'compute_score', compute_score)
    monkeypatch.setattr(ProviderPool, 'list_subtitles', list_subtitles)
    monkeypatch.setattr(ProviderPool, 'download_best_subtitles', download_best_subtitles)
    monkeypatch.setattr(subprocess, 'Popen', popen)

    # When
    actual = sut.download_subtitles(tv_episode=tvepisode, video_path=video_path,
                                    subtitles=external_subtitles, embedded_subtitles=embedded_subtitles)

    # Then
    assert p['expected'] == actual
    if p['expected']:
        assert len(p['pre_scripts']) + len(p['post_scripts']) * len(p['best_subtitles']) == popen.call_count
    if refine.called:
        assert embedded_subtitles == refine.call_args[1]['embedded_subtitles']
        assert tvepisode == refine.call_args[1]['tv_episode']