dashboard_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/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. Convey("Should return dashboard model", func() {
  27. So(savedDash.Title, ShouldEqual, "test dash 23")
  28. So(savedDash.Slug, ShouldEqual, "test-dash-23")
  29. So(savedDash.Id, ShouldNotEqual, 0)
  30. })
  31. Convey("Should be able to get dashboard", func() {
  32. query := m.GetDashboardQuery{
  33. Slug: "test-dash-23",
  34. OrgId: 1,
  35. }
  36. err := GetDashboard(&query)
  37. So(err, ShouldBeNil)
  38. So(query.Result.Title, ShouldEqual, "test dash 23")
  39. So(query.Result.Slug, ShouldEqual, "test-dash-23")
  40. })
  41. Convey("Should return error if no dashboard is updated", func() {
  42. cmd := m.SaveDashboardCommand{
  43. OrgId: 1,
  44. Overwrite: true,
  45. Dashboard: map[string]interface{}{
  46. "id": float64(123412321),
  47. "title": "Expect error",
  48. "tags": []interface{}{},
  49. },
  50. }
  51. err := SaveDashboard(&cmd)
  52. So(err, ShouldNotBeNil)
  53. })
  54. Convey("Should not be able to overwrite dashboard in another org", func() {
  55. query := m.GetDashboardQuery{Slug: "test-dash-23", OrgId: 1}
  56. GetDashboard(&query)
  57. cmd := m.SaveDashboardCommand{
  58. OrgId: 2,
  59. Overwrite: true,
  60. Dashboard: map[string]interface{}{
  61. "id": float64(query.Result.Id),
  62. "title": "Expect error",
  63. "tags": []interface{}{},
  64. },
  65. }
  66. err := SaveDashboard(&cmd)
  67. So(err, ShouldNotBeNil)
  68. })
  69. Convey("Should be able to search for dashboard", func() {
  70. query := search.FindPersistedDashboardsQuery{
  71. Title: "test",
  72. OrgId: 1,
  73. }
  74. err := SearchDashboards(&query)
  75. So(err, ShouldBeNil)
  76. So(len(query.Result), ShouldEqual, 1)
  77. hit := query.Result[0]
  78. So(len(hit.Tags), ShouldEqual, 2)
  79. })
  80. Convey("Should be able to search for dashboards using tags", func() {
  81. query1 := search.FindPersistedDashboardsQuery{Tag: "webapp", OrgId: 1}
  82. query2 := search.FindPersistedDashboardsQuery{Tag: "tagdoesnotexist", OrgId: 1}
  83. err := SearchDashboards(&query1)
  84. err = SearchDashboards(&query2)
  85. So(err, ShouldBeNil)
  86. So(len(query1.Result), ShouldEqual, 1)
  87. So(len(query2.Result), ShouldEqual, 0)
  88. })
  89. Convey("Should not be able to save dashboard with same name", func() {
  90. cmd := m.SaveDashboardCommand{
  91. OrgId: 1,
  92. Dashboard: map[string]interface{}{
  93. "id": nil,
  94. "title": "test dash 23",
  95. "tags": []interface{}{},
  96. },
  97. }
  98. err := SaveDashboard(&cmd)
  99. So(err, ShouldNotBeNil)
  100. })
  101. Convey("Should be able to get dashboard tags", func() {
  102. query := m.GetDashboardTagsQuery{OrgId: 1}
  103. err := GetDashboardTags(&query)
  104. So(err, ShouldBeNil)
  105. So(len(query.Result), ShouldEqual, 2)
  106. })
  107. Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() {
  108. starredDash := insertTestDashboard("starred dash", 1)
  109. StarDashboard(&m.StarDashboardCommand{
  110. DashboardId: starredDash.Id,
  111. UserId: 10,
  112. })
  113. StarDashboard(&m.StarDashboardCommand{
  114. DashboardId: savedDash.Id,
  115. UserId: 1,
  116. })
  117. Convey("Should be able to search for starred dashboards", func() {
  118. query := search.FindPersistedDashboardsQuery{OrgId: 1, UserId: 10, IsStarred: true}
  119. err := SearchDashboards(&query)
  120. So(err, ShouldBeNil)
  121. So(len(query.Result), ShouldEqual, 1)
  122. So(query.Result[0].Title, ShouldEqual, "starred dash")
  123. })
  124. })
  125. })
  126. })
  127. }