test_track.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import datetime as dt
  2. import os
  3. import re
  4. import mutagen
  5. import pytest
  6. import symuid
  7. from symuid._datetime import datetime_utc_now
  8. # pylint: disable=redefined-outer-name,protected-access
  9. # TODO test aac / m4a itunes tags
  10. TRACKS_DIR_PATH = os.path.join(os.path.dirname(__file__), 'tracks')
  11. def utc_dt(hour=0):
  12. return dt.datetime(2018, 9, 26, hour, tzinfo=dt.timezone.utc)
  13. @pytest.fixture
  14. def empty_id3_track(empty_id3_path):
  15. return symuid.Track(empty_id3_path)
  16. @pytest.mark.parametrize('track_name', [
  17. 'id3v2.4-typical.mp3',
  18. 'id3v2.4-empty.mp3',
  19. 'ogg-opus-typical.opus',
  20. 'ogg-vorbis-typical.ogg',
  21. ])
  22. def test_init(tracks_dir_path, track_name):
  23. symuid.Track(os.path.join(tracks_dir_path, track_name))
  24. @pytest.mark.parametrize(('track_name', 'expected_comment'), [
  25. ('id3v2.4-typical.mp3', 'some comment'),
  26. ('id3v2.4-empty.mp3', None),
  27. ('ogg-opus-typical.opus', 'some comment'),
  28. ('ogg-vorbis-typical.ogg', 'some further information'),
  29. ])
  30. def test_get_comment(tracks_dir_path, track_name, expected_comment):
  31. track = symuid.Track(os.path.join(tracks_dir_path, track_name))
  32. assert expected_comment == track.comment
  33. def test_set_comment(empty_id3_track):
  34. assert empty_id3_track.comment is None
  35. empty_id3_track.comment = 'note'
  36. assert empty_id3_track.comment == 'note'
  37. empty_id3_track.comment += 's'
  38. assert empty_id3_track.comment == 'notes'
  39. assert symuid.Track(empty_id3_track.path).comment == 'notes'
  40. @pytest.mark.parametrize(('play_count'), [
  41. symuid.PlayCount('pytest', 'lib', utc_dt(), 7),
  42. ])
  43. def test_register_play_count_id3(empty_id3_track, play_count):
  44. empty_id3_track.register_play_count(play_count)
  45. tags = mutagen.File(empty_id3_track.path).tags
  46. assert len(tags) == 1
  47. expected_desc = 'symuid:pcnt:{}:{}:{}'.format(
  48. play_count.player, play_count.library_id, int(
  49. play_count.register_dt.timestamp()),
  50. )
  51. tag = tags['TXXX:' + expected_desc]
  52. assert tag.desc == expected_desc
  53. assert tag.text == [str(play_count.count)]
  54. @pytest.mark.parametrize(('play_count'), [
  55. symuid.PlayCount('pytest', 'lib', utc_dt(), 7),
  56. ])
  57. def test_register_play_count_opus(empty_ogg_opus_path, play_count):
  58. track = symuid.Track(empty_ogg_opus_path)
  59. track.register_play_count(play_count)
  60. tags = mutagen.File(track.path).tags
  61. assert len(tags) == 1
  62. expected_desc = 'symuid:pcnt:{}:{}:{}'.format(
  63. play_count.player, play_count.library_id, int(
  64. play_count.register_dt.timestamp()),
  65. )
  66. assert tags[expected_desc] == [str(play_count.count)]
  67. @pytest.mark.parametrize(('expected_counts'), [
  68. [],
  69. [symuid.PlayCount('player', 'lib', utc_dt(), 3)],
  70. [symuid.PlayCount('a', '0', utc_dt(0), 1),
  71. symuid.PlayCount('b', '1', utc_dt(1), 2)],
  72. [symuid.PlayCount('a', '0', utc_dt(0), 1),
  73. symuid.PlayCount('a', '2', utc_dt(1), 2),
  74. symuid.PlayCount('b', '3', utc_dt(3), 3)],
  75. ])
  76. def test__get_play_counts_all(empty_id3_track, expected_counts):
  77. for play_count in expected_counts:
  78. empty_id3_track.register_play_count(play_count)
  79. assert expected_counts == list(empty_id3_track._get_play_counts())
  80. def test__get_play_counts_filtered(empty_id3_track):
  81. counts = [symuid.PlayCount('a', '0', utc_dt(0), 1),
  82. symuid.PlayCount('a', '0', utc_dt(1), 2),
  83. symuid.PlayCount('a', '1', utc_dt(0), 3),
  84. symuid.PlayCount('b', '2', utc_dt(1), 4)]
  85. for play_count in counts:
  86. empty_id3_track.register_play_count(play_count)
  87. assert set(empty_id3_track._get_play_counts(player='a')) \
  88. == set(filter(lambda pc: pc.player == 'a', counts))
  89. assert set(empty_id3_track._get_play_counts(player='b')) \
  90. == set(filter(lambda pc: pc.player == 'b', counts))
  91. assert set(empty_id3_track._get_play_counts(player='a', library_id='0')) \
  92. == set(filter(lambda pc: pc.library_id == '0', counts))
  93. assert set(empty_id3_track._get_play_counts(player='a', library_id='2')) \
  94. == set()
  95. def test__get_latest_play_counts(empty_id3_track):
  96. counts = [symuid.PlayCount('a', '0', utc_dt(0), 1),
  97. symuid.PlayCount('a', '0', utc_dt(1), 2),
  98. symuid.PlayCount('a', '1', utc_dt(0), 3),
  99. symuid.PlayCount('a', '1', utc_dt(2), 4),
  100. symuid.PlayCount('b', '2', utc_dt(3), 5)]
  101. for play_count in counts:
  102. empty_id3_track.register_play_count(play_count)
  103. assert set(empty_id3_track._get_latest_play_counts()) \
  104. == set([counts[1], counts[3], counts[4]])
  105. assert set(empty_id3_track._get_latest_play_counts(player='a')) \
  106. == set([counts[1], counts[3]])
  107. assert set(empty_id3_track._get_latest_play_counts(player='a', library_id='0')) \
  108. == set([counts[1]])
  109. def test_get_play_count_sum(empty_id3_track):
  110. counts = [symuid.PlayCount('a', '0', utc_dt(0), 1),
  111. symuid.PlayCount('a', '0', utc_dt(1), 2),
  112. symuid.PlayCount('a', '1', utc_dt(0), 3),
  113. symuid.PlayCount('a', '1', utc_dt(2), 4),
  114. symuid.PlayCount('b', '2', utc_dt(3), 5)]
  115. for play_count in counts:
  116. empty_id3_track.register_play_count(play_count)
  117. assert 2 + 4 + 5 == empty_id3_track.get_play_count_sum()
  118. def test_increase_play_count(empty_id3_track):
  119. init_count = symuid.PlayCount('a', '0', utc_dt(0), 3)
  120. empty_id3_track.register_play_count(init_count)
  121. assert empty_id3_track.get_play_count_sum() == 3
  122. empty_id3_track.increase_play_count('a', '0')
  123. assert empty_id3_track.get_play_count_sum() == 4
  124. counts = set(empty_id3_track._get_play_counts())
  125. assert len(counts) == 2
  126. counts.remove(init_count)
  127. new_count = counts.pop()
  128. assert new_count.player == 'a'
  129. assert new_count.library_id == '0'
  130. assert abs(new_count.register_dt - datetime_utc_now()).total_seconds() < 5
  131. assert new_count.count == 4
  132. def test_increase_play_count_init(empty_id3_track):
  133. empty_id3_track.increase_play_count('a', '0')
  134. assert empty_id3_track.get_play_count_sum() == 1
  135. count, = empty_id3_track._get_play_counts()
  136. assert count.player == 'a'
  137. assert count.library_id == '0'
  138. assert abs(count.register_dt - datetime_utc_now()).total_seconds() < 5
  139. assert count.count == 1
  140. def test_increase_play_count_others(empty_id3_track):
  141. empty_id3_track.register_play_count(
  142. symuid.PlayCount('a', '0', utc_dt(0), 1),
  143. )
  144. empty_id3_track.register_play_count(
  145. symuid.PlayCount('a', '1', utc_dt(0), 2),
  146. )
  147. empty_id3_track.register_play_count(
  148. symuid.PlayCount('b', '0', utc_dt(0), 3),
  149. )
  150. assert empty_id3_track.get_play_count_sum() == 6
  151. empty_id3_track.increase_play_count('a', '1')
  152. assert empty_id3_track.get_play_count_sum() == 7
  153. assert len(list(empty_id3_track._get_play_counts(player='b'))) == 1
  154. assert len(list(
  155. empty_id3_track._get_play_counts(player='a', library_id='0')
  156. )) == 1
  157. assert len(list(
  158. empty_id3_track._get_play_counts(player='a', library_id='1')
  159. )) == 2
  160. def test_walk(tracks_dir_path):
  161. tracks = symuid.Track.walk(
  162. tracks_dir_path,
  163. path_ignore_regex=re.compile(r'typical'))
  164. track_names = set(os.path.basename(t.path) for t in tracks)
  165. assert track_names == {'id3v2.4-empty.mp3',
  166. 'mp4-aac-empty.m4a',
  167. 'ogg-opus-empty.opus',
  168. 'ogg-vorbis-empty.ogg'}