<s> from datetime import datetime , timedelta <EOL> import io <EOL> import os <EOL> from babelfish import Language <EOL> import pytest <EOL> from six import text_type as str <EOL> try : <EOL> from unittest . mock import Mock <EOL> except ImportError : <EOL> from mock import Mock <EOL> from subliminal . video import ( Episode , Movie , Video , hash_opensubtitles , hash_thesubdb , scan_video , scan_videos , <EOL> search_external_subtitles ) <EOL> def timestamp ( date ) : <EOL> """<STR_LIT>""" <EOL> return ( date - datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ) . total_seconds ( ) <EOL> def test_video_exists_age ( movies , tmpdir , monkeypatch ) : <EOL> monkeypatch . chdir ( str ( tmpdir ) ) <EOL> video = movies [ '<STR_LIT>' ] <EOL> tmpdir . ensure ( video . name ) . setmtime ( timestamp ( datetime . utcnow ( ) - timedelta ( days = <NUM_LIT:3> ) ) ) <EOL> assert video . exists <EOL> assert timedelta ( days = <NUM_LIT:3> ) < video . age < timedelta ( days = <NUM_LIT:3> , seconds = <NUM_LIT:1> ) <EOL> def test_video_age ( movies ) : <EOL> assert movies [ '<STR_LIT>' ] . age == timedelta ( ) <EOL> def test_video_fromguess_episode ( episodes , monkeypatch ) : <EOL> guess = { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> monkeypatch . setattr ( Episode , '<STR_LIT>' , Mock ( ) ) <EOL> Video . fromguess ( episodes [ '<STR_LIT>' ] . name , guess ) <EOL> assert Episode . fromguess . called <EOL> def test_video_fromguess_movie ( movies , monkeypatch ) : <EOL> guess = { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> monkeypatch . setattr ( Movie , '<STR_LIT>' , Mock ( ) ) <EOL> Video . fromguess ( movies [ '<STR_LIT>' ] . name , guess ) <EOL> assert Movie . fromguess . called <EOL> def test_video_fromguess_wrong_type ( episodes ) : <EOL> guess = { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> Video . fromguess ( episodes [ '<STR_LIT>' ] . name , guess ) <EOL> assert str ( excinfo . value ) == '<STR_LIT>' <EOL> def test_video_fromname_movie ( movies ) : <EOL> video = Video . fromname ( movies [ '<STR_LIT>' ] . name ) <EOL> assert type ( video ) is Movie <EOL> assert video . name == movies [ '<STR_LIT>' ] . name <EOL> assert video . format == movies [ '<STR_LIT>' ] . format <EOL> assert video . release_group == movies [ '<STR_LIT>' ] . release_group <EOL> assert video . resolution == movies [ '<STR_LIT>' ] . resolution <EOL> assert video . video_codec == movies [ '<STR_LIT>' ] . video_codec <EOL> assert video . audio_codec is None <EOL> assert video . imdb_id is None <EOL> assert video . hashes == { } <EOL> assert video . size is None <EOL> assert video . subtitle_languages == set ( ) <EOL> assert video . title == movies [ '<STR_LIT>' ] . title <EOL> assert video . year == movies [ '<STR_LIT>' ] . year <EOL> def test_video_fromname_episode ( episodes ) : <EOL> video = Video . fromname ( episodes [ '<STR_LIT>' ] . name ) <EOL> assert type ( video ) is Episode <EOL> assert video . name == episodes [ '<STR_LIT>' ] . name <EOL> assert video . format == episodes [ '<STR_LIT>' ] . format <EOL> assert video . release_group == episodes [ '<STR_LIT>' ] . release_group <EOL> assert video . resolution == episodes [ '<STR_LIT>' ] . resolution <EOL> assert video . video_codec == episodes [ '<STR_LIT>' ] . video_codec <EOL> assert video . audio_codec is None <EOL> assert video . imdb_id is None <EOL> assert video . hashes == { } <EOL> assert video . size is None <EOL> assert video . subtitle_languages == set ( ) <EOL> assert video . series == episodes [ '<STR_LIT>' ] . series <EOL> assert video . season == episodes [ '<STR_LIT>' ] . season <EOL> assert video . episode == episodes [ '<STR_LIT>' ] . episode <EOL> assert video . title is None <EOL> assert video . year is None <EOL> assert video . tvdb_id is None <EOL> def test_video_hash ( episodes ) : <EOL> video = episodes [ '<STR_LIT>' ] <EOL> assert hash ( video ) == hash ( video . name ) <EOL> def test_episode_fromguess_wrong_type ( episodes ) : <EOL> guess = { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> Episode . fromguess ( episodes [ '<STR_LIT>' ] . name , guess ) <EOL> assert str ( excinfo . value ) == '<STR_LIT>' <EOL> def test_episode_fromguess_insufficient_data ( episodes ) : <EOL> guess = { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> Episode . fromguess ( episodes [ '<STR_LIT>' ] . name , guess ) <EOL> assert str ( excinfo . value ) == '<STR_LIT>' <EOL> def test_movie_fromguess_wrong_type ( movies ) : <EOL> guess = { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> Movie . fromguess ( movies [ '<STR_LIT>' ] . name , guess ) <EOL> assert str ( excinfo . value ) == '<STR_LIT>' <EOL> def test_movie_fromguess_insufficient_data ( movies ) : <EOL> guess = { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> Movie . fromguess ( movies [ '<STR_LIT>' ] . name , guess ) <EOL> assert str ( excinfo . value ) == '<STR_LIT>' <EOL> def test_movie_fromname ( movies ) : <EOL> video = Movie . fromname ( movies [ '<STR_LIT>' ] . name ) <EOL> assert video . name == movies [ '<STR_LIT>' ] . name <EOL> assert video . format == movies [ '<STR_LIT>' ] . format <EOL> assert video . release_group == movies [ '<STR_LIT>' ] . release_group <EOL> assert video . resolution == movies [ '<STR_LIT>' ] . resolution <EOL> assert video . video_codec == movies [ '<STR_LIT>' ] . video_codec <EOL> assert video . audio_codec is None <EOL> assert video . imdb_id is None <EOL> assert video . hashes == { } <EOL> assert video . size is None <EOL> assert video . subtitle_languages == set ( ) <EOL> assert video . title == movies [ '<STR_LIT>' ] . title <EOL> assert video . year == movies [ '<STR_LIT>' ] . year <EOL> def test_episode_fromname ( episodes ) : <EOL> video = Episode . fromname ( episodes [ '<STR_LIT>' ] . name ) <EOL> assert video . name == episodes [ '<STR_LIT>' ] . name <EOL> assert video . format == episodes [ '<STR_LIT>' ] . format <EOL> assert video . release_group == episodes [ '<STR_LIT>' ] . release_group <EOL> assert video . resolution == episodes [ '<STR_LIT>' ] . resolution <EOL> assert video . video_codec == episodes [ '<STR_LIT>' ] . video_codec <EOL> assert video . audio_codec is None <EOL> assert video . imdb_id is None <EOL> assert video . hashes == { } <EOL> assert video . size is None <EOL> assert video . subtitle_languages == set ( ) <EOL> assert video . series == episodes [ '<STR_LIT>' ] . series <EOL> assert video . season == episodes [ '<STR_LIT>' ] . season <EOL> assert video . episode == episodes [ '<STR_LIT>' ] . episode <EOL> assert video . title is None <EOL> assert video . year is None <EOL> assert video . tvdb_id is None <EOL> def test_search_external_subtitles ( episodes , tmpdir ) : <EOL> video_name = os . path . split ( episodes [ '<STR_LIT>' ] . name ) [ <NUM_LIT:1> ] <EOL> video_root = os . path . splitext ( video_name ) [ <NUM_LIT:0> ] <EOL> video_path = str ( tmpdir . ensure ( video_name ) ) <EOL> expected_subtitles = { <EOL> video_name + '<STR_LIT>' : Language ( '<STR_LIT>' ) , <EOL> video_root + '<STR_LIT>' : Language ( '<STR_LIT>' ) , <EOL> video_root + '<STR_LIT>' : Language ( '<STR_LIT>' ) , <EOL> video_name + '<STR_LIT>' : Language ( '<STR_LIT>' ) , <EOL> video_root + '<STR_LIT>' : Language ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> video_name + '<STR_LIT>' : Language ( '<STR_LIT>' , script = '<STR_LIT>' ) , <EOL> video_name + '<STR_LIT>' : Language ( '<STR_LIT>' ) <EOL> } <EOL> tmpdir . ensure ( os . path . split ( episodes [ '<STR_LIT>' ] . name ) [ <NUM_LIT:1> ] + '<STR_LIT>' ) <EOL> for path in expected_subtitles : <EOL> tmpdir . ensure ( path ) <EOL> subtitles = search_external_subtitles ( video_path ) <EOL> assert subtitles == expected_subtitles <EOL> def test_search_external_subtitles_no_directory ( movies , tmpdir , monkeypatch ) : <EOL> video_name = os . path . split ( movies [ '<STR_LIT>' ] . name ) [ <NUM_LIT:1> ] <EOL> video_root = os . path . splitext ( video_name ) [ <NUM_LIT:0> ] <EOL> tmpdir . ensure ( video_name ) <EOL> monkeypatch . chdir ( str ( tmpdir ) ) <EOL> expected_subtitles = { <EOL> video_name + '<STR_LIT>' : Language ( '<STR_LIT>' ) , <EOL> video_root + '<STR_LIT>' : Language ( '<STR_LIT>' ) <EOL> } <EOL> for path in expected_subtitles : <EOL> tmpdir . ensure ( path ) <EOL> subtitles = search_external_subtitles ( video_name ) <EOL> assert subtitles == expected_subtitles <EOL> def test_search_external_subtitles_in_directory ( episodes , tmpdir , monkeypatch ) : <EOL> video_name = episodes [ '<STR_LIT>' ] . name <EOL> video_root = os . path . splitext ( video_name ) [ <NUM_LIT:0> ] <EOL> tmpdir . ensure ( '<STR_LIT>' , video_name ) <EOL> subtitles_directory = str ( tmpdir . ensure ( '<STR_LIT>' , dir = True ) ) <EOL> expected_subtitles = { <EOL> video_name + '<STR_LIT>' : Language ( '<STR_LIT>' ) , <EOL> video_root + '<STR_LIT>' : Language ( '<STR_LIT>' ) <EOL> } <EOL> tmpdir . ensure ( '<STR_LIT>' , video_name + '<STR_LIT>' ) <EOL> for path in expected_subtitles : <EOL> tmpdir . ensure ( '<STR_LIT>' , path ) <EOL> subtitles = search_external_subtitles ( video_name , directory = subtitles_directory ) <EOL> assert subtitles == expected_subtitles <EOL> def test_scan_video_movie ( movies , tmpdir , monkeypatch ) : <EOL> video = movies [ '<STR_LIT>' ] <EOL> monkeypatch . chdir ( str ( tmpdir ) ) <EOL> tmpdir . ensure ( video . name ) <EOL> scanned_video = scan_video ( video . name ) <EOL> assert scanned_video . name == video . name <EOL> assert scanned_video . format == video . format <EOL> assert scanned_video . release_group == video . release_group <EOL> assert scanned_video . resolution == video . resolution <EOL> assert scanned_video . video_codec == video . video_codec <EOL> assert scanned_video . audio_codec is None <EOL> assert scanned_video . imdb_id is None <EOL> assert scanned_video . hashes == { } <EOL> assert scanned_video . size == <NUM_LIT:0> <EOL> assert scanned_video . subtitle_languages == set ( ) <EOL> assert scanned_video . title == video . title <EOL> assert scanned_video . year == video . year <EOL> def test_scan_video_episode ( episodes , tmpdir , monkeypatch ) : <EOL> video = episodes [ '<STR_LIT>' ] <EOL> monkeypatch . chdir ( str ( tmpdir ) ) <EOL> tmpdir . ensure ( video . name ) <EOL> scanned_video = scan_video ( video . name ) <EOL> assert scanned_video . name , video . name <EOL> assert scanned_video . format == video . format <EOL> assert scanned_video . release_group == video . release_group <EOL> assert scanned_video . resolution == video . resolution <EOL> assert scanned_video . video_codec == video . video_codec <EOL> assert scanned_video . audio_codec is None <EOL> assert scanned_video . imdb_id is None <EOL> assert scanned_video . hashes == { } <EOL> assert scanned_video . size == <NUM_LIT:0> <EOL> assert scanned_video . subtitle_languages == set ( ) <EOL> assert scanned_video . series == video . series <EOL> assert scanned_video . season == video . season <EOL> assert scanned_video . episode == video . episode <EOL> assert scanned_video . title is None <EOL> assert scanned_video . year is None <EOL> assert scanned_video . tvdb_id is None <EOL> def test_scan_video_metadata ( mkv ) : <EOL> scanned_video = scan_video ( mkv [ '<STR_LIT>' ] ) <EOL> assert type ( scanned_video ) is Movie <EOL> assert scanned_video . name == mkv [ '<STR_LIT>' ] <EOL> assert scanned_video . format is None <EOL> assert scanned_video . release_group is None <EOL> assert scanned_video . resolution is None <EOL> assert scanned_video . video_codec == '<STR_LIT>' <EOL> assert scanned_video . audio_codec == '<STR_LIT>' <EOL> assert scanned_video . imdb_id is None <EOL> assert scanned_video . hashes == { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> assert scanned_video . size == <NUM_LIT> <EOL> assert scanned_video . subtitle_languages == { Language ( '<STR_LIT>' ) , Language ( '<STR_LIT>' ) , Language ( '<STR_LIT>' ) , Language ( '<STR_LIT>' ) , <EOL> Language ( '<STR_LIT>' ) , Language ( '<STR_LIT>' ) , Language ( '<STR_LIT>' ) } <EOL> assert scanned_video . title == '<STR_LIT>' <EOL> assert scanned_video . year is None <EOL> def test_scan_video_path_does_not_exist ( movies ) : <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> scan_video ( movies [ '<STR_LIT>' ] . name ) <EOL> assert str ( excinfo . value ) == '<STR_LIT>' <EOL> def test_scan_video_invalid_extension ( movies , tmpdir , monkeypatch ) : <EOL> monkeypatch . chdir ( str ( tmpdir ) ) <EOL> movie_name = os . path . splitext ( movies [ '<STR_LIT>' ] . name ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> tmpdir . ensure ( movie_name ) <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> scan_video ( movie_name ) <EOL> assert str ( excinfo . value ) == '<STR_LIT>' <EOL> def test_scan_video_broken ( mkv , tmpdir , monkeypatch ) : <EOL> broken_path = '<STR_LIT>' <EOL> with io . open ( mkv [ '<STR_LIT>' ] , '<STR_LIT:rb>' ) as original : <EOL> with tmpdir . join ( broken_path ) . open ( '<STR_LIT:wb>' ) as broken : <EOL> broken . write ( original . read ( <NUM_LIT> ) ) <EOL> monkeypatch . chdir ( str ( tmpdir ) ) <EOL> scanned_video = scan_video ( broken_path ) <EOL> assert type ( scanned_video ) is Movie <EOL> assert scanned_video . name == str ( broken_path ) <EOL> assert scanned_video . format is None <EOL> assert scanned_video . release_group is None <EOL> assert scanned_video . resolution is None <EOL> assert scanned_video . video_codec is None <EOL> assert scanned_video . audio_codec is None <EOL> assert scanned_video . imdb_id is None <EOL> assert scanned_video . hashes == { } <EOL> assert scanned_video . size == <NUM_LIT> <EOL> assert scanned_video . subtitle_languages == set ( ) <EOL> assert scanned_video . title == '<STR_LIT>' <EOL> assert scanned_video . year is None <EOL> def test_scan_videos_path_does_not_exist ( movies ) : <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> scan_videos ( movies [ '<STR_LIT>' ] . name ) <EOL> assert str ( excinfo . value ) == '<STR_LIT>' <EOL> def test_scan_videos_path_is_not_a_directory ( movies , tmpdir , monkeypatch ) : <EOL> monkeypatch . chdir ( str ( tmpdir ) ) <EOL> tmpdir . ensure ( movies [ '<STR_LIT>' ] . name ) <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> scan_videos ( movies [ '<STR_LIT>' ] . name ) <EOL> assert str ( excinfo . value ) == '<STR_LIT>' <EOL> def test_scan_videos ( movies , tmpdir , monkeypatch ) : <EOL> man_of_steel = tmpdir . ensure ( '<STR_LIT>' , movies [ '<STR_LIT>' ] . name ) <EOL> tmpdir . ensure ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> tmpdir . ensure ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> tmpdir . ensure ( '<STR_LIT>' , movies [ '<STR_LIT>' ] . name ) <EOL> tmpdir . ensure ( '<STR_LIT>' , os . path . splitext ( movies [ '<STR_LIT>' ] . name ) [ <NUM_LIT:0> ] + '<STR_LIT>' ) <EOL> tmpdir . ensure ( '<STR_LIT>' , '<STR_LIT>' , dir = True ) <EOL> tmpdir . join ( '<STR_LIT>' , '<STR_LIT>' , os . path . split ( movies [ '<STR_LIT>' ] . name ) [ <NUM_LIT:1> ] ) . mksymlinkto ( man_of_steel ) <EOL> mock_scan_video = Mock ( ) <EOL> monkeypatch . setattr ( '<STR_LIT>' , mock_scan_video ) <EOL> monkeypatch . chdir ( str ( tmpdir ) ) <EOL> videos = scan_videos ( '<STR_LIT>' ) <EOL> kwargs = dict ( subtitles = True , embedded_subtitles = True , subtitles_dir = None ) <EOL> calls = [ ( ( os . path . join ( '<STR_LIT>' , movies [ '<STR_LIT>' ] . name ) , ) , kwargs ) , <EOL> ( ( os . path . join ( '<STR_LIT>' , movies [ '<STR_LIT>' ] . name ) , ) , kwargs ) ] <EOL> assert len ( videos ) == len ( calls ) <EOL> assert mock_scan_video . call_count == len ( calls ) <EOL> mock_scan_video . assert_has_calls ( calls , any_order = True ) <EOL> def test_hash_opensubtitles ( mkv ) : <EOL> assert hash_opensubtitles ( mkv [ '<STR_LIT>' ] ) == '<STR_LIT>' <EOL> def test_hash_opensubtitles_too_small ( tmpdir ) : <EOL> path = tmpdir . ensure ( '<STR_LIT>' ) <EOL> assert hash_opensubtitles ( str ( path ) ) is None <EOL> def test_hash_thesubdb ( mkv ) : <EOL> assert hash_thesubdb ( mkv [ '<STR_LIT>' ] ) == '<STR_LIT>' <EOL> def test_hash_thesubdb_too_small ( tmpdir ) : <EOL> path = tmpdir . ensure ( '<STR_LIT>' ) <EOL> assert hash_thesubdb ( str ( path ) ) is None </s>
