mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
respect query filter in FindDashboards (#46652)
This commit is contained in:
parent
bda3f860a8
commit
2bd4c9ccde
@ -11,14 +11,15 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/services/search"
|
"github.com/grafana/grafana/pkg/services/search"
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDashboardDataAccess(t *testing.T) {
|
func TestDashboardDataAccess(t *testing.T) {
|
||||||
@ -429,27 +430,62 @@ func TestDashboardDataAccessGivenPluginWithImportedDashboards(t *testing.T) {
|
|||||||
func TestDashboard_SortingOptions(t *testing.T) {
|
func TestDashboard_SortingOptions(t *testing.T) {
|
||||||
sqlStore := sqlstore.InitTestDB(t)
|
sqlStore := sqlstore.InitTestDB(t)
|
||||||
dashboardStore := ProvideDashboardStore(sqlStore)
|
dashboardStore := ProvideDashboardStore(sqlStore)
|
||||||
// insertTestDashboard uses GoConvey's assertions. Workaround.
|
|
||||||
t.Run("test with multiple sorting options", func(t *testing.T) {
|
dashB := insertTestDashboard(t, dashboardStore, "Beta", 1, 0, false)
|
||||||
sqlStore := sqlstore.InitTestDB(t)
|
dashA := insertTestDashboard(t, dashboardStore, "Alfa", 1, 0, false)
|
||||||
dashB := insertTestDashboard(t, dashboardStore, "Beta", 1, 0, false)
|
assert.NotZero(t, dashA.Id)
|
||||||
dashA := insertTestDashboard(t, dashboardStore, "Alfa", 1, 0, false)
|
assert.Less(t, dashB.Id, dashA.Id)
|
||||||
assert.NotZero(t, dashA.Id)
|
qNoSort := &search.FindPersistedDashboardsQuery{
|
||||||
assert.Less(t, dashB.Id, dashA.Id)
|
SignedInUser: &models.SignedInUser{OrgId: 1, UserId: 1, OrgRole: models.ROLE_ADMIN},
|
||||||
q := &search.FindPersistedDashboardsQuery{
|
}
|
||||||
SignedInUser: &models.SignedInUser{OrgId: 1, UserId: 1, OrgRole: models.ROLE_ADMIN},
|
dashboards, err := sqlStore.FindDashboards(context.Background(), qNoSort)
|
||||||
// adding two sorting options (silly no-op example, but it'll complicate the query)
|
require.NoError(t, err)
|
||||||
Filters: []interface{}{
|
require.Len(t, dashboards, 2)
|
||||||
searchstore.TitleSorter{},
|
assert.Equal(t, dashA.Id, dashboards[0].ID)
|
||||||
|
assert.Equal(t, dashB.Id, dashboards[1].ID)
|
||||||
|
|
||||||
|
qSort := &search.FindPersistedDashboardsQuery{
|
||||||
|
SignedInUser: &models.SignedInUser{OrgId: 1, UserId: 1, OrgRole: models.ROLE_ADMIN},
|
||||||
|
Sort: search.SortOption{
|
||||||
|
Filter: []search.SortOptionFilter{
|
||||||
searchstore.TitleSorter{Descending: true},
|
searchstore.TitleSorter{Descending: true},
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
dashboards, err := sqlStore.FindDashboards(context.Background(), q)
|
}
|
||||||
require.NoError(t, err)
|
dashboards, err = sqlStore.FindDashboards(context.Background(), qSort)
|
||||||
require.Len(t, dashboards, 2)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, dashA.Id, dashboards[0].ID)
|
require.Len(t, dashboards, 2)
|
||||||
assert.Equal(t, dashB.Id, dashboards[1].ID)
|
assert.Equal(t, dashB.Id, dashboards[0].ID)
|
||||||
})
|
assert.Equal(t, dashA.Id, dashboards[1].ID)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDashboard_Filter(t *testing.T) {
|
||||||
|
sqlStore := sqlstore.InitTestDB(t)
|
||||||
|
dashboardStore := ProvideDashboardStore(sqlStore)
|
||||||
|
insertTestDashboard(t, dashboardStore, "Alfa", 1, 0, false)
|
||||||
|
dashB := insertTestDashboard(t, dashboardStore, "Beta", 1, 0, false)
|
||||||
|
qNoFilter := &search.FindPersistedDashboardsQuery{
|
||||||
|
SignedInUser: &models.SignedInUser{OrgId: 1, UserId: 1, OrgRole: models.ROLE_ADMIN},
|
||||||
|
}
|
||||||
|
dashboards, err := sqlStore.FindDashboards(context.Background(), qNoFilter)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, dashboards, 2)
|
||||||
|
|
||||||
|
qFilter := &search.FindPersistedDashboardsQuery{
|
||||||
|
SignedInUser: &models.SignedInUser{OrgId: 1, UserId: 1, OrgRole: models.ROLE_ADMIN},
|
||||||
|
Filters: []interface{}{
|
||||||
|
searchstore.TitleFilter{
|
||||||
|
Dialect: sqlStore.Dialect,
|
||||||
|
Title: "Beta",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
dashboards, err = sqlStore.FindDashboards(context.Background(), qFilter)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, dashboards, 1)
|
||||||
|
assert.Equal(t, dashB.Id, dashboards[0].ID)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertTestRule(t *testing.T, sqlStore *sqlstore.SQLStore, foderOrgID int64, folderUID string) {
|
func insertTestRule(t *testing.T, sqlStore *sqlstore.SQLStore, foderOrgID int64, folderUID string) {
|
||||||
|
@ -104,6 +104,8 @@ func (ss *SQLStore) FindDashboards(ctx context.Context, query *search.FindPersis
|
|||||||
filters = append(filters, filter)
|
filters = append(filters, filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filters = append(filters, query.Filters...)
|
||||||
|
|
||||||
if query.OrgId != 0 {
|
if query.OrgId != 0 {
|
||||||
filters = append(filters, searchstore.OrgFilter{OrgId: query.OrgId})
|
filters = append(filters, searchstore.OrgFilter{OrgId: query.OrgId})
|
||||||
} else if query.SignedInUser.OrgId != 0 {
|
} else if query.SignedInUser.OrgId != 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user