dashboards_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package models
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. "github.com/grafana/grafana/pkg/setting"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestDashboardModel(t *testing.T) {
  9. Convey("Generate full dashboard url", t, func() {
  10. setting.AppUrl = "http://grafana.local/"
  11. fullUrl := GetFullDashboardUrl("uid", "my-dashboard")
  12. So(fullUrl, ShouldEqual, "http://grafana.local/d/uid/my-dashboard")
  13. })
  14. Convey("Generate relative dashboard url", t, func() {
  15. setting.AppUrl = ""
  16. fullUrl := GetDashboardUrl("uid", "my-dashboard")
  17. So(fullUrl, ShouldEqual, "/d/uid/my-dashboard")
  18. })
  19. Convey("When generating slug", t, func() {
  20. dashboard := NewDashboard("Grafana Play Home")
  21. dashboard.UpdateSlug()
  22. So(dashboard.Slug, ShouldEqual, "grafana-play-home")
  23. })
  24. Convey("Can slugify title", t, func() {
  25. slug := SlugifyTitle("Grafana Play Home")
  26. So(slug, ShouldEqual, "grafana-play-home")
  27. })
  28. Convey("Given a dashboard json", t, func() {
  29. json := simplejson.New()
  30. json.Set("title", "test dash")
  31. Convey("With tags as string value", func() {
  32. json.Set("tags", "")
  33. dash := NewDashboardFromJson(json)
  34. So(len(dash.GetTags()), ShouldEqual, 0)
  35. })
  36. })
  37. Convey("Given a new dashboard folder", t, func() {
  38. json := simplejson.New()
  39. json.Set("title", "test dash")
  40. cmd := &SaveDashboardCommand{Dashboard: json, IsFolder: true}
  41. dash := cmd.GetDashboardModel()
  42. Convey("Should set IsFolder to true", func() {
  43. So(dash.IsFolder, ShouldBeTrue)
  44. })
  45. })
  46. Convey("Given a child dashboard", t, func() {
  47. json := simplejson.New()
  48. json.Set("title", "test dash")
  49. cmd := &SaveDashboardCommand{Dashboard: json, FolderId: 1}
  50. dash := cmd.GetDashboardModel()
  51. Convey("Should set FolderId", func() {
  52. So(dash.FolderId, ShouldEqual, 1)
  53. })
  54. })
  55. }