2022-05-25 03:41:51 -05:00
|
|
|
package dashverimpl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-31 04:56:05 -05:00
|
|
|
"errors"
|
2022-05-25 03:41:51 -05:00
|
|
|
"testing"
|
|
|
|
|
2022-12-27 10:17:24 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-05-25 03:41:51 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
|
2022-05-31 04:56:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2022-05-25 03:41:51 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2022-12-27 10:17:24 -06:00
|
|
|
require.Equal(t, dashboard.ToDTO(""), dashboardVersion)
|
2022-05-25 03:41:51 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-31 04:56:05 -05:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-02 08:59:05 -05:00
|
|
|
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{}
|
2022-12-27 10:17:24 -06:00
|
|
|
dashboardVersionStore.ExpectedListVersions = []*dashver.DashboardVersion{{}}
|
2022-06-02 08:59:05 -05:00
|
|
|
res, err := dashboardVersionService.List(context.Background(), &query)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, 1, len(res))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-31 04:56:05 -05:00
|
|
|
type FakeDashboardVersionStore struct {
|
2022-05-25 03:41:51 -05:00
|
|
|
ExpectedDashboardVersion *dashver.DashboardVersion
|
2022-05-31 04:56:05 -05:00
|
|
|
ExptectedDeletedVersions int64
|
|
|
|
ExpectedVersions []interface{}
|
2022-12-27 10:17:24 -06:00
|
|
|
ExpectedListVersions []*dashver.DashboardVersion
|
2022-05-25 03:41:51 -05:00
|
|
|
ExpectedError error
|
|
|
|
}
|
|
|
|
|
2022-05-31 04:56:05 -05:00
|
|
|
func newDashboardVersionStoreFake() *FakeDashboardVersionStore {
|
|
|
|
return &FakeDashboardVersionStore{}
|
2022-05-25 03:41:51 -05:00
|
|
|
}
|
|
|
|
|
2022-05-31 04:56:05 -05:00
|
|
|
func (f *FakeDashboardVersionStore) Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error) {
|
2022-05-25 03:41:51 -05:00
|
|
|
return f.ExpectedDashboardVersion, f.ExpectedError
|
|
|
|
}
|
2022-05-31 04:56:05 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-06-02 08:59:05 -05:00
|
|
|
|
2022-12-27 10:17:24 -06:00
|
|
|
func (f *FakeDashboardVersionStore) List(ctx context.Context, query *dashver.ListDashboardVersionsQuery) ([]*dashver.DashboardVersion, error) {
|
2022-06-02 08:59:05 -05:00
|
|
|
return f.ExpectedListVersions, f.ExpectedError
|
|
|
|
}
|