diff --git a/persistence/player_repository_test.go b/persistence/player_repository_test.go index 08b1e577e..7502aef5b 100644 --- a/persistence/player_repository_test.go +++ b/persistence/player_repository_test.go @@ -191,7 +191,7 @@ var _ = Describe("PlayerRepository", func() { Expect(err).To(BeNil()) clone.ID = id - new, err := repo.Get(id) + newItem, err := repo.Get(id) if clone.UserId == "" { Expect(count).To(Equal(baseCount)) @@ -199,7 +199,7 @@ var _ = Describe("PlayerRepository", func() { } else { Expect(count).To(Equal(baseCount + 1)) Expect(err).To(BeNil()) - Expect(*new).To(Equal(clone)) + Expect(*newItem).To(Equal(clone)) } }, Entry("same user", userPlayer), @@ -225,14 +225,14 @@ var _ = Describe("PlayerRepository", func() { } clone.MaxBitRate = player.MaxBitRate - new, err := repo.Get(clone.ID) + newItem, err := repo.Get(clone.ID) if player.UserId == "" { Expect(err).To(Equal(model.ErrNotFound)) } else if !admin && player.UserId == adminUser.ID { - Expect(*new).To(Equal(player)) + Expect(*newItem).To(Equal(player)) } else { - Expect(*new).To(Equal(clone)) + Expect(*newItem).To(Equal(clone)) } }, Entry("same user", userPlayer), diff --git a/persistence/sql_base_repository.go b/persistence/sql_base_repository.go index 2df360a08..b87d354c8 100644 --- a/persistence/sql_base_repository.go +++ b/persistence/sql_base_repository.go @@ -139,9 +139,12 @@ func (r sqlRepository) applyFilters(sq SelectBuilder, options ...model.QueryOpti return sq } +func (r sqlRepository) seedKey() string { + return r.tableName + userId(r.ctx) +} + func (r sqlRepository) seededRandomSort() string { - u, _ := request.UserFrom(r.ctx) - return fmt.Sprintf("SEEDEDRAND('%s', %s.id)", r.tableName+u.ID, r.tableName) + return fmt.Sprintf("SEEDEDRAND('%s', %s.id)", r.seedKey(), r.tableName) } func (r sqlRepository) resetSeededRandom(options []model.QueryOptions) { @@ -150,12 +153,12 @@ func (r sqlRepository) resetSeededRandom(options []model.QueryOptions) { } if options[0].Seed != "" { - hasher.SetSeed(r.tableName+userId(r.ctx), options[0].Seed) + hasher.SetSeed(r.seedKey(), options[0].Seed) return } if options[0].Offset == 0 { - hasher.Reseed(r.tableName + userId(r.ctx)) + hasher.Reseed(r.seedKey()) } } diff --git a/persistence/sql_base_repository_test.go b/persistence/sql_base_repository_test.go index 83fad4b39..8b1900a62 100644 --- a/persistence/sql_base_repository_test.go +++ b/persistence/sql_base_repository_test.go @@ -1,8 +1,11 @@ package persistence import ( + "context" + "github.com/Masterminds/squirrel" "github.com/navidrome/navidrome/model" + "github.com/navidrome/navidrome/model/request" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" ) @@ -151,5 +154,13 @@ var _ = Describe("sqlRepository", func() { Expect(sql).To(Equal("name asc, coalesce(nullif(release_date, ''), nullif(original_date, '')) desc, status desc")) }) }) + Context("seededRandomSort", func() { + It("returns a random sort order function call", func() { + r.ctx = request.WithUser(context.Background(), model.User{ID: "2"}) + r.tableName = "media_file" + sql := r.seededRandomSort() + Expect(sql).To(ContainSubstring("SEEDEDRAND('media_file2', media_file.id)")) + }) + }) }) })