test_sync.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import os
  2. import shutil
  3. import unittest.mock
  4. import pytest
  5. from symuid import PlayCount, Track
  6. from symuid._datetime import unix_epoch_time_to_datetime_utc
  7. from symuid._uuid import generate_uuid4_bytes
  8. from symuid.sync import _main, _walk_tracks, sync
  9. def test_add_uuid(empty_ogg_opus_path):
  10. assert Track(empty_ogg_opus_path).get_uuid() is None
  11. sync(_walk_tracks([os.path.dirname(empty_ogg_opus_path)]))
  12. assert Track(empty_ogg_opus_path).get_uuid() is not None
  13. def test_add_uuid_idempotent(empty_ogg_opus_path):
  14. sync(_walk_tracks([os.path.dirname(empty_ogg_opus_path)]))
  15. uuid = Track(empty_ogg_opus_path).get_uuid()
  16. sync(_walk_tracks([os.path.dirname(empty_ogg_opus_path)]))
  17. assert Track(empty_ogg_opus_path).get_uuid() == uuid
  18. def test_add_uuid_main_file(empty_ogg_opus_path):
  19. assert Track(empty_ogg_opus_path).get_uuid() is None
  20. with unittest.mock.patch("sys.argv", ["", empty_ogg_opus_path]):
  21. _main()
  22. assert Track(empty_ogg_opus_path).get_uuid() is not None
  23. def test_add_uuid_main_multiple_files(empty_ogg_opus_path, empty_ogg_vorbis_path):
  24. with unittest.mock.patch(
  25. "sys.argv", ["", empty_ogg_opus_path, empty_ogg_vorbis_path]
  26. ):
  27. _main()
  28. assert Track(empty_ogg_opus_path).get_uuid() is not None
  29. assert Track(empty_ogg_vorbis_path).get_uuid() is not None
  30. def test_add_uuid_main_dir(empty_ogg_opus_path):
  31. assert Track(empty_ogg_opus_path).get_uuid() is None
  32. with unittest.mock.patch("sys.argv", ["", os.path.dirname(empty_ogg_opus_path)]):
  33. _main()
  34. assert Track(empty_ogg_opus_path).get_uuid() is not None
  35. def test_sync_play_count(tmpdir, tracks_dir_path):
  36. shutil.copyfile(
  37. src=os.path.join(tracks_dir_path, "id3v2.4-empty.mp3"),
  38. dst=tmpdir.join("a1.mp3"),
  39. )
  40. shutil.copyfile(
  41. src=os.path.join(tracks_dir_path, "ogg-vorbis-empty.ogg"),
  42. dst=tmpdir.join("a2.ogg"),
  43. )
  44. shutil.copyfile(
  45. src=os.path.join(tracks_dir_path, "ogg-opus-empty.opus"),
  46. dst=tmpdir.join("a3.opus"),
  47. )
  48. shutil.copyfile(
  49. src=os.path.join(tracks_dir_path, "id3v2.4-empty.mp3"),
  50. dst=tmpdir.join("b.mp3"),
  51. )
  52. uuid_a = generate_uuid4_bytes()
  53. uuid_b = generate_uuid4_bytes()
  54. track_a1 = Track(tmpdir.join("a1.mp3"))
  55. track_a1.assign_uuid(uuid_a)
  56. track_a2 = Track(tmpdir.join("a2.ogg"))
  57. track_a2.assign_uuid(uuid_a)
  58. track_a3 = Track(tmpdir.join("a3.opus"))
  59. track_a3.assign_uuid(uuid_a)
  60. track_b = Track(tmpdir.join("b.mp3"))
  61. track_b.assign_uuid(uuid_b)
  62. track_a1.register_play_count(
  63. PlayCount(
  64. player="cmus",
  65. library_id="lib1",
  66. register_dt=unix_epoch_time_to_datetime_utc(0),
  67. count=21,
  68. )
  69. )
  70. track_a1.register_play_count(
  71. PlayCount(
  72. player="cmus",
  73. library_id="lib1",
  74. register_dt=unix_epoch_time_to_datetime_utc(1),
  75. count=22,
  76. )
  77. )
  78. track_a2.register_play_count(
  79. PlayCount(
  80. player="cmus",
  81. library_id="lib1",
  82. register_dt=unix_epoch_time_to_datetime_utc(0),
  83. count=21,
  84. )
  85. )
  86. track_a2.register_play_count(
  87. PlayCount(
  88. player="cmus",
  89. library_id="lib2",
  90. register_dt=unix_epoch_time_to_datetime_utc(2),
  91. count=7,
  92. )
  93. )
  94. track_b.register_play_count(
  95. PlayCount(
  96. player="cmus",
  97. library_id="lib2",
  98. register_dt=unix_epoch_time_to_datetime_utc(3),
  99. count=4,
  100. )
  101. )
  102. sync((track_a1, track_a2, track_a3, track_b))
  103. play_counts_a1 = set(Track(tmpdir.join("a1.mp3")).get_play_counts())
  104. play_counts_a2 = set(Track(tmpdir.join("a2.ogg")).get_play_counts())
  105. play_counts_a3 = set(Track(tmpdir.join("a3.opus")).get_play_counts())
  106. play_counts_b = set(Track(tmpdir.join("b.mp3")).get_play_counts())
  107. assert len(play_counts_a1) == 3
  108. assert len(play_counts_a1) == len(play_counts_a2)
  109. assert len(play_counts_a1) == len(play_counts_a3)
  110. assert play_counts_a1 == play_counts_a2
  111. assert {pc.register_dt.timestamp() for pc in play_counts_a1} == pytest.approx(
  112. {0, 1, 2}
  113. )
  114. assert len(play_counts_b) == 1
  115. assert {pc.register_dt.timestamp() for pc in play_counts_b} == pytest.approx({3})
  116. def test_sync_play_count_callback(tmpdir, tracks_dir_path):
  117. shutil.copyfile(
  118. src=os.path.join(tracks_dir_path, "id3v2.4-empty.mp3"),
  119. dst=tmpdir.join("a1.mp3"),
  120. )
  121. shutil.copyfile(
  122. src=os.path.join(tracks_dir_path, "ogg-vorbis-empty.ogg"),
  123. dst=tmpdir.join("a2.ogg"),
  124. )
  125. uuid_a = generate_uuid4_bytes()
  126. track_a1 = Track(tmpdir.join("a1.mp3"))
  127. track_a1.assign_uuid(uuid_a)
  128. track_a2 = Track(tmpdir.join("a2.ogg"))
  129. track_a2.assign_uuid(uuid_a)
  130. track_a1.register_play_count(
  131. PlayCount(
  132. player="cmus",
  133. library_id="lib1",
  134. register_dt=unix_epoch_time_to_datetime_utc(0),
  135. count=21,
  136. )
  137. )
  138. play_count_added_cb = unittest.mock.MagicMock()
  139. sync(_walk_tracks([tmpdir]), play_count_added_cb=play_count_added_cb)
  140. play_count_added_cb.assert_called_once_with(
  141. track_a2, ("symuid:pcnt:cmus:lib1:0", ["21"])
  142. )
  143. def test_sync_play_count_callback_main(capsys, tmpdir, tracks_dir_path):
  144. shutil.copyfile(
  145. src=os.path.join(tracks_dir_path, "id3v2.4-empty.mp3"),
  146. dst=tmpdir.join("a1.mp3"),
  147. )
  148. shutil.copyfile(
  149. src=os.path.join(tracks_dir_path, "ogg-vorbis-empty.ogg"),
  150. dst=tmpdir.join("a2.ogg"),
  151. )
  152. uuid_a = generate_uuid4_bytes()
  153. track_a1 = Track(tmpdir.join("a1.mp3"))
  154. track_a1.assign_uuid(uuid_a)
  155. track_a2 = Track(tmpdir.join("a2.ogg"))
  156. track_a2.assign_uuid(uuid_a)
  157. track_a1.register_play_count(
  158. PlayCount(
  159. player="cmus",
  160. library_id="lib1",
  161. register_dt=unix_epoch_time_to_datetime_utc(0),
  162. count=21,
  163. )
  164. )
  165. with unittest.mock.patch("sys.argv", ["", tmpdir.strpath]):
  166. _main()
  167. out, _ = capsys.readouterr()
  168. assert (
  169. out
  170. == "{!r}: added play count tag ('symuid:pcnt:cmus:lib1:0', ['21'])\n".format(
  171. track_a2.path
  172. )
  173. )