navidrome/tests/mock_artist_repo.go
Deluan Quintão b59eb32961
Some checks are pending
Pipeline: Test, Lint, Build / Get version info (push) Waiting to run
Pipeline: Test, Lint, Build / Lint Go code (push) Waiting to run
Pipeline: Test, Lint, Build / Test Go code (push) Waiting to run
Pipeline: Test, Lint, Build / Test JS code (push) Waiting to run
Pipeline: Test, Lint, Build / Lint i18n files (push) Waiting to run
Pipeline: Test, Lint, Build / Check Docker configuration (push) Waiting to run
Pipeline: Test, Lint, Build / Build (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-1 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-2 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-3 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-4 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-5 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-6 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-7 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-8 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-9 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-10 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Push to GHCR (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build Windows installers (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Package/Release (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Blocked by required conditions
feat(subsonic): sort search3 results by relevance (#5086)
* fix(subsonic): optimize search3 for high-cardinality FTS queries

Use a two-phase query strategy for FTS5 searches to avoid the
performance penalty of expensive LEFT JOINs (annotation, bookmark,
library) on high-cardinality results like "the".

Phase 1 runs a lightweight query (main table + FTS index only) to get
sorted, paginated rowids. Phase 2 hydrates only those few rowids with
the full JOINs, making them nearly free.

For queries with complex ORDER BY expressions that reference joined
tables (e.g. artist search sorted by play count), the optimization is
skipped and the original single-query approach is used.

* fix(search): update order by clauses to include 'rank' for FTS queries

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(search): reintroduce 'rank' in Phase 2 ORDER BY for FTS queries

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(search): remove 'rank' from ORDER BY in non-FTS queries and adjust two-phase query handling

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(search): update FTS ranking to use bm25 weights and simplify ORDER BY qualification

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(search): refine FTS query handling and improve comments for clarity

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(search): refactor full-text search handling to streamline query strategy selection and improve LIKE fallback logic.

Increase e2e coverage for search3

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: enhance FTS column definitions and relevance weights

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(search): refactor Search method signatures to remove offset and size parameters, streamline query handling

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(search): allow single-character queries in search strategies and update related tests

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(search): make FTS Phase 1 treat Max=0 as no limit, reorganize tests

FTS Phase 1 unconditionally called Limit(uint64(options.Max)), which
produced LIMIT 0 when Max was zero. This diverged from applyOptions
where Max=0 means no limit. Now Phase 1 mirrors applyOptions: only add
LIMIT/OFFSET when the value is positive. Also moved legacy backend
integration tests from sql_search_fts_test.go to sql_search_like_test.go
and added regression tests for the Max=0 behavior on both backends.

* refactor: simplify callSearch function by removing variadic options and directly using QueryOptions

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(search): implement ftsQueryDegraded function to detect significant content loss in FTS queries

Signed-off-by: Deluan <deluan@navidrome.org>

---------

Signed-off-by: Deluan <deluan@navidrome.org>
2026-02-23 08:51:54 -05:00

160 lines
3.5 KiB
Go

package tests
import (
"errors"
"time"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/id"
)
func CreateMockArtistRepo() *MockArtistRepo {
return &MockArtistRepo{
Data: make(map[string]*model.Artist),
}
}
type MockArtistRepo struct {
model.ArtistRepository
Data map[string]*model.Artist
Err bool
Options model.QueryOptions
}
func (m *MockArtistRepo) SetError(err bool) {
m.Err = err
}
func (m *MockArtistRepo) SetData(artists model.Artists) {
m.Data = make(map[string]*model.Artist)
for i, a := range artists {
m.Data[a.ID] = &artists[i]
}
}
func (m *MockArtistRepo) Exists(id string) (bool, error) {
if m.Err {
return false, errors.New("Error!")
}
_, found := m.Data[id]
return found, nil
}
func (m *MockArtistRepo) Get(id string) (*model.Artist, error) {
if m.Err {
return nil, errors.New("Error!")
}
if d, ok := m.Data[id]; ok {
return d, nil
}
return nil, model.ErrNotFound
}
func (m *MockArtistRepo) Put(ar *model.Artist, columsToUpdate ...string) error {
if m.Err {
return errors.New("error")
}
if ar.ID == "" {
ar.ID = id.NewRandom()
}
m.Data[ar.ID] = ar
return nil
}
func (m *MockArtistRepo) IncPlayCount(id string, timestamp time.Time) error {
if m.Err {
return errors.New("error")
}
if d, ok := m.Data[id]; ok {
d.PlayCount++
d.PlayDate = &timestamp
return nil
}
return model.ErrNotFound
}
func (m *MockArtistRepo) GetAll(options ...model.QueryOptions) (model.Artists, error) {
if len(options) > 0 {
m.Options = options[0]
}
if m.Err {
return nil, errors.New("mock repo error")
}
var allArtists model.Artists
for _, artist := range m.Data {
allArtists = append(allArtists, *artist)
}
// Apply Max=1 if present (simple simulation for findArtistByName)
if len(options) > 0 && options[0].Max == 1 && len(allArtists) > 0 {
return allArtists[:1], nil
}
return allArtists, nil
}
func (m *MockArtistRepo) UpdateExternalInfo(artist *model.Artist) error {
if m.Err {
return errors.New("mock repo error")
}
return nil
}
func (m *MockArtistRepo) RefreshStats(allArtists bool) (int64, error) {
if m.Err {
return 0, errors.New("mock repo error")
}
return int64(len(m.Data)), nil
}
func (m *MockArtistRepo) RefreshPlayCounts() (int64, error) {
if m.Err {
return 0, errors.New("mock repo error")
}
return int64(len(m.Data)), nil
}
func (m *MockArtistRepo) GetIndex(includeMissing bool, libraryIds []int, roles ...model.Role) (model.ArtistIndexes, error) {
if m.Err {
return nil, errors.New("mock repo error")
}
artists, err := m.GetAll()
if err != nil {
return nil, err
}
// For mock purposes, if no artists available, return empty result
if len(artists) == 0 {
return model.ArtistIndexes{}, nil
}
// Simple index grouping by first letter (simplified implementation for mocks)
indexMap := make(map[string]model.Artists)
for _, artist := range artists {
key := "#"
if len(artist.Name) > 0 {
key = string(artist.Name[0])
}
indexMap[key] = append(indexMap[key], artist)
}
var result model.ArtistIndexes
for k, artists := range indexMap {
result = append(result, model.ArtistIndex{ID: k, Artists: artists})
}
return result, nil
}
func (m *MockArtistRepo) Search(q string, options ...model.QueryOptions) (model.Artists, error) {
if len(options) > 0 {
m.Options = options[0]
}
if m.Err {
return nil, errors.New("unexpected error")
}
// Simple mock implementation - just return all artists for testing
allArtists, err := m.GetAll()
return allArtists, err
}
var _ model.ArtistRepository = (*MockArtistRepo)(nil)