Skip to content

Commit 4df9aaf

Browse files
committed
added some tests for FindBestSubtitleMatches and QueryOpenSubtitles
1 parent 34ef791 commit 4df9aaf

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

ss.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import xmlrpclib
33
import difflib
44
import os
5-
from calculate_hash import CalculateHashForFile
5+
import calculate_hash
66
import gzip
77
import urllib
88
import tempfile
@@ -16,7 +16,6 @@
1616
def QueryOpenSubtitles(movie_filenames, language):
1717
uri = 'http://api.opensubtitles.org/xml-rpc'
1818
server = xmlrpclib.Server(uri, verbose=0, allow_none=True, use_datetime=True)
19-
2019
login_info = server.LogIn('', '', 'en', 'OS Test User Agent')
2120
token = login_info['token']
2221

@@ -26,7 +25,7 @@ def QueryOpenSubtitles(movie_filenames, language):
2625
for movie_filename in movie_filenames:
2726
search_queries = [
2827
dict(
29-
moviehash=CalculateHashForFile(movie_filename),
28+
moviehash=calculate_hash.CalculateHashForFile(movie_filename),
3029
moviebytesize=os.path.getsize(movie_filename),
3130
sublanguageid=language,
3231
),
@@ -36,6 +35,7 @@ def QueryOpenSubtitles(movie_filenames, language):
3635
]
3736

3837
response = server.SearchSubtitles(token, search_queries)
38+
print movie_filename, response
3939
search_results = response['data']
4040

4141
if search_results:

tests/test_ss.py

+69-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from __future__ import with_statement
12
import pytest
2-
from ss import FindMovieFiles
3+
from mock import patch, MagicMock, call
4+
from ss import FindMovieFiles, QueryOpenSubtitles, FindBestSubtitleMatches
35

46

57
#===================================================================================================
@@ -25,6 +27,72 @@ def testFindMovieFiles(tmpdir):
2527
]
2628

2729

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+
2896
#===================================================================================================
2997
# main
3098
#===================================================================================================

0 commit comments

Comments
 (0)