dashboard_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package sqlstore
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/search"
  8. )
  9. func insertTestDashboard(title string, orgId int64, parentId int64, isFolder bool, tags ...interface{}) *m.Dashboard {
  10. cmd := m.SaveDashboardCommand{
  11. OrgId: orgId,
  12. ParentId: parentId,
  13. IsFolder: isFolder,
  14. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  15. "id": nil,
  16. "title": title,
  17. "tags": tags,
  18. }),
  19. }
  20. err := SaveDashboard(&cmd)
  21. So(err, ShouldBeNil)
  22. return cmd.Result
  23. }
  24. func TestDashboardDataAccess(t *testing.T) {
  25. Convey("Testing DB", t, func() {
  26. InitTestDB(t)
  27. Convey("Given saved dashboard", func() {
  28. savedFolder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp")
  29. savedDash := insertTestDashboard("test dash 23", 1, savedFolder.Id, false, "prod", "webapp")
  30. insertTestDashboard("test dash 45", 1, savedFolder.Id, false, "prod")
  31. insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp")
  32. Convey("Should return dashboard model", func() {
  33. So(savedDash.Title, ShouldEqual, "test dash 23")
  34. So(savedDash.Slug, ShouldEqual, "test-dash-23")
  35. So(savedDash.Id, ShouldNotEqual, 0)
  36. So(savedDash.IsFolder, ShouldBeFalse)
  37. So(savedDash.ParentId, ShouldBeGreaterThan, 0)
  38. So(savedFolder.Title, ShouldEqual, "1 test dash folder")
  39. So(savedFolder.Slug, ShouldEqual, "1-test-dash-folder")
  40. So(savedFolder.Id, ShouldNotEqual, 0)
  41. So(savedFolder.IsFolder, ShouldBeTrue)
  42. So(savedFolder.ParentId, ShouldEqual, 0)
  43. })
  44. Convey("Should be able to get dashboard", func() {
  45. query := m.GetDashboardQuery{
  46. Slug: "test-dash-23",
  47. OrgId: 1,
  48. }
  49. err := GetDashboard(&query)
  50. So(err, ShouldBeNil)
  51. So(query.Result.Title, ShouldEqual, "test dash 23")
  52. So(query.Result.Slug, ShouldEqual, "test-dash-23")
  53. So(query.Result.IsFolder, ShouldBeFalse)
  54. })
  55. Convey("Should be able to delete dashboard", func() {
  56. dash := insertTestDashboard("delete me", 1, 0, false, "delete this")
  57. err := DeleteDashboard(&m.DeleteDashboardCommand{
  58. Id: dash.Id,
  59. OrgId: 1,
  60. })
  61. So(err, ShouldBeNil)
  62. })
  63. Convey("Should return error if no dashboard is updated", func() {
  64. cmd := m.SaveDashboardCommand{
  65. OrgId: 1,
  66. Overwrite: true,
  67. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  68. "id": float64(123412321),
  69. "title": "Expect error",
  70. "tags": []interface{}{},
  71. }),
  72. }
  73. err := SaveDashboard(&cmd)
  74. So(err, ShouldNotBeNil)
  75. })
  76. Convey("Should not be able to overwrite dashboard in another org", func() {
  77. query := m.GetDashboardQuery{Slug: "test-dash-23", OrgId: 1}
  78. GetDashboard(&query)
  79. cmd := m.SaveDashboardCommand{
  80. OrgId: 2,
  81. Overwrite: true,
  82. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  83. "id": float64(query.Result.Id),
  84. "title": "Expect error",
  85. "tags": []interface{}{},
  86. }),
  87. }
  88. err := SaveDashboard(&cmd)
  89. So(err, ShouldNotBeNil)
  90. })
  91. Convey("Should be able to search for dashboard and return in folder hierarchy", func() {
  92. query := search.FindPersistedDashboardsQuery{
  93. Title: "test dash 23",
  94. OrgId: 1,
  95. Mode: "tree",
  96. }
  97. err := SearchDashboards(&query)
  98. So(err, ShouldBeNil)
  99. So(len(query.Result), ShouldEqual, 1)
  100. hit := query.Result[0].Dashboards[0]
  101. So(len(hit.Tags), ShouldEqual, 2)
  102. So(hit.Type, ShouldEqual, search.DashHitDB)
  103. So(hit.ParentId, ShouldBeGreaterThan, 0)
  104. })
  105. Convey("Should be able to search for dashboard folder", func() {
  106. query := search.FindPersistedDashboardsQuery{
  107. Title: "1 test dash folder",
  108. OrgId: 1,
  109. }
  110. err := SearchDashboards(&query)
  111. So(err, ShouldBeNil)
  112. So(len(query.Result), ShouldEqual, 1)
  113. hit := query.Result[0]
  114. So(hit.Type, ShouldEqual, search.DashHitFolder)
  115. })
  116. Convey("Should be able to search for a dashboard folder's children", func() {
  117. query := search.FindPersistedDashboardsQuery{
  118. OrgId: 1,
  119. ParentId: savedFolder.Id,
  120. }
  121. err := SearchDashboards(&query)
  122. So(err, ShouldBeNil)
  123. So(len(query.Result), ShouldEqual, 2)
  124. hit := query.Result[0]
  125. So(hit.Id, ShouldEqual, savedDash.Id)
  126. })
  127. Convey("Should be able to search for dashboard by dashboard ids", func() {
  128. Convey("should be able to find two dashboards by id", func() {
  129. query := search.FindPersistedDashboardsQuery{
  130. DashboardIds: []int{2, 3},
  131. OrgId: 1,
  132. Mode: "tree",
  133. }
  134. err := SearchDashboards(&query)
  135. So(err, ShouldBeNil)
  136. So(len(query.Result[0].Dashboards), ShouldEqual, 2)
  137. hit := query.Result[0].Dashboards[0]
  138. So(len(hit.Tags), ShouldEqual, 2)
  139. hit2 := query.Result[0].Dashboards[1]
  140. So(len(hit2.Tags), ShouldEqual, 1)
  141. })
  142. Convey("DashboardIds that does not exists should not cause errors", func() {
  143. query := search.FindPersistedDashboardsQuery{
  144. DashboardIds: []int{1000},
  145. OrgId: 1,
  146. }
  147. err := SearchDashboards(&query)
  148. So(err, ShouldBeNil)
  149. So(len(query.Result), ShouldEqual, 0)
  150. })
  151. })
  152. Convey("Should not be able to save dashboard with same name", func() {
  153. cmd := m.SaveDashboardCommand{
  154. OrgId: 1,
  155. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  156. "id": nil,
  157. "title": "test dash 23",
  158. "tags": []interface{}{},
  159. }),
  160. }
  161. err := SaveDashboard(&cmd)
  162. So(err, ShouldNotBeNil)
  163. })
  164. Convey("Should be able to update dashboard and remove parentId", func() {
  165. cmd := m.SaveDashboardCommand{
  166. OrgId: 1,
  167. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  168. "id": 1,
  169. "title": "parentId",
  170. "tags": []interface{}{},
  171. }),
  172. Overwrite: true,
  173. ParentId: 2,
  174. }
  175. err := SaveDashboard(&cmd)
  176. So(err, ShouldBeNil)
  177. So(cmd.Result.ParentId, ShouldEqual, 2)
  178. cmd = m.SaveDashboardCommand{
  179. OrgId: 1,
  180. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  181. "id": 1,
  182. "title": "parentId",
  183. "tags": []interface{}{},
  184. }),
  185. ParentId: 0,
  186. Overwrite: true,
  187. }
  188. err = SaveDashboard(&cmd)
  189. So(err, ShouldBeNil)
  190. query := m.GetDashboardQuery{
  191. Slug: cmd.Result.Slug,
  192. OrgId: 1,
  193. }
  194. err = GetDashboard(&query)
  195. So(err, ShouldBeNil)
  196. So(query.Result.ParentId, ShouldEqual, 0)
  197. })
  198. Convey("Should be able to get dashboard tags", func() {
  199. query := m.GetDashboardTagsQuery{OrgId: 1}
  200. err := GetDashboardTags(&query)
  201. So(err, ShouldBeNil)
  202. So(len(query.Result), ShouldEqual, 2)
  203. })
  204. Convey("Given two dashboards, one is starred dashboard by user 10, other starred by user 1", func() {
  205. starredDash := insertTestDashboard("starred dash", 1, 0, false)
  206. StarDashboard(&m.StarDashboardCommand{
  207. DashboardId: starredDash.Id,
  208. UserId: 10,
  209. })
  210. StarDashboard(&m.StarDashboardCommand{
  211. DashboardId: savedDash.Id,
  212. UserId: 1,
  213. })
  214. Convey("Should be able to search for starred dashboards", func() {
  215. query := search.FindPersistedDashboardsQuery{OrgId: 1, UserId: 10, IsStarred: true}
  216. err := SearchDashboards(&query)
  217. So(err, ShouldBeNil)
  218. So(len(query.Result), ShouldEqual, 1)
  219. So(query.Result[0].Title, ShouldEqual, "starred dash")
  220. })
  221. })
  222. })
  223. })
  224. }