dashboard_version_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package sqlstore
  2. import (
  3. "reflect"
  4. "testing"
  5. . "github.com/smartystreets/goconvey/convey"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. m "github.com/grafana/grafana/pkg/models"
  8. )
  9. func updateTestDashboard(dashboard *m.Dashboard, data map[string]interface{}) {
  10. data["title"] = dashboard.Title
  11. saveCmd := m.SaveDashboardCommand{
  12. OrgId: dashboard.OrgId,
  13. Overwrite: true,
  14. Dashboard: simplejson.NewFromAny(data),
  15. }
  16. err := SaveDashboard(&saveCmd)
  17. So(err, ShouldBeNil)
  18. }
  19. func TestGetDashboardVersion(t *testing.T) {
  20. Convey("Testing dashboard version retrieval", t, func() {
  21. InitTestDB(t)
  22. Convey("Get a Dashboard ID and version ID", func() {
  23. savedDash := insertTestDashboard("test dash 26", 1, "diff")
  24. query := m.GetDashboardVersionQuery{
  25. DashboardId: savedDash.Id,
  26. Version: savedDash.Version,
  27. }
  28. err := GetDashboardVersion(&query)
  29. So(err, ShouldBeNil)
  30. So(savedDash.Id, ShouldEqual, query.DashboardId)
  31. So(savedDash.Version, ShouldEqual, query.Version)
  32. dashCmd := m.GetDashboardQuery{
  33. OrgId: savedDash.OrgId,
  34. Slug: savedDash.Slug,
  35. }
  36. err = GetDashboard(&dashCmd)
  37. So(err, ShouldBeNil)
  38. eq := reflect.DeepEqual(dashCmd.Result.Data, query.Result.Data)
  39. So(eq, ShouldEqual, true)
  40. })
  41. Convey("Attempt to get a version that doesn't exist", func() {
  42. query := m.GetDashboardVersionQuery{
  43. DashboardId: int64(999),
  44. Version: 123,
  45. }
  46. err := GetDashboardVersion(&query)
  47. So(err, ShouldNotBeNil)
  48. So(err, ShouldEqual, m.ErrDashboardVersionNotFound)
  49. })
  50. })
  51. }
  52. func TestGetDashboardVersions(t *testing.T) {
  53. Convey("Testing dashboard versions retrieval", t, func() {
  54. InitTestDB(t)
  55. savedDash := insertTestDashboard("test dash 43", 1, "diff-all")
  56. Convey("Get all versions for a given Dashboard ID", func() {
  57. query := m.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1}
  58. err := GetDashboardVersions(&query)
  59. So(err, ShouldBeNil)
  60. So(len(query.Result), ShouldEqual, 1)
  61. })
  62. Convey("Attempt to get the versions for a non-existent Dashboard ID", func() {
  63. query := m.GetDashboardVersionsQuery{DashboardId: int64(999), OrgId: 1}
  64. err := GetDashboardVersions(&query)
  65. So(err, ShouldNotBeNil)
  66. So(err, ShouldEqual, m.ErrNoVersionsForDashboardId)
  67. So(len(query.Result), ShouldEqual, 0)
  68. })
  69. Convey("Get all versions for an updated dashboard", func() {
  70. updateTestDashboard(savedDash, map[string]interface{}{
  71. "tags": "different-tag",
  72. })
  73. query := m.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1}
  74. err := GetDashboardVersions(&query)
  75. So(err, ShouldBeNil)
  76. So(len(query.Result), ShouldEqual, 2)
  77. })
  78. })
  79. }