dashboard_test.go 4.6 KB

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