dashboard_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package sqlstore
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. m "github.com/torkelo/grafana-pro/pkg/models"
  6. )
  7. func TestDashboardDataAccess(t *testing.T) {
  8. Convey("Testing DB", t, func() {
  9. InitTestDB(t)
  10. Convey("Given saved dashboard", func() {
  11. var savedDash *m.Dashboard
  12. cmd := m.SaveDashboardCommand{
  13. AccountId: 1,
  14. Dashboard: map[string]interface{}{
  15. "id": nil,
  16. "title": "test dash 23",
  17. "tags": []interface{}{"prod", "webapp"},
  18. },
  19. }
  20. err := SaveDashboard(&cmd)
  21. So(err, ShouldBeNil)
  22. savedDash = cmd.Result
  23. Convey("Should return dashboard model", func() {
  24. So(savedDash.Title, ShouldEqual, "test dash 23")
  25. So(savedDash.Slug, ShouldEqual, "test-dash-23")
  26. So(savedDash.Id, ShouldNotEqual, 0)
  27. })
  28. Convey("Should be able to get dashboard", func() {
  29. query := m.GetDashboardQuery{
  30. Slug: "test-dash-23",
  31. AccountId: 1,
  32. }
  33. err := GetDashboard(&query)
  34. So(err, ShouldBeNil)
  35. So(query.Result.Title, ShouldEqual, "test dash 23")
  36. So(query.Result.Slug, ShouldEqual, "test-dash-23")
  37. })
  38. Convey("Should be able to search for dashboard", func() {
  39. query := m.SearchDashboardsQuery{
  40. Title: "%test%",
  41. AccountId: 1,
  42. }
  43. err := SearchDashboards(&query)
  44. So(err, ShouldBeNil)
  45. So(len(query.Result), ShouldEqual, 1)
  46. hit := query.Result[0]
  47. So(len(hit.Tags), ShouldEqual, 2)
  48. })
  49. Convey("Should be able to search for dashboards using tags", func() {
  50. query1 := m.SearchDashboardsQuery{Tag: "webapp", AccountId: 1}
  51. query2 := m.SearchDashboardsQuery{Tag: "tagdoesnotexist", AccountId: 1}
  52. err := SearchDashboards(&query1)
  53. err = SearchDashboards(&query2)
  54. So(err, ShouldBeNil)
  55. So(len(query1.Result), ShouldEqual, 1)
  56. So(len(query2.Result), ShouldEqual, 0)
  57. })
  58. Convey("Should not be able to save dashboard with same name", func() {
  59. cmd := m.SaveDashboardCommand{
  60. AccountId: 1,
  61. Dashboard: map[string]interface{}{
  62. "id": nil,
  63. "title": "test dash 23",
  64. "tags": []interface{}{},
  65. },
  66. }
  67. err := SaveDashboard(&cmd)
  68. So(err, ShouldNotBeNil)
  69. })
  70. Convey("Should be able to get dashboard tags", func() {
  71. query := m.GetDashboardTagsQuery{AccountId: 1}
  72. err := GetDashboardTags(&query)
  73. So(err, ShouldBeNil)
  74. So(len(query.Result), ShouldEqual, 2)
  75. })
  76. })
  77. })
  78. }