fix(filters): ensure sort by priority (#1325)

* fix(filters): sort filters from filtersMap

* fix(filters): use slices.SortStableFunc and fix tests

* fix(filters): add local cmp pkg before go 1.21
This commit is contained in:
ze0s 2023-12-31 14:59:12 +01:00 committed by GitHub
parent c060814022
commit a0a81ed34c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 125 additions and 6 deletions

View file

@ -12,11 +12,13 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/pkg/cmp"
"github.com/autobrr/autobrr/pkg/errors"
sq "github.com/Masterminds/squirrel"
"github.com/lib/pq"
"github.com/rs/zerolog"
"golang.org/x/exp/slices"
)
type FilterRepo struct {
@ -713,6 +715,12 @@ func (r *FilterRepo) findByIndexerIdentifier(ctx context.Context, indexer string
filters = append(filters, filter)
}
// the filterMap messes up the order, so we need to sort the filters slice
slices.SortStableFunc(filters, func(a, b *domain.Filter) int {
// TODO remove with Go 1.21 and use std lib cmp
return cmp.Compare(b.Priority, a.Priority)
})
return filters, nil
}

View file

@ -467,20 +467,63 @@ func TestFilterRepo_FindByIndexerIdentifier(t *testing.T) {
log := setupLoggerForTest()
repo := NewFilterRepo(log, db)
indexerRepo := NewIndexerRepo(log, db)
mockData := getMockFilter()
//mockData := getMockFilter()
indexerMockData := getMockIndexer()
filtersData := []*domain.Filter{
{
Enabled: true,
Name: "filter 1",
Priority: 20,
Resolutions: []string{},
Codecs: []string{},
Sources: []string{},
Containers: []string{},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
},
{
Enabled: true,
Name: "filter 2",
Priority: 30,
Resolutions: []string{},
Codecs: []string{},
Sources: []string{},
Containers: []string{},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
},
{
Enabled: true,
Name: "filter 20",
Priority: 100,
Resolutions: []string{},
Codecs: []string{},
Sources: []string{},
Containers: []string{},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
},
}
t.Run(fmt.Sprintf("FindByIndexerIdentifier_Succeeds [%s]", dbType), func(t *testing.T) {
// Setup
err := repo.Store(context.Background(), mockData)
assert.NoError(t, err)
indexer, err := indexerRepo.Store(context.Background(), indexerMockData)
assert.NoError(t, err)
assert.NotNil(t, indexer)
err = repo.StoreIndexerConnection(context.Background(), mockData.ID, int(indexer.ID))
for _, filter := range filtersData {
filter := filter
err := repo.Store(context.Background(), filter)
assert.NoError(t, err)
err = repo.StoreIndexerConnection(context.Background(), filter.ID, int(indexer.ID))
assert.NoError(t, err)
}
filtersList, err := repo.ListFilters(context.Background())
assert.NoError(t, err)
assert.NotEmpty(t, filtersList)
// Execute
filters, err := repo.FindByIndexerIdentifier(context.Background(), indexerMockData.Identifier)
@ -488,9 +531,18 @@ func TestFilterRepo_FindByIndexerIdentifier(t *testing.T) {
assert.NotNil(t, filters)
assert.NotEmpty(t, filters)
assert.Equal(t, filters[0].Priority, int32(100))
assert.Equal(t, filters[1].Priority, int32(30))
assert.Equal(t, filters[2].Priority, int32(20))
// Cleanup
_ = indexerRepo.Delete(context.Background(), int(indexer.ID))
_ = repo.Delete(context.Background(), mockData.ID)
for _, filter := range filtersData {
filter := filter
_ = repo.Delete(context.Background(), filter.ID)
}
})
t.Run(fmt.Sprintf("FindByIndexerIdentifier_Fails_Invalid_Identifier [%s]", dbType), func(t *testing.T) {