This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
zeyatest.py
executable file
·238 lines (224 loc) · 9.03 KB
/
zeyatest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009, 2010 Phil Sung
#
# This file is part of Zeya.
#
# Zeya is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Zeya is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Zeya. If not, see <http://www.gnu.org/licenses/>.
# Test suite for Zeya.
import StringIO
import os
import unittest
import backends
import decoders
import m3u
import options
import pls
import rhythmbox
class FakeTagpy():
"""
Fake object that can be a stand-in for the tagpy module, but which returns
a fixed tag object.
"""
def __init__(self, retval):
self.retval = retval
def FileRef(self, filename):
class FakeTag():
# Avoid shadowing the outer instance of 'self', so we can read from
# it.
def tag(inner_self):
return self.retval
return FakeTag()
class TagData():
"""
Tag object that holds metadata for a single song.
"""
def __init__(self, artist, title, album):
self.artist = artist
self.title = title
self.album = album
class CommonTest(unittest.TestCase):
def test_tokenization(self):
"""
Test that our tokenization method yields a correct ordering of songs.
"""
# Note, filename1 > filename2
filename1 = "/home/phil/9 - something.ogg"
filename2 = "/home/phil/10 - something.ogg"
t1 = rhythmbox.tokenize_filename(filename1)
t2 = rhythmbox.tokenize_filename(filename2)
self.assertTrue(t1 < t2)
class DecodersTest(unittest.TestCase):
def test_extensions(self):
"""
Test decoders.get_extension.
"""
self.assertEqual("mp3", decoders.get_extension("/path/to/SOMETHING.MP3"))
def test_is_decoder_registered(self):
"""
Test decoders.is_decoder_registered.
"""
self.assertTrue(decoders.is_decoder_registered("/path/to/something.mp3"))
self.assertFalse(decoders.is_decoder_registered("/path/to/something.xyz"))
def test_get_decoder(self):
"""
Test decoders.get_decoder
"""
self.assertTrue(decoders.get_decoder("/path/to/SOMETHING.MP3")[0]
.startswith("/usr/bin"))
class M3uTest(unittest.TestCase):
def test_parse_m3u(self):
playlist_data = """#EXTM3U
#EXTINF:,One
/home/phil/music/1 One.flac
#EXTINF:,Something
/home/phil/music/2 Two.flac"""
playlist = m3u.M3uPlaylist("/home/phil/foo.m3u",
StringIO.StringIO(playlist_data))
self.assertEqual(['/home/phil/music/1 One.flac', '/home/phil/music/2 Two.flac'],
playlist.get_filenames())
self.assertEqual(u'foo.m3u', playlist.get_title())
def test_parse_m3u_with_relative_paths(self):
playlist_data = "music/1 One.flac\nmusic/2 Two.flac"
playlist = m3u.M3uPlaylist("/home/phil/foo.m3u",
StringIO.StringIO(playlist_data))
self.assertEqual(['/home/phil/music/1 One.flac', '/home/phil/music/2 Two.flac'],
playlist.get_filenames())
class MetadataExtractionTest(unittest.TestCase):
def test_with_metadata(self):
tagpy = FakeTagpy(TagData(artist="Beatles", title="Ticket to Ride",
album="Help!"))
metadata = backends.extract_metadata("/dev/null", tagpy)
self.assertEqual("Ticket to Ride", metadata[backends.TITLE])
self.assertEqual("Beatles", metadata[backends.ARTIST])
self.assertEqual("Help!", metadata[backends.ALBUM])
def test_without_metadata(self):
tagpy = FakeTagpy(None)
metadata = backends.extract_metadata("/the/path/to/Song.flac", tagpy)
self.assertEqual("Song.flac", metadata[backends.TITLE])
self.assertEqual("", metadata[backends.ARTIST])
self.assertEqual("path/to", metadata[backends.ALBUM])
def test_short_path(self):
tagpy = FakeTagpy(None)
metadata = backends.extract_metadata("/music/Song.flac", tagpy)
self.assertEqual("music", metadata[backends.ALBUM])
def test_noalbum_path(self):
tagpy = FakeTagpy(TagData(artist="Beatles", title=None, album=None))
metadata = backends.extract_metadata("/music/Song.flac", tagpy)
self.assertEqual("", metadata[backends.ALBUM])
def test_decode_filename(self):
tagpy = FakeTagpy(None)
metadata = backends.extract_metadata("/path/to/\xe4\xb8\xad.flac", tagpy)
self.assertEqual(u"\u4e2d.flac", metadata[backends.TITLE])
def test_album_name_from_path_unicode(self):
value1 = backends.album_name_from_path(
TagData(artist="Beatles", title=None, album=None),
"/path/to/music")
self.assertTrue(type(value1) == unicode)
self.assertEqual(u'', value1)
value2 = backends.album_name_from_path(None, "/path/\xc3\x84/music")
self.assertEqual(u'path/\u00c4', value2)
value3 = backends.album_name_from_path(None, "/\xc3\x84/music")
self.assertEqual(u'\u00c4', value3)
class OptionsTest(unittest.TestCase):
def test_default(self):
params = options.get_options([])
self.assertFalse(params[0])
self.assertEqual('dir', params[1])
self.assertEqual(os.path.abspath('.'), params[5])
def test_get_help(self):
params = options.get_options(["--help"])
self.assertTrue(params[0])
def test_path(self):
params = options.get_options(["--path=/foo/bar"])
self.assertEqual('/foo/bar', params[5])
def test_backend(self):
params = options.get_options(["--backend=rhythmbox"])
self.assertEqual('rhythmbox', params[1])
def test_bad_backend(self):
try:
params = options.get_options(["--backend=invalid"])
self.fail("get_options should have raised BadArgsError")
except options.BadArgsError:
pass
def test_bitrate(self):
params = options.get_options(["-b128"])
self.assertEqual(128, params[2])
def test_bad_bitrate(self):
try:
params = options.get_options(["--bitrate=0"])
self.fail("get_options should have raised BadArgsError")
except options.BadArgsError:
pass
def test_port(self):
params = options.get_options(["-p9999"])
self.assertEqual(9999, params[4])
def test_bad_port(self):
try:
params = options.get_options(["--port=p"])
self.fail("get_options should have raised BadArgsError")
except options.BadArgsError:
pass
class PlsTest(unittest.TestCase):
def test_parse_pls(self):
playlist_data = """[playlist]
X-GNOME-Title=Foo
NumberOfEntries=2
File1=foo/bar/1 one.flac
Title1=One
File2=/var/bar/2 two.flac
Title2=Something"""
playlist = \
pls.PlsPlaylist("/home/phil/playlist.pls",
StringIO.StringIO(playlist_data))
self.assertEqual(['/home/phil/foo/bar/1 one.flac',
'/var/bar/2 two.flac'],
playlist.get_filenames())
self.assertEqual(u'Foo', playlist.get_title())
def test_parse_pls_with_file_url(self):
playlist_data = """[playlist]
X-GNOME-Title=Foo
NumberOfEntries=1
File1=file:///home/phil/music/Beach%20Boys,%20The/One.flac
Title1=One"""
playlist = \
pls.PlsPlaylist("/home/phil/playlist.pls",
StringIO.StringIO(playlist_data))
self.assertEqual(['/home/phil/music/Beach Boys, The/One.flac'],
playlist.get_filenames())
class RhythmboxTest(unittest.TestCase):
def test_read_library(self):
"""
Verify that the contents of the Rhythmbox XML library are read
correctly.
"""
backend = rhythmbox.RhythmboxBackend(open("testdata/rhythmbox.xml"))
library = backend.get_library_contents()
self.assertEqual(u'Help!', library[0]['album'])
self.assertEqual(u'The Beatles', library[0]['artist'])
self.assertEqual(u'Help!', library[0]['title'])
key1 = library[0]['key']
self.assertEqual('/tmp/Beatles, The/Help.flac',
backend.get_filename_from_key(key1))
# Test correct handling of songs and filenames with non-Latin
# characters.
self.assertEqual(u'', library[1]['album'])
self.assertEqual(u'', library[1]['artist'])
self.assertEqual(u'\u4e2d\u6587', library[1]['title'])
key2 = library[1]['key']
self.assertEqual(u'/tmp/\u4e2d\u6587.flac'.encode('UTF-8'),
backend.get_filename_from_key(key2))
if __name__ == "__main__":
unittest.main()