mirror of
https://github.com/grafana/grafana.git
synced 2025-08-03 05:08:36 +08:00
Dashboards: Split GetDashboardVersions method (#49967)
* Split GetDashboarVersions method * Add sqlstore dialect and tests * Fix signature of PAtchPreference * Add GetDialect to sqlstore and remove GetDashboardVersions * Add GetDialect to db interface * Implement List * add deleted test function * Remove GetDialect from sqlstore interface * Remove deleted method from mock * Refactor test
This commit is contained in:
@ -30,8 +30,8 @@ func TestIntegrationGetDashboardVersion(t *testing.T) {
|
||||
|
||||
res, err := dashVerStore.Get(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, query.DashboardID, savedDash.Id)
|
||||
require.Equal(t, query.Version, savedDash.Version)
|
||||
assert.Equal(t, query.DashboardID, savedDash.Id)
|
||||
assert.Equal(t, query.Version, savedDash.Version)
|
||||
|
||||
dashCmd := &models.Dashboard{
|
||||
Id: res.ID,
|
||||
@ -41,8 +41,8 @@ func TestIntegrationGetDashboardVersion(t *testing.T) {
|
||||
err = getDashboard(t, ss, dashCmd)
|
||||
require.Nil(t, err)
|
||||
|
||||
require.EqualValues(t, dashCmd.Data.Get("uid"), res.Data.Get("uid"))
|
||||
require.EqualValues(t, dashCmd.Data.Get("orgId"), res.Data.Get("orgId"))
|
||||
assert.EqualValues(t, dashCmd.Data.Get("uid"), res.Data.Get("uid"))
|
||||
assert.EqualValues(t, dashCmd.Data.Get("orgId"), res.Data.Get("orgId"))
|
||||
})
|
||||
|
||||
t.Run("Attempt to get a version that doesn't exist", func(t *testing.T) {
|
||||
@ -54,7 +54,7 @@ func TestIntegrationGetDashboardVersion(t *testing.T) {
|
||||
|
||||
_, err := dashVerStore.Get(context.Background(), &query)
|
||||
require.Error(t, err)
|
||||
require.Equal(t, models.ErrDashboardVersionNotFound, err)
|
||||
assert.Equal(t, models.ErrDashboardVersionNotFound, err)
|
||||
})
|
||||
}
|
||||
|
||||
@ -79,6 +79,44 @@ func TestIntegrationDeleteExpiredVersions(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntegrationListDashboardVersions(t *testing.T) {
|
||||
ss := sqlstore.InitTestDB(t)
|
||||
dashVerStore := sqlStore{db: ss, dialect: ss.Dialect}
|
||||
savedDash := insertTestDashboard(t, ss, "test dash 43", 1, 0, false, "diff-all")
|
||||
|
||||
t.Run("Get all versions for a given Dashboard ID", func(t *testing.T) {
|
||||
query := dashver.ListDashboardVersionsQuery{
|
||||
DashboardID: savedDash.Id,
|
||||
OrgID: 1,
|
||||
Limit: 1000,
|
||||
}
|
||||
|
||||
res, err := dashVerStore.List(context.Background(), &query)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 1, len(res))
|
||||
})
|
||||
|
||||
t.Run("Attempt to get the versions for a non-existent Dashboard ID", func(t *testing.T) {
|
||||
query := dashver.ListDashboardVersionsQuery{DashboardID: int64(999), OrgID: 1, Limit: 1000}
|
||||
|
||||
res, err := dashVerStore.List(context.Background(), &query)
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, dashver.ErrNoVersionsForDashboardID, err)
|
||||
assert.Equal(t, 0, len(res))
|
||||
})
|
||||
|
||||
t.Run("Get all versions for an updated dashboard", func(t *testing.T) {
|
||||
updateTestDashboard(t, ss, savedDash, map[string]interface{}{
|
||||
"tags": "different-tag",
|
||||
})
|
||||
query := dashver.ListDashboardVersionsQuery{DashboardID: savedDash.Id, OrgID: 1, Limit: 1000}
|
||||
res, err := dashVerStore.List(context.Background(), &query)
|
||||
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 2, len(res))
|
||||
})
|
||||
}
|
||||
|
||||
func getDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, dashboard *models.Dashboard) error {
|
||||
t.Helper()
|
||||
return sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
@ -95,6 +133,7 @@ func getDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, dashboard *models.D
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func insertTestDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, title string, orgId int64,
|
||||
folderId int64, isFolder bool, tags ...interface{}) *models.Dashboard {
|
||||
t.Helper()
|
||||
@ -149,3 +188,64 @@ func insertTestDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, title string
|
||||
|
||||
return dash
|
||||
}
|
||||
|
||||
func updateTestDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, dashboard *models.Dashboard, data map[string]interface{}) {
|
||||
t.Helper()
|
||||
|
||||
data["id"] = dashboard.Id
|
||||
|
||||
parentVersion := dashboard.Version
|
||||
|
||||
cmd := models.SaveDashboardCommand{
|
||||
OrgId: dashboard.OrgId,
|
||||
Overwrite: true,
|
||||
Dashboard: simplejson.NewFromAny(data),
|
||||
}
|
||||
var dash *models.Dashboard
|
||||
err := sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
var existing models.Dashboard
|
||||
dash = cmd.GetDashboardModel()
|
||||
dashWithIdExists, err := sess.Where("id=? AND org_id=?", dash.Id, dash.OrgId).Get(&existing)
|
||||
require.NoError(t, err)
|
||||
require.True(t, dashWithIdExists)
|
||||
|
||||
if dash.Version != existing.Version {
|
||||
dash.SetVersion(existing.Version)
|
||||
dash.Version = existing.Version
|
||||
}
|
||||
|
||||
dash.SetVersion(dash.Version + 1)
|
||||
dash.Created = time.Now()
|
||||
dash.Updated = time.Now()
|
||||
dash.Id = dashboard.Id
|
||||
dash.Uid = util.GenerateShortUID()
|
||||
|
||||
_, err = sess.MustCols("folder_id").ID(dash.Id).Update(dash)
|
||||
return err
|
||||
})
|
||||
|
||||
require.Nil(t, err)
|
||||
|
||||
err = sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
dashVersion := &models.DashboardVersion{
|
||||
DashboardId: dash.Id,
|
||||
ParentVersion: parentVersion,
|
||||
RestoredFrom: cmd.RestoredFrom,
|
||||
Version: dash.Version,
|
||||
Created: time.Now(),
|
||||
CreatedBy: dash.UpdatedBy,
|
||||
Message: cmd.Message,
|
||||
Data: dash.Data,
|
||||
}
|
||||
|
||||
if affectedRows, err := sess.Insert(dashVersion); err != nil {
|
||||
return err
|
||||
} else if affectedRows == 0 {
|
||||
return models.ErrDashboardNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user