mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
The DashboardVersion struct is the database object; the DashboardVersionDTO is the object that should be sent to the API layer. In the future I'd like to move DashboardVersion to dashverimpl and un-export it, but there are a few places that Insert directly into that table, not all of which are test fixtures, so that should wait until we clean up at least the DashboardService's use of it.
97 lines
3.6 KiB
Go
97 lines
3.6 KiB
Go
package dashverimpl
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
)
|
|
|
|
func TestDashboardVersionService(t *testing.T) {
|
|
dashboardVersionStore := newDashboardVersionStoreFake()
|
|
dashboardVersionService := Service{store: dashboardVersionStore}
|
|
|
|
t.Run("Get dashboard version", func(t *testing.T) {
|
|
dashboard := &dashver.DashboardVersion{
|
|
ID: 11,
|
|
Data: &simplejson.Json{},
|
|
}
|
|
dashboardVersionStore.ExpectedDashboardVersion = dashboard
|
|
dashboardVersion, err := dashboardVersionService.Get(context.Background(), &dashver.GetDashboardVersionQuery{})
|
|
require.NoError(t, err)
|
|
require.Equal(t, dashboard.ToDTO(""), dashboardVersion)
|
|
})
|
|
}
|
|
|
|
func TestDeleteExpiredVersions(t *testing.T) {
|
|
versionsToKeep := 5
|
|
setting.DashboardVersionsToKeep = versionsToKeep
|
|
|
|
dashboardVersionStore := newDashboardVersionStoreFake()
|
|
dashboardVersionService := Service{store: dashboardVersionStore}
|
|
|
|
t.Run("Don't delete anything if there are no expired versions", func(t *testing.T) {
|
|
err := dashboardVersionService.DeleteExpired(context.Background(), &dashver.DeleteExpiredVersionsCommand{DeletedRows: 4})
|
|
require.Nil(t, err)
|
|
})
|
|
|
|
t.Run("Clean up old dashboard versions successfully", func(t *testing.T) {
|
|
dashboardVersionStore.ExptectedDeletedVersions = 4
|
|
dashboardVersionStore.ExpectedVersions = []interface{}{1, 2, 3, 4}
|
|
err := dashboardVersionService.DeleteExpired(context.Background(), &dashver.DeleteExpiredVersionsCommand{DeletedRows: 4})
|
|
require.Nil(t, err)
|
|
})
|
|
|
|
t.Run("Clean up old dashboard versions with error", func(t *testing.T) {
|
|
dashboardVersionStore.ExpectedError = errors.New("some error")
|
|
err := dashboardVersionService.DeleteExpired(context.Background(), &dashver.DeleteExpiredVersionsCommand{DeletedRows: 4})
|
|
require.NotNil(t, err)
|
|
})
|
|
}
|
|
|
|
func TestListDashboardVersions(t *testing.T) {
|
|
dashboardVersionStore := newDashboardVersionStoreFake()
|
|
dashboardVersionService := Service{store: dashboardVersionStore}
|
|
|
|
t.Run("Get all versions for a given Dashboard ID", func(t *testing.T) {
|
|
query := dashver.ListDashboardVersionsQuery{}
|
|
dashboardVersionStore.ExpectedListVersions = []*dashver.DashboardVersion{{}}
|
|
res, err := dashboardVersionService.List(context.Background(), &query)
|
|
require.Nil(t, err)
|
|
require.Equal(t, 1, len(res))
|
|
})
|
|
}
|
|
|
|
type FakeDashboardVersionStore struct {
|
|
ExpectedDashboardVersion *dashver.DashboardVersion
|
|
ExptectedDeletedVersions int64
|
|
ExpectedVersions []interface{}
|
|
ExpectedListVersions []*dashver.DashboardVersion
|
|
ExpectedError error
|
|
}
|
|
|
|
func newDashboardVersionStoreFake() *FakeDashboardVersionStore {
|
|
return &FakeDashboardVersionStore{}
|
|
}
|
|
|
|
func (f *FakeDashboardVersionStore) Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error) {
|
|
return f.ExpectedDashboardVersion, f.ExpectedError
|
|
}
|
|
|
|
func (f *FakeDashboardVersionStore) GetBatch(ctx context.Context, cmd *dashver.DeleteExpiredVersionsCommand, perBatch int, versionsToKeep int) ([]interface{}, error) {
|
|
return f.ExpectedVersions, f.ExpectedError
|
|
}
|
|
|
|
func (f *FakeDashboardVersionStore) DeleteBatch(ctx context.Context, cmd *dashver.DeleteExpiredVersionsCommand, versionIdsToDelete []interface{}) (int64, error) {
|
|
return f.ExptectedDeletedVersions, f.ExpectedError
|
|
}
|
|
|
|
func (f *FakeDashboardVersionStore) List(ctx context.Context, query *dashver.ListDashboardVersionsQuery) ([]*dashver.DashboardVersion, error) {
|
|
return f.ExpectedListVersions, f.ExpectedError
|
|
}
|