dashboard_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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, accountId int64, tags ...interface{}) *m.Dashboard {
  8. cmd := m.SaveDashboardCommand{
  9. AccountId: accountId,
  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. AccountId: 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 be able to search for dashboard", func() {
  41. query := m.SearchDashboardsQuery{
  42. Title: "%test%",
  43. AccountId: 1,
  44. }
  45. err := SearchDashboards(&query)
  46. So(err, ShouldBeNil)
  47. So(len(query.Result), ShouldEqual, 1)
  48. hit := query.Result[0]
  49. So(len(hit.Tags), ShouldEqual, 2)
  50. })
  51. Convey("Should be able to search for dashboards using tags", func() {
  52. query1 := m.SearchDashboardsQuery{Tag: "webapp", AccountId: 1}
  53. query2 := m.SearchDashboardsQuery{Tag: "tagdoesnotexist", AccountId: 1}
  54. err := SearchDashboards(&query1)
  55. err = SearchDashboards(&query2)
  56. So(err, ShouldBeNil)
  57. So(len(query1.Result), ShouldEqual, 1)
  58. So(len(query2.Result), ShouldEqual, 0)
  59. })
  60. Convey("Should not be able to save dashboard with same name", func() {
  61. cmd := m.SaveDashboardCommand{
  62. AccountId: 1,
  63. Dashboard: map[string]interface{}{
  64. "id": nil,
  65. "title": "test dash 23",
  66. "tags": []interface{}{},
  67. },
  68. }
  69. err := SaveDashboard(&cmd)
  70. So(err, ShouldNotBeNil)
  71. })
  72. Convey("Should be able to get dashboard tags", func() {
  73. query := m.GetDashboardTagsQuery{AccountId: 1}
  74. err := GetDashboardTags(&query)
  75. So(err, ShouldBeNil)
  76. So(len(query.Result), ShouldEqual, 2)
  77. })
  78. Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() {
  79. starredDash := insertTestDashboard("starred dash", 1)
  80. StarDashboard(&m.StarDashboardCommand{
  81. DashboardId: starredDash.Id,
  82. UserId: 10,
  83. })
  84. StarDashboard(&m.StarDashboardCommand{
  85. DashboardId: savedDash.Id,
  86. UserId: 1,
  87. })
  88. Convey("Should be able to search for starred dashboards", func() {
  89. query := m.SearchDashboardsQuery{AccountId: 1, UserId: 10, IsStarred: true}
  90. err := SearchDashboards(&query)
  91. So(err, ShouldBeNil)
  92. So(len(query.Result), ShouldEqual, 1)
  93. So(query.Result[0].Title, ShouldEqual, "starred dash")
  94. })
  95. })
  96. })
  97. })
  98. }