1
+ from __future__ import with_statement
1
2
import pytest
2
- from ss import FindMovieFiles
3
+ from mock import patch , MagicMock , call
4
+ from ss import FindMovieFiles , QueryOpenSubtitles , FindBestSubtitleMatches
3
5
4
6
5
7
#===================================================================================================
@@ -25,6 +27,72 @@ def testFindMovieFiles(tmpdir):
25
27
]
26
28
27
29
30
+ #===================================================================================================
31
+ # testQueryOpenSubtitles
32
+ #===================================================================================================
33
+ def testQueryOpenSubtitles (tmpdir ):
34
+ tmpdir .join ('movie1.avi' ).ensure ()
35
+ tmpdir .join ('movie2.avi' ).ensure ()
36
+ #
37
+ with patch ('xmlrpclib.Server' ) as rpc_mock :
38
+ with patch ('calculate_hash.CalculateHashForFile' ) as hash_mock :
39
+ hash_mock .return_value = 13
40
+ rpc_mock .return_value = server = MagicMock (name = 'MockServer' )
41
+ server .LogIn = MagicMock ()
42
+ server .LogIn .return_value = dict (token = 'TOKEN' )
43
+ server .SearchSubtitles = MagicMock ()
44
+ server .SearchSubtitles .return_value = dict (data = {'SubFileName' : 'movie.srt' })
45
+ server .LogOut = MagicMock ()
46
+
47
+ filenames = [str (tmpdir .join ('movie1.avi' )), str (tmpdir .join ('movie2.avi' ))]
48
+ search_results = QueryOpenSubtitles (filenames , 'en' )
49
+ server .LogIn .assert_called_once_with ('' , '' , 'en' , 'OS Test User Agent' )
50
+ calls = [
51
+ call ('TOKEN' , [dict (moviehash = 13 , moviebytesize = 0 , sublanguageid = 'en' ), dict (query = 'movie1' )]),
52
+ call ('TOKEN' , [dict (moviehash = 13 , moviebytesize = 0 , sublanguageid = 'en' ), dict (query = 'movie2' )]),
53
+ ]
54
+ server .SearchSubtitles .assert_has_calls (calls )
55
+ server .LogOut .assert_called_once_with ('TOKEN' )
56
+
57
+ assert search_results == {
58
+ str (tmpdir .join ('movie1.avi' )) : {'SubFileName' : 'movie.srt' },
59
+ str (tmpdir .join ('movie2.avi' )) : {'SubFileName' : 'movie.srt' },
60
+ }
61
+
62
+
63
+ #===================================================================================================
64
+ # testFindBestSubtitleMatches
65
+ #===================================================================================================
66
+ def testFindBestSubtitleMatches ():
67
+
68
+ with patch ('ss.QueryOpenSubtitles' ) as mock :
69
+ mock .return_value = {
70
+ 'Parks.and.Recreation.S05E13.HDTV.x264-LOL.avi' : [
71
+ dict (
72
+ SubFileName = 'Parks.and.Recreation.S05E13.HDTV.x264-LOL.srt' ,
73
+ SubDownloadsCnt = 1000 ,
74
+ SubDownloadLink = 'http://sub1.srt' ,
75
+ SubFormat = 'srt' ,
76
+ ),
77
+ dict (
78
+ SubFileName = 'Parks.and.Recreation.S05E13.HDTV.x264-LOL.srt' ,
79
+ SubDownloadsCnt = 1500 ,
80
+ SubDownloadLink = 'http://sub2.srt' ,
81
+ SubFormat = 'srt' ,
82
+ ),
83
+ dict (
84
+ SubFileName = 'Parks.and.Recreation.S05E13.HDTV.-LOL.srt' ,
85
+ SubDownloadsCnt = 3000 ,
86
+ SubDownloadLink = 'http://sub3.srt' ,
87
+ SubFormat = 'srt' ,
88
+ ),
89
+ ]
90
+ }
91
+
92
+ results = list (FindBestSubtitleMatches (['Parks.and.Recreation.S05E13.HDTV.x264-LOL.avi' ], 'en' ))
93
+ assert results == [('Parks.and.Recreation.S05E13.HDTV.x264-LOL.avi' , 'http://sub1.srt' , '.srt' )]
94
+
95
+
28
96
#===================================================================================================
29
97
# main
30
98
#===================================================================================================
0 commit comments