Browse Source

added Track.increase_play_count()

Fabian Peter Hammerle 5 years ago
parent
commit
eb231e2888
2 changed files with 62 additions and 1 deletions
  1. 13 1
      symuid/__init__.py
  2. 49 0
      tests/test_track.py

+ 13 - 1
symuid/__init__.py

@@ -104,7 +104,7 @@ class Track:
 
     def get_latest_play_count(self, player, library_id):
         assert player is not None, player
-        assert library is not None, library_id
+        assert library_id is not None, library_id
         counts = self._get_latest_play_counts(player, library_id)
         if len(counts) == 0:
             return None
@@ -129,6 +129,18 @@ class Track:
         elif current_count != pc.count:
             raise Exception((current_count, pc.count))
 
+    def increase_play_count(self, player, library_id, tag_set_cb=None):
+        current_pc = self.get_latest_play_count(player, library_id)
+        self.register_play_count(
+            PlayCount(
+                player=player,
+                library_id=library_id,
+                register_dt=_utc_dt_now(),
+                count=current_pc.count + 1 if current_pc else 1,
+            ),
+            tag_set_cb=tag_set_cb,
+        )
+
     @classmethod
     def walk(cls, root_path, path_ignore_regex, ignored_cb=None, unsupported_cb=None):
         for dirpath, dirnames, filenames in os.walk(root_path):

+ 49 - 0
tests/test_track.py

@@ -106,3 +106,52 @@ def test_get_play_count_sum(empty_id3_track):
     for pc in counts:
         empty_id3_track.register_play_count(pc)
     assert 2 + 4 + 5 == empty_id3_track.get_play_count_sum()
+
+
+def test_increase_play_count(empty_id3_track):
+    init_count = symuid.PlayCount('a', '0', utc_dt(0), 3)
+    empty_id3_track.register_play_count(init_count)
+    assert 3 == empty_id3_track.get_play_count_sum()
+    empty_id3_track.increase_play_count('a', '0')
+    dt = symuid._utc_dt_now()
+    assert 4 == empty_id3_track.get_play_count_sum()
+    counts = set(empty_id3_track._get_play_counts())
+    assert len(counts) == 2
+    counts.remove(init_count)
+    new_count = counts.pop()
+    assert new_count.player == 'a'
+    assert new_count.library_id == '0'
+    assert abs(new_count.register_dt - dt).total_seconds() < 5
+    assert new_count.count == 4
+
+
+def test_increase_play_count_init(empty_id3_track):
+    empty_id3_track.increase_play_count('a', '0')
+    assert 1 == empty_id3_track.get_play_count_sum()
+    count, = empty_id3_track._get_play_counts()
+    assert count.player == 'a'
+    assert count.library_id == '0'
+    assert abs(count.register_dt - symuid._utc_dt_now()).total_seconds() < 5
+    assert count.count == 1
+
+
+def test_increase_play_count_others(empty_id3_track):
+    empty_id3_track.register_play_count(
+        symuid.PlayCount('a', '0', utc_dt(0), 1),
+    )
+    empty_id3_track.register_play_count(
+        symuid.PlayCount('a', '1', utc_dt(0), 2),
+    )
+    empty_id3_track.register_play_count(
+        symuid.PlayCount('b', '0', utc_dt(0), 3),
+    )
+    assert 6 == empty_id3_track.get_play_count_sum()
+    empty_id3_track.increase_play_count('a', '1')
+    assert 7 == empty_id3_track.get_play_count_sum()
+    assert 1 == len(list(empty_id3_track._get_play_counts(player='b')))
+    assert 1 == len(list(
+        empty_id3_track._get_play_counts(player='a', library_id='0')
+    ))
+    assert 2 == len(list(
+        empty_id3_track._get_play_counts(player='a', library_id='1')
+    ))