2015-01-05 10:04:29 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
2016-04-25 01:46:15 -05:00
|
|
|
"github.com/gosimple/slug"
|
2016-03-12 03:13:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2015-02-05 03:37:13 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2015-06-05 04:08:19 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/search"
|
2015-01-05 10:04:29 -06:00
|
|
|
)
|
|
|
|
|
2017-03-27 07:36:28 -05:00
|
|
|
func insertTestDashboard(title string, orgId int64, parentId int64, isFolder bool, tags ...interface{}) *m.Dashboard {
|
2015-02-04 04:35:59 -06:00
|
|
|
cmd := m.SaveDashboardCommand{
|
2017-03-27 07:36:28 -05:00
|
|
|
OrgId: orgId,
|
|
|
|
ParentId: parentId,
|
|
|
|
IsFolder: isFolder,
|
2016-03-12 03:13:49 -06:00
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
2015-02-04 04:35:59 -06:00
|
|
|
"id": nil,
|
|
|
|
"title": title,
|
|
|
|
"tags": tags,
|
2016-03-12 03:13:49 -06:00
|
|
|
}),
|
2015-02-04 04:35:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := SaveDashboard(&cmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
return cmd.Result
|
|
|
|
}
|
|
|
|
|
2015-01-05 10:04:29 -06:00
|
|
|
func TestDashboardDataAccess(t *testing.T) {
|
|
|
|
|
|
|
|
Convey("Testing DB", t, func() {
|
|
|
|
InitTestDB(t)
|
|
|
|
|
|
|
|
Convey("Given saved dashboard", func() {
|
2017-03-27 07:36:28 -05:00
|
|
|
savedFolder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp")
|
|
|
|
savedDash := insertTestDashboard("test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
|
|
|
|
insertTestDashboard("test dash 45", 1, savedFolder.Id, false, "prod")
|
|
|
|
insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
|
2015-01-05 10:04:29 -06:00
|
|
|
|
|
|
|
Convey("Should return dashboard model", func() {
|
|
|
|
So(savedDash.Title, ShouldEqual, "test dash 23")
|
|
|
|
So(savedDash.Slug, ShouldEqual, "test-dash-23")
|
|
|
|
So(savedDash.Id, ShouldNotEqual, 0)
|
2017-03-27 07:36:28 -05:00
|
|
|
So(savedDash.IsFolder, ShouldBeFalse)
|
|
|
|
So(savedDash.ParentId, ShouldBeGreaterThan, 0)
|
|
|
|
|
|
|
|
So(savedFolder.Title, ShouldEqual, "1 test dash folder")
|
|
|
|
So(savedFolder.Slug, ShouldEqual, "1-test-dash-folder")
|
|
|
|
So(savedFolder.Id, ShouldNotEqual, 0)
|
|
|
|
So(savedFolder.IsFolder, ShouldBeTrue)
|
|
|
|
So(savedFolder.ParentId, ShouldEqual, 0)
|
2015-01-05 10:04:29 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should be able to get dashboard", func() {
|
|
|
|
query := m.GetDashboardQuery{
|
2015-02-23 13:07:49 -06:00
|
|
|
Slug: "test-dash-23",
|
|
|
|
OrgId: 1,
|
2015-01-05 10:04:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := GetDashboard(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(query.Result.Title, ShouldEqual, "test dash 23")
|
|
|
|
So(query.Result.Slug, ShouldEqual, "test-dash-23")
|
2017-03-27 07:36:28 -05:00
|
|
|
So(query.Result.IsFolder, ShouldBeFalse)
|
2015-01-05 10:04:29 -06:00
|
|
|
})
|
|
|
|
|
2016-04-25 01:46:15 -05:00
|
|
|
Convey("Should be able to delete dashboard", func() {
|
2017-03-27 07:36:28 -05:00
|
|
|
insertTestDashboard("delete me", 1, 0, false, "delete this")
|
2016-04-25 01:46:15 -05:00
|
|
|
|
|
|
|
dashboardSlug := slug.Make("delete me")
|
|
|
|
|
|
|
|
err := DeleteDashboard(&m.DeleteDashboardCommand{
|
|
|
|
Slug: dashboardSlug,
|
|
|
|
OrgId: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
2015-05-04 00:46:46 -05:00
|
|
|
Convey("Should return error if no dashboard is updated", func() {
|
|
|
|
cmd := m.SaveDashboardCommand{
|
|
|
|
OrgId: 1,
|
|
|
|
Overwrite: true,
|
2016-03-12 03:13:49 -06:00
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
2015-05-04 00:46:46 -05:00
|
|
|
"id": float64(123412321),
|
|
|
|
"title": "Expect error",
|
|
|
|
"tags": []interface{}{},
|
2016-03-12 03:13:49 -06:00
|
|
|
}),
|
2015-05-04 00:46:46 -05:00
|
|
|
}
|
2015-05-04 01:19:29 -05:00
|
|
|
|
|
|
|
err := SaveDashboard(&cmd)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should not be able to overwrite dashboard in another org", func() {
|
|
|
|
query := m.GetDashboardQuery{Slug: "test-dash-23", OrgId: 1}
|
|
|
|
GetDashboard(&query)
|
|
|
|
|
|
|
|
cmd := m.SaveDashboardCommand{
|
|
|
|
OrgId: 2,
|
|
|
|
Overwrite: true,
|
2016-03-12 03:13:49 -06:00
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
2015-05-04 01:19:29 -05:00
|
|
|
"id": float64(query.Result.Id),
|
|
|
|
"title": "Expect error",
|
|
|
|
"tags": []interface{}{},
|
2016-03-12 03:13:49 -06:00
|
|
|
}),
|
2015-05-04 01:19:29 -05:00
|
|
|
}
|
2015-05-04 00:46:46 -05:00
|
|
|
|
|
|
|
err := SaveDashboard(&cmd)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
2017-05-08 15:23:25 -05:00
|
|
|
Convey("Should be able to search for dashboard and return in folder hierarchy", func() {
|
2015-05-13 06:36:13 -05:00
|
|
|
query := search.FindPersistedDashboardsQuery{
|
2016-03-05 05:26:21 -06:00
|
|
|
Title: "test dash 23",
|
2015-02-23 13:07:49 -06:00
|
|
|
OrgId: 1,
|
2017-06-01 16:30:31 -05:00
|
|
|
Mode: "tree",
|
2015-01-06 10:15:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := SearchDashboards(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(query.Result), ShouldEqual, 1)
|
2017-05-08 15:23:25 -05:00
|
|
|
hit := query.Result[0].Dashboards[0]
|
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
So(len(hit.Tags), ShouldEqual, 2)
|
2017-03-27 07:36:28 -05:00
|
|
|
So(hit.Type, ShouldEqual, search.DashHitDB)
|
|
|
|
So(hit.ParentId, ShouldBeGreaterThan, 0)
|
2017-05-08 15:23:25 -05:00
|
|
|
|
2017-03-27 07:36:28 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should be able to search for dashboard folder", func() {
|
|
|
|
query := search.FindPersistedDashboardsQuery{
|
|
|
|
Title: "1 test dash folder",
|
|
|
|
OrgId: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := SearchDashboards(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(query.Result), ShouldEqual, 1)
|
|
|
|
hit := query.Result[0]
|
|
|
|
So(hit.Type, ShouldEqual, search.DashHitFolder)
|
|
|
|
})
|
|
|
|
|
2017-06-01 16:30:31 -05:00
|
|
|
Convey("Should be able to search for a dashboard folder's children", func() {
|
|
|
|
query := search.FindPersistedDashboardsQuery{
|
|
|
|
OrgId: 1,
|
|
|
|
ParentId: savedFolder.Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := SearchDashboards(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(len(query.Result), ShouldEqual, 2)
|
|
|
|
hit := query.Result[0]
|
|
|
|
So(hit.Id, ShouldEqual, savedDash.Id)
|
|
|
|
})
|
|
|
|
|
2016-03-05 05:26:21 -06:00
|
|
|
Convey("Should be able to search for dashboard by dashboard ids", func() {
|
|
|
|
Convey("should be able to find two dashboards by id", func() {
|
|
|
|
query := search.FindPersistedDashboardsQuery{
|
2017-03-27 07:36:28 -05:00
|
|
|
DashboardIds: []int{2, 3},
|
2016-03-05 05:26:21 -06:00
|
|
|
OrgId: 1,
|
2017-06-01 16:30:31 -05:00
|
|
|
Mode: "tree",
|
2016-03-05 05:26:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := SearchDashboards(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2017-05-08 15:23:25 -05:00
|
|
|
So(len(query.Result[0].Dashboards), ShouldEqual, 2)
|
2016-03-05 05:26:21 -06:00
|
|
|
|
2017-05-08 15:23:25 -05:00
|
|
|
hit := query.Result[0].Dashboards[0]
|
2016-03-05 05:26:21 -06:00
|
|
|
So(len(hit.Tags), ShouldEqual, 2)
|
|
|
|
|
2017-05-08 15:23:25 -05:00
|
|
|
hit2 := query.Result[0].Dashboards[1]
|
2016-03-05 05:26:21 -06:00
|
|
|
So(len(hit2.Tags), ShouldEqual, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("DashboardIds that does not exists should not cause errors", func() {
|
|
|
|
query := search.FindPersistedDashboardsQuery{
|
|
|
|
DashboardIds: []int{1000},
|
|
|
|
OrgId: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := SearchDashboards(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result), ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-01-05 10:04:29 -06:00
|
|
|
Convey("Should not be able to save dashboard with same name", func() {
|
|
|
|
cmd := m.SaveDashboardCommand{
|
2015-02-23 13:07:49 -06:00
|
|
|
OrgId: 1,
|
2016-03-12 03:13:49 -06:00
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
2015-01-05 10:04:29 -06:00
|
|
|
"id": nil,
|
|
|
|
"title": "test dash 23",
|
2015-01-07 05:37:24 -06:00
|
|
|
"tags": []interface{}{},
|
2016-03-12 03:13:49 -06:00
|
|
|
}),
|
2015-01-05 10:04:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := SaveDashboard(&cmd)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
2017-05-31 09:53:12 -05:00
|
|
|
Convey("Should be able to update dashboard and remove parentId", func() {
|
|
|
|
cmd := m.SaveDashboardCommand{
|
|
|
|
OrgId: 1,
|
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
"title": "parentId",
|
|
|
|
"tags": []interface{}{},
|
|
|
|
}),
|
|
|
|
Overwrite: true,
|
|
|
|
ParentId: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := SaveDashboard(&cmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cmd.Result.ParentId, ShouldEqual, 2)
|
|
|
|
|
|
|
|
cmd = m.SaveDashboardCommand{
|
|
|
|
OrgId: 1,
|
|
|
|
Dashboard: simplejson.NewFromAny(map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
"title": "parentId",
|
|
|
|
"tags": []interface{}{},
|
|
|
|
}),
|
|
|
|
ParentId: 0,
|
|
|
|
Overwrite: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = SaveDashboard(&cmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
query := m.GetDashboardQuery{
|
|
|
|
Slug: cmd.Result.Slug,
|
|
|
|
OrgId: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = GetDashboard(&query)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(query.Result.ParentId, ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
Convey("Should be able to get dashboard tags", func() {
|
2015-02-23 13:07:49 -06:00
|
|
|
query := m.GetDashboardTagsQuery{OrgId: 1}
|
2015-01-05 10:04:29 -06:00
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
err := GetDashboardTags(&query)
|
|
|
|
So(err, ShouldBeNil)
|
2015-01-05 10:04:29 -06:00
|
|
|
|
2015-01-20 07:15:48 -06:00
|
|
|
So(len(query.Result), ShouldEqual, 2)
|
2015-01-07 05:37:24 -06:00
|
|
|
})
|
2015-02-04 04:35:59 -06:00
|
|
|
|
|
|
|
Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() {
|
2017-03-27 07:36:28 -05:00
|
|
|
starredDash := insertTestDashboard("starred dash", 1, 0, false)
|
2015-02-04 04:35:59 -06:00
|
|
|
StarDashboard(&m.StarDashboardCommand{
|
|
|
|
DashboardId: starredDash.Id,
|
|
|
|
UserId: 10,
|
|
|
|
})
|
|
|
|
|
|
|
|
StarDashboard(&m.StarDashboardCommand{
|
|
|
|
DashboardId: savedDash.Id,
|
|
|
|
UserId: 1,
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should be able to search for starred dashboards", func() {
|
2015-05-13 06:36:13 -05:00
|
|
|
query := search.FindPersistedDashboardsQuery{OrgId: 1, UserId: 10, IsStarred: true}
|
2015-02-04 04:35:59 -06:00
|
|
|
err := SearchDashboards(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(query.Result), ShouldEqual, 1)
|
|
|
|
So(query.Result[0].Title, ShouldEqual, "starred dash")
|
|
|
|
})
|
|
|
|
})
|
2015-01-07 05:37:24 -06:00
|
|
|
})
|
|
|
|
})
|
2015-01-05 10:04:29 -06:00
|
|
|
}
|