PageRenderTime 243ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/frontends/mpd/serializer_test.py

https://github.com/lann/mopidy
Python | 353 lines | 346 code | 7 blank | 0 comment | 0 complexity | 0bbf5917b92d3b307091886a423db755 MD5 | raw file
  1. import datetime
  2. import os
  3. from mopidy import settings
  4. from mopidy.utils.path import mtime, uri_to_path
  5. from mopidy.frontends.mpd import translator, protocol
  6. from mopidy.models import Album, Artist, Playlist, Track
  7. from tests import unittest
  8. class TrackMpdFormatTest(unittest.TestCase):
  9. track = Track(
  10. uri=u'a uri',
  11. artists=[Artist(name=u'an artist')],
  12. name=u'a name',
  13. album=Album(name=u'an album', num_tracks=13,
  14. artists=[Artist(name=u'an other artist')]),
  15. track_no=7,
  16. date=datetime.date(1977, 1, 1),
  17. length=137000,
  18. )
  19. def setUp(self):
  20. settings.LOCAL_MUSIC_PATH = '/dir/subdir'
  21. mtime.set_fake_time(1234567)
  22. def tearDown(self):
  23. settings.runtime.clear()
  24. mtime.undo_fake()
  25. def test_track_to_mpd_format_for_empty_track(self):
  26. result = translator.track_to_mpd_format(Track())
  27. self.assert_(('file', '') in result)
  28. self.assert_(('Time', 0) in result)
  29. self.assert_(('Artist', '') in result)
  30. self.assert_(('Title', '') in result)
  31. self.assert_(('Album', '') in result)
  32. self.assert_(('Track', 0) in result)
  33. self.assert_(('Date', '') in result)
  34. self.assertEqual(len(result), 7)
  35. def test_track_to_mpd_format_with_position(self):
  36. result = translator.track_to_mpd_format(Track(), position=1)
  37. self.assert_(('Pos', 1) not in result)
  38. def test_track_to_mpd_format_with_cpid(self):
  39. result = translator.track_to_mpd_format(Track(), cpid=1)
  40. self.assert_(('Id', 1) not in result)
  41. def test_track_to_mpd_format_with_position_and_cpid(self):
  42. result = translator.track_to_mpd_format(Track(), position=1, cpid=2)
  43. self.assert_(('Pos', 1) in result)
  44. self.assert_(('Id', 2) in result)
  45. def test_track_to_mpd_format_for_nonempty_track(self):
  46. result = translator.track_to_mpd_format(
  47. self.track, position=9, cpid=122)
  48. self.assert_(('file', 'a uri') in result)
  49. self.assert_(('Time', 137) in result)
  50. self.assert_(('Artist', 'an artist') in result)
  51. self.assert_(('Title', 'a name') in result)
  52. self.assert_(('Album', 'an album') in result)
  53. self.assert_(('AlbumArtist', 'an other artist') in result)
  54. self.assert_(('Track', '7/13') in result)
  55. self.assert_(('Date', datetime.date(1977, 1, 1)) in result)
  56. self.assert_(('Pos', 9) in result)
  57. self.assert_(('Id', 122) in result)
  58. self.assertEqual(len(result), 10)
  59. def test_track_to_mpd_format_musicbrainz_trackid(self):
  60. track = self.track.copy(musicbrainz_id='foo')
  61. result = translator.track_to_mpd_format(track)
  62. self.assert_(('MUSICBRAINZ_TRACKID', 'foo') in result)
  63. def test_track_to_mpd_format_musicbrainz_albumid(self):
  64. album = self.track.album.copy(musicbrainz_id='foo')
  65. track = self.track.copy(album=album)
  66. result = translator.track_to_mpd_format(track)
  67. self.assert_(('MUSICBRAINZ_ALBUMID', 'foo') in result)
  68. def test_track_to_mpd_format_musicbrainz_albumid(self):
  69. artist = list(self.track.artists)[0].copy(musicbrainz_id='foo')
  70. album = self.track.album.copy(artists=[artist])
  71. track = self.track.copy(album=album)
  72. result = translator.track_to_mpd_format(track)
  73. self.assert_(('MUSICBRAINZ_ALBUMARTISTID', 'foo') in result)
  74. def test_track_to_mpd_format_musicbrainz_artistid(self):
  75. artist = list(self.track.artists)[0].copy(musicbrainz_id='foo')
  76. track = self.track.copy(artists=[artist])
  77. result = translator.track_to_mpd_format(track)
  78. self.assert_(('MUSICBRAINZ_ARTISTID', 'foo') in result)
  79. def test_artists_to_mpd_format(self):
  80. artists = [Artist(name=u'ABBA'), Artist(name=u'Beatles')]
  81. translated = translator.artists_to_mpd_format(artists)
  82. self.assertEqual(translated, u'ABBA, Beatles')
  83. def test_artists_to_mpd_format_artist_with_no_name(self):
  84. artists = [Artist(name=None)]
  85. translated = translator.artists_to_mpd_format(artists)
  86. self.assertEqual(translated, u'')
  87. class PlaylistMpdFormatTest(unittest.TestCase):
  88. def test_mpd_format(self):
  89. playlist = Playlist(tracks=[
  90. Track(track_no=1), Track(track_no=2), Track(track_no=3)])
  91. result = translator.playlist_to_mpd_format(playlist)
  92. self.assertEqual(len(result), 3)
  93. def test_mpd_format_with_range(self):
  94. playlist = Playlist(tracks=[
  95. Track(track_no=1), Track(track_no=2), Track(track_no=3)])
  96. result = translator.playlist_to_mpd_format(playlist, 1, 2)
  97. self.assertEqual(len(result), 1)
  98. self.assertEqual(dict(result[0])['Track'], 2)
  99. class TracksToTagCacheFormatTest(unittest.TestCase):
  100. def setUp(self):
  101. settings.LOCAL_MUSIC_PATH = '/dir/subdir'
  102. mtime.set_fake_time(1234567)
  103. def tearDown(self):
  104. settings.runtime.clear()
  105. mtime.undo_fake()
  106. def translate(self, track):
  107. folder = settings.LOCAL_MUSIC_PATH
  108. result = dict(translator.track_to_mpd_format(track))
  109. result['file'] = uri_to_path(result['file'])
  110. result['file'] = result['file'][len(folder)+1:]
  111. result['key'] = os.path.basename(result['file'])
  112. result['mtime'] = mtime('')
  113. return translator.order_mpd_track_info(result.items())
  114. def consume_headers(self, result):
  115. self.assertEqual(('info_begin',), result[0])
  116. self.assertEqual(('mpd_version', protocol.VERSION), result[1])
  117. self.assertEqual(('fs_charset', protocol.ENCODING), result[2])
  118. self.assertEqual(('info_end',), result[3])
  119. return result[4:]
  120. def consume_song_list(self, result):
  121. self.assertEqual(('songList begin',), result[0])
  122. for i, row in enumerate(result):
  123. if row == ('songList end',):
  124. return result[1:i], result[i+1:]
  125. self.fail("Couldn't find songList end in result")
  126. def consume_directory(self, result):
  127. self.assertEqual('directory', result[0][0])
  128. self.assertEqual(('mtime', mtime('.')), result[1])
  129. self.assertEqual(('begin', os.path.split(result[0][1])[1]), result[2])
  130. directory = result[2][1]
  131. for i, row in enumerate(result):
  132. if row == ('end', directory):
  133. return result[3:i], result[i+1:]
  134. self.fail("Couldn't find end %s in result" % directory)
  135. def test_empty_tag_cache_has_header(self):
  136. result = translator.tracks_to_tag_cache_format([])
  137. result = self.consume_headers(result)
  138. def test_empty_tag_cache_has_song_list(self):
  139. result = translator.tracks_to_tag_cache_format([])
  140. result = self.consume_headers(result)
  141. song_list, result = self.consume_song_list(result)
  142. self.assertEqual(len(song_list), 0)
  143. self.assertEqual(len(result), 0)
  144. def test_tag_cache_has_header(self):
  145. track = Track(uri='file:///dir/subdir/song.mp3')
  146. result = translator.tracks_to_tag_cache_format([track])
  147. result = self.consume_headers(result)
  148. def test_tag_cache_has_song_list(self):
  149. track = Track(uri='file:///dir/subdir/song.mp3')
  150. result = translator.tracks_to_tag_cache_format([track])
  151. result = self.consume_headers(result)
  152. song_list, result = self.consume_song_list(result)
  153. self.assert_(song_list)
  154. self.assertEqual(len(result), 0)
  155. def test_tag_cache_has_formated_track(self):
  156. track = Track(uri='file:///dir/subdir/song.mp3')
  157. formated = self.translate(track)
  158. result = translator.tracks_to_tag_cache_format([track])
  159. result = self.consume_headers(result)
  160. song_list, result = self.consume_song_list(result)
  161. self.assertEqual(song_list, formated)
  162. self.assertEqual(len(result), 0)
  163. def test_tag_cache_has_formated_track_with_key_and_mtime(self):
  164. track = Track(uri='file:///dir/subdir/song.mp3')
  165. formated = self.translate(track)
  166. result = translator.tracks_to_tag_cache_format([track])
  167. result = self.consume_headers(result)
  168. song_list, result = self.consume_song_list(result)
  169. self.assertEqual(song_list, formated)
  170. self.assertEqual(len(result), 0)
  171. def test_tag_cache_suports_directories(self):
  172. track = Track(uri='file:///dir/subdir/folder/song.mp3')
  173. formated = self.translate(track)
  174. result = translator.tracks_to_tag_cache_format([track])
  175. result = self.consume_headers(result)
  176. folder, result = self.consume_directory(result)
  177. song_list, result = self.consume_song_list(result)
  178. self.assertEqual(len(song_list), 0)
  179. self.assertEqual(len(result), 0)
  180. song_list, result = self.consume_song_list(folder)
  181. self.assertEqual(len(result), 0)
  182. self.assertEqual(song_list, formated)
  183. def test_tag_cache_diretory_header_is_right(self):
  184. track = Track(uri='file:///dir/subdir/folder/sub/song.mp3')
  185. result = translator.tracks_to_tag_cache_format([track])
  186. result = self.consume_headers(result)
  187. folder, result = self.consume_directory(result)
  188. self.assertEqual(('directory', 'folder/sub'), folder[0])
  189. self.assertEqual(('mtime', mtime('.')), folder[1])
  190. self.assertEqual(('begin', 'sub'), folder[2])
  191. def test_tag_cache_suports_sub_directories(self):
  192. track = Track(uri='file:///dir/subdir/folder/sub/song.mp3')
  193. formated = self.translate(track)
  194. result = translator.tracks_to_tag_cache_format([track])
  195. result = self.consume_headers(result)
  196. folder, result = self.consume_directory(result)
  197. song_list, result = self.consume_song_list(result)
  198. self.assertEqual(len(song_list), 0)
  199. self.assertEqual(len(result), 0)
  200. folder, result = self.consume_directory(folder)
  201. song_list, result = self.consume_song_list(result)
  202. self.assertEqual(len(result), 0)
  203. self.assertEqual(len(song_list), 0)
  204. song_list, result = self.consume_song_list(folder)
  205. self.assertEqual(len(result), 0)
  206. self.assertEqual(song_list, formated)
  207. def test_tag_cache_supports_multiple_tracks(self):
  208. tracks = [
  209. Track(uri='file:///dir/subdir/song1.mp3'),
  210. Track(uri='file:///dir/subdir/song2.mp3'),
  211. ]
  212. formated = []
  213. formated.extend(self.translate(tracks[0]))
  214. formated.extend(self.translate(tracks[1]))
  215. result = translator.tracks_to_tag_cache_format(tracks)
  216. result = self.consume_headers(result)
  217. song_list, result = self.consume_song_list(result)
  218. self.assertEqual(song_list, formated)
  219. self.assertEqual(len(result), 0)
  220. def test_tag_cache_supports_multiple_tracks_in_dirs(self):
  221. tracks = [
  222. Track(uri='file:///dir/subdir/song1.mp3'),
  223. Track(uri='file:///dir/subdir/folder/song2.mp3'),
  224. ]
  225. formated = []
  226. formated.append(self.translate(tracks[0]))
  227. formated.append(self.translate(tracks[1]))
  228. result = translator.tracks_to_tag_cache_format(tracks)
  229. result = self.consume_headers(result)
  230. folder, result = self.consume_directory(result)
  231. song_list, song_result = self.consume_song_list(folder)
  232. self.assertEqual(song_list, formated[1])
  233. self.assertEqual(len(song_result), 0)
  234. song_list, result = self.consume_song_list(result)
  235. self.assertEqual(len(result), 0)
  236. self.assertEqual(song_list, formated[0])
  237. class TracksToDirectoryTreeTest(unittest.TestCase):
  238. def setUp(self):
  239. settings.LOCAL_MUSIC_PATH = '/root/'
  240. def tearDown(self):
  241. settings.runtime.clear()
  242. def test_no_tracks_gives_emtpy_tree(self):
  243. tree = translator.tracks_to_directory_tree([])
  244. self.assertEqual(tree, ({}, []))
  245. def test_top_level_files(self):
  246. tracks = [
  247. Track(uri='file:///root/file1.mp3'),
  248. Track(uri='file:///root/file2.mp3'),
  249. Track(uri='file:///root/file3.mp3'),
  250. ]
  251. tree = translator.tracks_to_directory_tree(tracks)
  252. self.assertEqual(tree, ({}, tracks))
  253. def test_single_file_in_subdir(self):
  254. tracks = [Track(uri='file:///root/dir/file1.mp3')]
  255. tree = translator.tracks_to_directory_tree(tracks)
  256. expected = ({'dir': ({}, tracks)}, [])
  257. self.assertEqual(tree, expected)
  258. def test_single_file_in_sub_subdir(self):
  259. tracks = [Track(uri='file:///root/dir1/dir2/file1.mp3')]
  260. tree = translator.tracks_to_directory_tree(tracks)
  261. expected = ({'dir1': ({'dir1/dir2': ({}, tracks)}, [])}, [])
  262. self.assertEqual(tree, expected)
  263. def test_complex_file_structure(self):
  264. tracks = [
  265. Track(uri='file:///root/file1.mp3'),
  266. Track(uri='file:///root/dir1/file2.mp3'),
  267. Track(uri='file:///root/dir1/file3.mp3'),
  268. Track(uri='file:///root/dir2/file4.mp3'),
  269. Track(uri='file:///root/dir2/sub/file5.mp3'),
  270. ]
  271. tree = translator.tracks_to_directory_tree(tracks)
  272. expected = (
  273. {
  274. 'dir1': ({}, [tracks[1], tracks[2]]),
  275. 'dir2': (
  276. {
  277. 'dir2/sub': ({}, [tracks[4]])
  278. },
  279. [tracks[3]]
  280. ),
  281. },
  282. [tracks[0]]
  283. )
  284. self.assertEqual(tree, expected)