sqlstore: finish removing Find and SearchDashboards (#49347)

* chore: replace artisnal FakeDashboardService with generated mock

Maintaining a handcrafted FakeDashboardService is not sustainable now that we are in the process of moving the dashboard-related functions out of sqlstore.

* sqlstore: finish removing Find and SearchDashboards

Find and SearchDashboards were previously copied into the dashboard service. This commit completes that work, removing Find and SearchDashboards from the sqlstore and updating callers to use the dashboard service.

* dashboards: remove SearchDashboards from Store interface

SearchDashboards is a wrapper around FindDashboard that transforms the results, so it's been moved out of the Store entirely and the functionality moved into the Dashboard Service's search implementation.

The database tests depended heavily on the transformation, so I added testSearchDashboards, a copy of search dashboards, instead of (heavily) refactoring all the tests.
This commit is contained in:
Kristin Laemmert
2022-05-24 09:24:55 -04:00
committed by GitHub
parent 86871807d2
commit debbb8d59d
15 changed files with 212 additions and 304 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"sort"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/star"
"github.com/grafana/grafana/pkg/setting"
@@ -11,15 +12,16 @@ import (
"github.com/grafana/grafana/pkg/models"
)
func ProvideService(cfg *setting.Cfg, sqlstore *sqlstore.SQLStore, starService star.Service) *SearchService {
func ProvideService(cfg *setting.Cfg, sqlstore *sqlstore.SQLStore, starService star.Service, dashboardService dashboards.DashboardService) *SearchService {
s := &SearchService{
Cfg: cfg,
sortOptions: map[string]models.SortOption{
SortAlphaAsc.Name: SortAlphaAsc,
SortAlphaDesc.Name: SortAlphaDesc,
},
sqlstore: sqlstore,
starService: starService,
sqlstore: sqlstore,
starService: starService,
dashboardService: dashboardService,
}
return s
}
@@ -47,10 +49,11 @@ type Service interface {
}
type SearchService struct {
Cfg *setting.Cfg
sortOptions map[string]models.SortOption
sqlstore sqlstore.Store
starService star.Service
Cfg *setting.Cfg
sortOptions map[string]models.SortOption
sqlstore sqlstore.Store
starService star.Service
dashboardService dashboards.DashboardService
}
func (s *SearchService) SearchHandler(ctx context.Context, query *Query) error {
@@ -71,7 +74,7 @@ func (s *SearchService) SearchHandler(ctx context.Context, query *Query) error {
dashboardQuery.Sort = sortOpt
}
if err := s.sqlstore.SearchDashboards(ctx, &dashboardQuery); err != nil {
if err := s.dashboardService.SearchDashboards(ctx, &dashboardQuery); err != nil {
return err
}

View File

@@ -4,29 +4,37 @@ import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/grafana/grafana/pkg/services/star"
"github.com/grafana/grafana/pkg/services/star/startest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSearch_SortedResults(t *testing.T) {
ss := startest.NewStarServiceFake()
ms := mockstore.NewSQLStoreMock()
ms.ExpectedPersistedDashboards = models.HitList{
&models.Hit{ID: 16, Title: "CCAA", Type: "dash-db", Tags: []string{"BB", "AA"}},
&models.Hit{ID: 10, Title: "AABB", Type: "dash-db", Tags: []string{"CC", "AA"}},
&models.Hit{ID: 15, Title: "BBAA", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
&models.Hit{ID: 25, Title: "bbAAa", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
&models.Hit{ID: 17, Title: "FOLDER", Type: "dash-folder"},
}
ds := dashboards.NewFakeDashboardService(t)
ds.On("SearchDashboards", mock.Anything, mock.AnythingOfType("*models.FindPersistedDashboardsQuery")).Run(func(args mock.Arguments) {
q := args.Get(1).(*models.FindPersistedDashboardsQuery)
q.Result = models.HitList{
&models.Hit{ID: 16, Title: "CCAA", Type: "dash-db", Tags: []string{"BB", "AA"}},
&models.Hit{ID: 10, Title: "AABB", Type: "dash-db", Tags: []string{"CC", "AA"}},
&models.Hit{ID: 15, Title: "BBAA", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
&models.Hit{ID: 25, Title: "bbAAa", Type: "dash-db", Tags: []string{"EE", "AA", "BB"}},
&models.Hit{ID: 17, Title: "FOLDER", Type: "dash-folder"},
}
}).Return(nil)
ms.ExpectedSignedInUser = &models.SignedInUser{IsGrafanaAdmin: true}
ss.ExpectedUserStars = &star.GetUserStarsResult{UserStars: map[int64]bool{10: true, 12: true}}
svc := &SearchService{
sqlstore: ms,
starService: ss,
sqlstore: ms,
starService: ss,
dashboardService: ds,
}
query := &Query{