Skip to content

Commit

Permalink
Add tests for AlbumPlayCountMode, change the calc to match the reques…
Browse files Browse the repository at this point in the history
…t from #1032
  • Loading branch information
deluan committed Apr 27, 2024
1 parent 5d50558 commit 92a98cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion persistence/album_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"strings"

. "github.com/Masterminds/squirrel"
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions persistence/album_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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),
)
})
})

0 comments on commit 92a98cd

Please sign in to comment.