Small refactoring

- Remove duplication
- Remove warning about builtin keyword `new`
This commit is contained in:
Deluan 2024-09-13 20:18:00 -04:00
parent e8d0f2ec2c
commit e65eb225c8
3 changed files with 23 additions and 9 deletions

View file

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

View file

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

View file

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