dashboard_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package sqlstore
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "github.com/gosimple/slug"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/search"
  9. )
  10. func insertTestDashboard(title string, orgId int64, tags ...interface{}) *m.Dashboard {
  11. cmd := m.SaveDashboardCommand{
  12. OrgId: orgId,
  13. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  14. "id": nil,
  15. "title": title,
  16. "tags": tags,
  17. }),
  18. }
  19. err := SaveDashboard(&cmd)
  20. So(err, ShouldBeNil)
  21. return cmd.Result
  22. }
  23. func TestDashboardDataAccess(t *testing.T) {
  24. Convey("Testing DB", t, func() {
  25. InitTestDB(t)
  26. Convey("Given saved dashboard", func() {
  27. savedDash := insertTestDashboard("test dash 23", 1, "prod", "webapp")
  28. insertTestDashboard("test dash 45", 1, "prod")
  29. insertTestDashboard("test dash 67", 1, "prod", "webapp")
  30. Convey("Should return dashboard model", func() {
  31. So(savedDash.Title, ShouldEqual, "test dash 23")
  32. So(savedDash.Slug, ShouldEqual, "test-dash-23")
  33. So(savedDash.Id, ShouldNotEqual, 0)
  34. })
  35. Convey("Should be able to get dashboard", func() {
  36. query := m.GetDashboardQuery{
  37. Slug: "test-dash-23",
  38. OrgId: 1,
  39. }
  40. err := GetDashboard(&query)
  41. So(err, ShouldBeNil)
  42. So(query.Result.Title, ShouldEqual, "test dash 23")
  43. So(query.Result.Slug, ShouldEqual, "test-dash-23")
  44. })
  45. Convey("Should be able to delete dashboard", func() {
  46. insertTestDashboard("delete me", 1, "delete this")
  47. dashboardSlug := slug.Make("delete me")
  48. err := DeleteDashboard(&m.DeleteDashboardCommand{
  49. Slug: dashboardSlug,
  50. OrgId: 1,
  51. })
  52. So(err, ShouldBeNil)
  53. })
  54. Convey("Should return error if no dashboard is updated", func() {
  55. cmd := m.SaveDashboardCommand{
  56. OrgId: 1,
  57. Overwrite: true,
  58. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  59. "id": float64(123412321),
  60. "title": "Expect error",
  61. "tags": []interface{}{},
  62. }),
  63. }
  64. err := SaveDashboard(&cmd)
  65. So(err, ShouldNotBeNil)
  66. })
  67. Convey("Should not be able to overwrite dashboard in another org", func() {
  68. query := m.GetDashboardQuery{Slug: "test-dash-23", OrgId: 1}
  69. GetDashboard(&query)
  70. cmd := m.SaveDashboardCommand{
  71. OrgId: 2,
  72. Overwrite: true,
  73. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  74. "id": float64(query.Result.Id),
  75. "title": "Expect error",
  76. "tags": []interface{}{},
  77. }),
  78. }
  79. err := SaveDashboard(&cmd)
  80. So(err, ShouldNotBeNil)
  81. })
  82. Convey("Should be able to search for dashboard", func() {
  83. query := search.FindPersistedDashboardsQuery{
  84. Title: "test dash 23",
  85. OrgId: 1,
  86. }
  87. err := SearchDashboards(&query)
  88. So(err, ShouldBeNil)
  89. So(len(query.Result), ShouldEqual, 1)
  90. hit := query.Result[0]
  91. So(len(hit.Tags), ShouldEqual, 2)
  92. })
  93. Convey("Should be able to search for dashboard by dashboard ids", func() {
  94. Convey("should be able to find two dashboards by id", func() {
  95. query := search.FindPersistedDashboardsQuery{
  96. DashboardIds: []int{1, 2},
  97. OrgId: 1,
  98. }
  99. err := SearchDashboards(&query)
  100. So(err, ShouldBeNil)
  101. So(len(query.Result), ShouldEqual, 2)
  102. hit := query.Result[0]
  103. So(len(hit.Tags), ShouldEqual, 2)
  104. hit2 := query.Result[1]
  105. So(len(hit2.Tags), ShouldEqual, 1)
  106. })
  107. Convey("DashboardIds that does not exists should not cause errors", func() {
  108. query := search.FindPersistedDashboardsQuery{
  109. DashboardIds: []int{1000},
  110. OrgId: 1,
  111. }
  112. err := SearchDashboards(&query)
  113. So(err, ShouldBeNil)
  114. So(len(query.Result), ShouldEqual, 0)
  115. })
  116. })
  117. Convey("Should not be able to save dashboard with same name", func() {
  118. cmd := m.SaveDashboardCommand{
  119. OrgId: 1,
  120. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  121. "id": nil,
  122. "title": "test dash 23",
  123. "tags": []interface{}{},
  124. }),
  125. }
  126. err := SaveDashboard(&cmd)
  127. So(err, ShouldNotBeNil)
  128. })
  129. Convey("Should be able to get dashboard tags", func() {
  130. query := m.GetDashboardTagsQuery{OrgId: 1}
  131. err := GetDashboardTags(&query)
  132. So(err, ShouldBeNil)
  133. So(len(query.Result), ShouldEqual, 2)
  134. })
  135. Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() {
  136. starredDash := insertTestDashboard("starred dash", 1)
  137. StarDashboard(&m.StarDashboardCommand{
  138. DashboardId: starredDash.Id,
  139. UserId: 10,
  140. })
  141. StarDashboard(&m.StarDashboardCommand{
  142. DashboardId: savedDash.Id,
  143. UserId: 1,
  144. })
  145. Convey("Should be able to search for starred dashboards", func() {
  146. query := search.FindPersistedDashboardsQuery{OrgId: 1, UserId: 10, IsStarred: true}
  147. err := SearchDashboards(&query)
  148. So(err, ShouldBeNil)
  149. So(len(query.Result), ShouldEqual, 1)
  150. So(query.Result[0].Title, ShouldEqual, "starred dash")
  151. })
  152. })
  153. })
  154. })
  155. }