test_track.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. import datetime as dt
  2. import os
  3. import re
  4. import shlex
  5. import stat
  6. import subprocess
  7. import unittest.mock
  8. import mutagen
  9. import pytest
  10. import symuid
  11. from symuid._datetime import datetime_utc_now
  12. # pylint: disable=redefined-outer-name,protected-access
  13. # TODO test aac / m4a itunes tags
  14. TRACKS_DIR_PATH = os.path.join(os.path.dirname(__file__), "tracks")
  15. def utc_dt(hour=0):
  16. return dt.datetime(2018, 9, 26, hour, tzinfo=dt.timezone.utc)
  17. @pytest.fixture
  18. def empty_id3_track(empty_id3_path):
  19. return symuid.Track(empty_id3_path)
  20. @pytest.mark.parametrize(
  21. "track_name",
  22. [
  23. "id3v2.4-typical.mp3",
  24. "id3v2.4-empty.mp3",
  25. "ogg-opus-typical.opus",
  26. "ogg-vorbis-typical.ogg",
  27. ],
  28. )
  29. def test_init(tracks_dir_path, track_name):
  30. symuid.Track(os.path.join(tracks_dir_path, track_name))
  31. @pytest.mark.parametrize(
  32. ("track_name", "expected_comment"),
  33. [
  34. ("id3v2.4-typical.mp3", "some comment"),
  35. ("id3v2.4-empty.mp3", None),
  36. ("ogg-opus-typical.opus", "some comment"),
  37. ("ogg-vorbis-typical.ogg", "some further information"),
  38. ],
  39. )
  40. def test_get_comment(tracks_dir_path, track_name, expected_comment):
  41. track = symuid.Track(os.path.join(tracks_dir_path, track_name))
  42. assert expected_comment == track.comment
  43. def test_set_comment(empty_id3_track):
  44. assert empty_id3_track.comment is None
  45. empty_id3_track.comment = "note"
  46. assert empty_id3_track.comment == "note"
  47. empty_id3_track.comment += "s"
  48. assert empty_id3_track.comment == "notes"
  49. assert symuid.Track(empty_id3_track.path).comment == "notes"
  50. @pytest.mark.parametrize(
  51. ("play_count"), [symuid.PlayCount("pytest", "lib", utc_dt(), 7),]
  52. )
  53. def test_register_play_count_id3(empty_id3_track, play_count):
  54. empty_id3_track.register_play_count(play_count)
  55. tags = mutagen.File(empty_id3_track.path).tags
  56. assert len(tags) == 1
  57. expected_desc = "symuid:pcnt:{}:{}:{}".format(
  58. play_count.player,
  59. play_count.library_id,
  60. int(play_count.register_dt.timestamp()),
  61. )
  62. tag = tags["TXXX:" + expected_desc]
  63. assert tag.desc == expected_desc
  64. assert tag.text == [str(play_count.count)]
  65. @pytest.mark.parametrize(
  66. ("play_count"), [symuid.PlayCount("pytest", "lib", utc_dt(), 7),]
  67. )
  68. def test_register_play_count_opus(empty_ogg_opus_path, play_count):
  69. track = symuid.Track(empty_ogg_opus_path)
  70. track.register_play_count(play_count)
  71. tags = mutagen.File(track.path).tags
  72. assert len(tags) == 1
  73. expected_desc = "symuid:pcnt:{}:{}:{}".format(
  74. play_count.player,
  75. play_count.library_id,
  76. int(play_count.register_dt.timestamp()),
  77. )
  78. assert tags[expected_desc] == [str(play_count.count)]
  79. @pytest.mark.parametrize(
  80. ("expected_counts"),
  81. [
  82. [],
  83. [symuid.PlayCount("player", "lib", utc_dt(), 3)],
  84. [
  85. symuid.PlayCount("a", "0", utc_dt(0), 1),
  86. symuid.PlayCount("b", "1", utc_dt(1), 2),
  87. ],
  88. [
  89. symuid.PlayCount("a", "0", utc_dt(0), 1),
  90. symuid.PlayCount("a", "2", utc_dt(1), 2),
  91. symuid.PlayCount("b", "3", utc_dt(3), 3),
  92. ],
  93. ],
  94. )
  95. def test__get_play_counts_all(empty_id3_track, expected_counts):
  96. for play_count in expected_counts:
  97. empty_id3_track.register_play_count(play_count)
  98. assert expected_counts == list(empty_id3_track._get_play_counts())
  99. def test__get_play_counts_filtered(empty_id3_track):
  100. counts = [
  101. symuid.PlayCount("a", "0", utc_dt(0), 1),
  102. symuid.PlayCount("a", "0", utc_dt(1), 2),
  103. symuid.PlayCount("a", "1", utc_dt(0), 3),
  104. symuid.PlayCount("b", "2", utc_dt(1), 4),
  105. ]
  106. for play_count in counts:
  107. empty_id3_track.register_play_count(play_count)
  108. assert set(empty_id3_track._get_play_counts(player="a")) == set(
  109. filter(lambda pc: pc.player == "a", counts)
  110. )
  111. assert set(empty_id3_track._get_play_counts(player="b")) == set(
  112. filter(lambda pc: pc.player == "b", counts)
  113. )
  114. assert set(empty_id3_track._get_play_counts(player="a", library_id="0")) == set(
  115. filter(lambda pc: pc.library_id == "0", counts)
  116. )
  117. assert set(empty_id3_track._get_play_counts(player="a", library_id="2")) == set()
  118. def test__get_latest_play_counts(empty_id3_track):
  119. counts = [
  120. symuid.PlayCount("a", "0", utc_dt(0), 1),
  121. symuid.PlayCount("a", "0", utc_dt(1), 2),
  122. symuid.PlayCount("a", "1", utc_dt(0), 3),
  123. symuid.PlayCount("a", "1", utc_dt(2), 4),
  124. symuid.PlayCount("b", "2", utc_dt(3), 5),
  125. ]
  126. for play_count in counts:
  127. empty_id3_track.register_play_count(play_count)
  128. assert set(empty_id3_track._get_latest_play_counts()) == set(
  129. [counts[1], counts[3], counts[4]]
  130. )
  131. assert set(empty_id3_track._get_latest_play_counts(player="a")) == set(
  132. [counts[1], counts[3]]
  133. )
  134. assert set(
  135. empty_id3_track._get_latest_play_counts(player="a", library_id="0")
  136. ) == set([counts[1]])
  137. def test_get_play_count_sum(empty_id3_track):
  138. counts = [
  139. symuid.PlayCount("a", "0", utc_dt(0), 1),
  140. symuid.PlayCount("a", "0", utc_dt(1), 2),
  141. symuid.PlayCount("a", "1", utc_dt(0), 3),
  142. symuid.PlayCount("a", "1", utc_dt(2), 4),
  143. symuid.PlayCount("b", "2", utc_dt(3), 5),
  144. ]
  145. for play_count in counts:
  146. empty_id3_track.register_play_count(play_count)
  147. assert 2 + 4 + 5 == empty_id3_track.get_play_count_sum()
  148. def test_increase_play_count(empty_id3_track):
  149. init_count = symuid.PlayCount("a", "0", utc_dt(0), 3)
  150. empty_id3_track.register_play_count(init_count)
  151. assert empty_id3_track.get_play_count_sum() == 3
  152. empty_id3_track.increase_play_count("a", "0")
  153. assert empty_id3_track.get_play_count_sum() == 4
  154. counts = set(empty_id3_track._get_play_counts())
  155. assert len(counts) == 2
  156. counts.remove(init_count)
  157. new_count = counts.pop()
  158. assert new_count.player == "a"
  159. assert new_count.library_id == "0"
  160. assert abs(new_count.register_dt - datetime_utc_now()).total_seconds() < 5
  161. assert new_count.count == 4
  162. def test_increase_play_count_init(empty_id3_track):
  163. empty_id3_track.increase_play_count("a", "0")
  164. assert empty_id3_track.get_play_count_sum() == 1
  165. (count,) = empty_id3_track._get_play_counts()
  166. assert count.player == "a"
  167. assert count.library_id == "0"
  168. assert abs(count.register_dt - datetime_utc_now()).total_seconds() < 5
  169. assert count.count == 1
  170. def test_increase_play_count_others(empty_id3_track):
  171. empty_id3_track.register_play_count(symuid.PlayCount("a", "0", utc_dt(0), 1),)
  172. empty_id3_track.register_play_count(symuid.PlayCount("a", "1", utc_dt(0), 2),)
  173. empty_id3_track.register_play_count(symuid.PlayCount("b", "0", utc_dt(0), 3),)
  174. assert empty_id3_track.get_play_count_sum() == 6
  175. empty_id3_track.increase_play_count("a", "1")
  176. assert empty_id3_track.get_play_count_sum() == 7
  177. assert len(list(empty_id3_track._get_play_counts(player="b"))) == 1
  178. assert len(list(empty_id3_track._get_play_counts(player="a", library_id="0"))) == 1
  179. assert len(list(empty_id3_track._get_play_counts(player="a", library_id="1"))) == 2
  180. def test_walk(tracks_dir_path):
  181. tracks = symuid.Track.walk(
  182. tracks_dir_path, path_ignore_regex=re.compile(r"typical")
  183. )
  184. track_names = set(os.path.basename(t.path) for t in tracks)
  185. assert track_names == {
  186. "id3v2.4-empty.mp3",
  187. "mp4-aac-empty.m4a",
  188. "ogg-opus-empty.opus",
  189. "ogg-vorbis-empty.ogg",
  190. }
  191. @pytest.mark.parametrize(
  192. ("status_stdout", "expected_path"),
  193. [
  194. (
  195. b"status playing\n"
  196. b"file /some/path/possibly including/spaces.mp3\n"
  197. b"duration 210\n"
  198. b"position 42\n"
  199. b"tag artist some artist\n"
  200. b"tag album some album\n"
  201. b"tag title some title\n"
  202. b"tag date 2012-03-04\n"
  203. b"tag bpm 0\n"
  204. b"set aaa_mode all\n"
  205. b"set continue true\n"
  206. b"set play_library true\n"
  207. b"set play_sorted false\n"
  208. b"set replaygain disabled\n"
  209. b"set replaygain_limit true\n"
  210. b"set replaygain_preamp 0.000000\n"
  211. b"set repeat false\n"
  212. b"set repeat_current false\n"
  213. b"set shuffle true\n"
  214. b"set softvol false\n"
  215. b"set vol_left 42\n"
  216. b"set vol_right 42\n",
  217. "/some/path/possibly including/spaces.mp3",
  218. ),
  219. ],
  220. )
  221. def test_get_active_cmus(tmpdir, status_stdout, expected_path):
  222. status_path = tmpdir.join("status")
  223. with status_path.open("wb") as status_file:
  224. status_file.write(status_stdout)
  225. cmd_mock_path = tmpdir.join("cmus-remote-mock")
  226. with cmd_mock_path.open("w") as cmd_file:
  227. cmd_file.write("#!/bin/sh\n")
  228. cmd_file.write('[ "$@" = "-Q" ] || exit 1\n')
  229. cmd_file.write("cat {}\n".format(shlex.quote(status_path.strpath)))
  230. cmd_mock_path.chmod(stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
  231. with unittest.mock.patch("symuid.Track.__init__", return_value=None) as init_mock:
  232. with unittest.mock.patch(
  233. "symuid.Track._CMUS_REMOTE_COMMAND", cmd_mock_path.strpath
  234. ):
  235. assert isinstance(symuid.Track.get_active_cmus(), symuid.Track)
  236. init_mock.assert_called_once()
  237. init_args, init_kwargs = init_mock.call_args
  238. assert init_args == tuple()
  239. assert init_kwargs == {"path": expected_path}
  240. def test_get_active_cmus_none(tmpdir):
  241. status_path = tmpdir.join("status")
  242. with status_path.open("wb") as status_file:
  243. status_file.write(
  244. b"status stopped\n"
  245. b"set aaa_mode all\n"
  246. b"set continue true\n"
  247. b"set play_library true\n"
  248. b"set play_sorted false\n"
  249. b"set replaygain disabled\n"
  250. b"set replaygain_limit true\n"
  251. b"set replaygain_preamp 0.000000\n"
  252. b"set repeat false\n"
  253. b"set repeat_current false\n"
  254. b"set shuffle true\n"
  255. b"set softvol false\n"
  256. b"set vol_left 0\n"
  257. b"set vol_right 0\n"
  258. )
  259. cmd_mock_path = tmpdir.join("cmus-remote-mock")
  260. with cmd_mock_path.open("w") as cmd_file:
  261. cmd_file.write("#!/bin/sh\n")
  262. cmd_file.write('[ "$@" = "-Q" ] || exit 1\n')
  263. cmd_file.write("cat {}\n".format(shlex.quote(status_path.strpath)))
  264. cmd_mock_path.chmod(stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
  265. with unittest.mock.patch(
  266. "symuid.Track._CMUS_REMOTE_COMMAND", cmd_mock_path.strpath
  267. ):
  268. assert symuid.Track.get_active_cmus() is None
  269. def test_get_active_cmus_not_running(tmpdir):
  270. cmd_mock_path = tmpdir.join("cmus-remote-mock")
  271. with cmd_mock_path.open("w") as cmd_file:
  272. cmd_file.write("#!/bin/sh\n")
  273. cmd_file.write("echo cmus-remote: cmus is not running >> /dev/stderr\n")
  274. cmd_file.write("exit 1\n")
  275. cmd_mock_path.chmod(stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
  276. with unittest.mock.patch(
  277. "symuid.Track._CMUS_REMOTE_COMMAND", cmd_mock_path.strpath
  278. ):
  279. assert symuid.Track.get_active_cmus() is None
  280. def test_get_active_cmus_unexpected_error(tmpdir):
  281. cmd_mock_path = tmpdir.join("cmus-remote-mock")
  282. with cmd_mock_path.open("w") as cmd_file:
  283. cmd_file.write("#!/bin/sh\n")
  284. cmd_file.write("exit 42\n")
  285. cmd_mock_path.chmod(stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
  286. with unittest.mock.patch(
  287. "symuid.Track._CMUS_REMOTE_COMMAND", cmd_mock_path.strpath
  288. ):
  289. with pytest.raises(subprocess.CalledProcessError):
  290. symuid.Track.get_active_cmus()