dashboard_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. 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 not be able to save dashboard with same name", func() {
  81. cmd := m.SaveDashboardCommand{
  82. OrgId: 1,
  83. Dashboard: map[string]interface{}{
  84. "id": nil,
  85. "title": "test dash 23",
  86. "tags": []interface{}{},
  87. },
  88. }
  89. err := SaveDashboard(&cmd)
  90. So(err, ShouldNotBeNil)
  91. })
  92. Convey("Should be able to get dashboard tags", func() {
  93. query := m.GetDashboardTagsQuery{OrgId: 1}
  94. err := GetDashboardTags(&query)
  95. So(err, ShouldBeNil)
  96. So(len(query.Result), ShouldEqual, 2)
  97. })
  98. Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() {
  99. starredDash := insertTestDashboard("starred dash", 1)
  100. StarDashboard(&m.StarDashboardCommand{
  101. DashboardId: starredDash.Id,
  102. UserId: 10,
  103. })
  104. StarDashboard(&m.StarDashboardCommand{
  105. DashboardId: savedDash.Id,
  106. UserId: 1,
  107. })
  108. Convey("Should be able to search for starred dashboards", func() {
  109. query := search.FindPersistedDashboardsQuery{OrgId: 1, UserId: 10, IsStarred: true}
  110. err := SearchDashboards(&query)
  111. So(err, ShouldBeNil)
  112. So(len(query.Result), ShouldEqual, 1)
  113. So(query.Result[0].Title, ShouldEqual, "starred dash")
  114. })
  115. })
  116. })
  117. })
  118. }