dashboard_version_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. cmd := m.GetDashboardVersionCommand{
  25. DashboardId: savedDash.Id,
  26. Version: savedDash.Version,
  27. }
  28. err := GetDashboardVersion(&cmd)
  29. So(err, ShouldBeNil)
  30. So(savedDash.Id, ShouldEqual, cmd.DashboardId)
  31. So(savedDash.Version, ShouldEqual, cmd.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, cmd.Result.Data)
  39. So(eq, ShouldEqual, true)
  40. })
  41. Convey("Attempt to get a version that doesn't exist", func() {
  42. cmd := m.GetDashboardVersionCommand{
  43. DashboardId: int64(999),
  44. Version: 123,
  45. }
  46. err := GetDashboardVersion(&cmd)
  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. cmd := m.GetDashboardVersionsCommand{
  58. DashboardId: savedDash.Id,
  59. }
  60. err := GetDashboardVersions(&cmd)
  61. So(err, ShouldBeNil)
  62. So(len(cmd.Result), ShouldEqual, 1)
  63. })
  64. Convey("Attempt to get the versions for a non-existent Dashboard ID", func() {
  65. cmd := m.GetDashboardVersionsCommand{
  66. DashboardId: int64(999),
  67. }
  68. err := GetDashboardVersions(&cmd)
  69. So(err, ShouldNotBeNil)
  70. So(err, ShouldEqual, m.ErrNoVersionsForDashboardId)
  71. So(len(cmd.Result), ShouldEqual, 0)
  72. })
  73. Convey("Get all versions for an updated dashboard", func() {
  74. updateTestDashboard(savedDash, map[string]interface{}{
  75. "tags": "different-tag",
  76. })
  77. cmd := m.GetDashboardVersionsCommand{
  78. DashboardId: savedDash.Id,
  79. }
  80. err := GetDashboardVersions(&cmd)
  81. So(err, ShouldBeNil)
  82. So(len(cmd.Result), ShouldEqual, 2)
  83. })
  84. })
  85. }
  86. func TestCompareDashboardVersions(t *testing.T) {
  87. Convey("Testing dashboard version comparison", t, func() {
  88. InitTestDB(t)
  89. savedDash := insertTestDashboard("test dash 43", 1, "x")
  90. updateTestDashboard(savedDash, map[string]interface{}{
  91. "tags": "y",
  92. })
  93. Convey("Compare two versions that are different", func() {
  94. getVersionCmd := m.GetDashboardVersionsCommand{
  95. DashboardId: savedDash.Id,
  96. }
  97. err := GetDashboardVersions(&getVersionCmd)
  98. So(err, ShouldBeNil)
  99. So(len(getVersionCmd.Result), ShouldEqual, 2)
  100. cmd := m.CompareDashboardVersionsCommand{
  101. DashboardId: savedDash.Id,
  102. Original: getVersionCmd.Result[0].Version,
  103. New: getVersionCmd.Result[1].Version,
  104. DiffType: m.DiffDelta,
  105. }
  106. err = CompareDashboardVersionsCommand(&cmd)
  107. So(err, ShouldBeNil)
  108. So(cmd.Delta, ShouldNotBeNil)
  109. })
  110. Convey("Compare two versions that are the same", func() {
  111. cmd := m.CompareDashboardVersionsCommand{
  112. DashboardId: savedDash.Id,
  113. Original: savedDash.Version,
  114. New: savedDash.Version,
  115. DiffType: m.DiffDelta,
  116. }
  117. err := CompareDashboardVersionsCommand(&cmd)
  118. So(err, ShouldNotBeNil)
  119. So(cmd.Delta, ShouldBeNil)
  120. })
  121. Convey("Compare two versions that don't exist", func() {
  122. cmd := m.CompareDashboardVersionsCommand{
  123. DashboardId: savedDash.Id,
  124. Original: 123,
  125. New: 456,
  126. DiffType: m.DiffDelta,
  127. }
  128. err := CompareDashboardVersionsCommand(&cmd)
  129. So(err, ShouldNotBeNil)
  130. So(cmd.Delta, ShouldBeNil)
  131. })
  132. })
  133. }
  134. func TestRestoreDashboardVersion(t *testing.T) {
  135. Convey("Testing dashboard version restoration", t, func() {
  136. InitTestDB(t)
  137. savedDash := insertTestDashboard("test dash 26", 1, "restore")
  138. updateTestDashboard(savedDash, map[string]interface{}{
  139. "tags": "not restore",
  140. })
  141. Convey("Restore dashboard to a previous version", func() {
  142. versionsCmd := m.GetDashboardVersionsCommand{
  143. DashboardId: savedDash.Id,
  144. }
  145. err := GetDashboardVersions(&versionsCmd)
  146. So(err, ShouldBeNil)
  147. cmd := m.RestoreDashboardVersionCommand{
  148. DashboardId: savedDash.Id,
  149. Version: savedDash.Version,
  150. UserId: 0,
  151. }
  152. err = RestoreDashboardVersion(&cmd)
  153. So(err, ShouldBeNil)
  154. })
  155. })
  156. }