dashboard_test.go 3.9 KB

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