From 92a98cd558dd7f05f65fea4830a661aa47abacf9 Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 27 Apr 2024 15:20:46 -0400 Subject: [PATCH] Add tests for AlbumPlayCountMode, change the calc to match the request from #1032 --- persistence/album_repository.go | 3 ++- persistence/album_repository_test.go | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/persistence/album_repository.go b/persistence/album_repository.go index 981e55ebf..3b9b49a1d 100644 --- a/persistence/album_repository.go +++ b/persistence/album_repository.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "math" "strings" . "github.com/Masterminds/squirrel" @@ -174,7 +175,7 @@ func (r *albumRepository) toModels(dba []dbAlbum) model.Albums { res := model.Albums{} for i := range dba { if conf.Server.AlbumPlayCountMode == consts.AlbumPlayCountModeNormalized && dba[i].Album.SongCount != 0 { - dba[i].Album.PlayCount = (dba[i].Album.PlayCount + (int64(dba[i].Album.SongCount) - 1)) / int64(dba[i].Album.SongCount) + dba[i].Album.PlayCount = int64(math.Round(float64(dba[i].Album.PlayCount) / float64(dba[i].Album.SongCount))) } res = append(res, *dba[i].Album) } diff --git a/persistence/album_repository_test.go b/persistence/album_repository_test.go index 0e7eb7edf..6c31233fe 100644 --- a/persistence/album_repository_test.go +++ b/persistence/album_repository_test.go @@ -120,10 +120,13 @@ var _ = Describe("AlbumRepository", func() { albums := repo.toModels(dba) Expect(albums[0].PlayCount).To(Equal(int64(expected))) }, + Entry("1 song, 0 plays", 1, 0, 0), Entry("1 song, 4 plays", 1, 4, 4), Entry("3 songs, 6 plays", 3, 6, 6), + Entry("10 songs, 6 plays", 10, 6, 6), Entry("70 songs, 70 plays", 70, 70, 70), Entry("10 songs, 50 plays", 10, 50, 50), + Entry("120 songs, 121 plays", 120, 121, 121), ) DescribeTable("normalizes play count when AlbumPlayCountMode is normalized", @@ -135,10 +138,13 @@ var _ = Describe("AlbumRepository", func() { albums := repo.toModels(dba) Expect(albums[0].PlayCount).To(Equal(int64(expected))) }, + Entry("1 song, 0 plays", 1, 0, 0), Entry("1 song, 4 plays", 1, 4, 4), Entry("3 songs, 6 plays", 3, 6, 2), + Entry("10 songs, 6 plays", 10, 6, 1), Entry("70 songs, 70 plays", 70, 70, 1), Entry("10 songs, 50 plays", 10, 50, 5), + Entry("120 songs, 121 plays", 120, 121, 1), ) }) })