Add tests for AlbumPlayCountMode, change the calc to match the request from #1032

This commit is contained in:
Deluan 2024-04-27 15:20:46 -04:00
parent 5d50558610
commit 92a98cd558
2 changed files with 8 additions and 1 deletions

View File

@ -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)
}

View File

@ -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),
)
})
})