dashboard_test.go 4.5 KB

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