file_store_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package stores
  2. //
  3. // import (
  4. // "fmt"
  5. // "io"
  6. // "io/ioutil"
  7. // "os"
  8. // "path/filepath"
  9. // "testing"
  10. //
  11. // . "github.com/smartystreets/goconvey/convey"
  12. // "github.com/torkelo/grafana-pro/pkg/models"
  13. // )
  14. //
  15. // func TestFileStore(t *testing.T) {
  16. //
  17. // GivenFileStore("When saving a dashboard", t, func(store *fileStore) {
  18. // dashboard := models.NewDashboard("hello")
  19. //
  20. // err := store.Save(dashboard)
  21. //
  22. // Convey("should be saved to disk", func() {
  23. // So(err, ShouldBeNil)
  24. //
  25. // _, err = os.Stat(store.getFilePathForDashboard("hello"))
  26. // So(err, ShouldBeNil)
  27. // })
  28. // })
  29. //
  30. // GivenFileStore("When getting a saved dashboard", t, func(store *fileStore) {
  31. // copyDashboardToTempData("default.json", "", store.dashDir)
  32. // dash, err := store.GetById("default")
  33. //
  34. // Convey("should be read from disk", func() {
  35. // So(err, ShouldBeNil)
  36. // So(dash, ShouldNotBeNil)
  37. //
  38. // So(dash.Title, ShouldEqual, "Grafana Play Home")
  39. // })
  40. // })
  41. //
  42. // GivenFileStore("when getting dashboard with capital letters", t, func(store *fileStore) {
  43. // copyDashboardToTempData("annotations.json", "", store.dashDir)
  44. // dash, err := store.GetById("AnnoTations")
  45. //
  46. // Convey("should be read from disk", func() {
  47. // So(err, ShouldBeNil)
  48. // So(dash, ShouldNotBeNil)
  49. //
  50. // So(dash.Title, ShouldEqual, "Annotations")
  51. // })
  52. // })
  53. //
  54. // GivenFileStore("When copying dashboards into data dir", t, func(store *fileStore) {
  55. // copyDashboardToTempData("annotations.json", "", store.dashDir)
  56. // copyDashboardToTempData("default.json", "", store.dashDir)
  57. // copyDashboardToTempData("graph-styles.json", "", store.dashDir)
  58. // store.scanFiles()
  59. //
  60. // Convey("scan should generate index of all dashboards", func() {
  61. //
  62. // result, err := store.Query("*")
  63. // So(err, ShouldBeNil)
  64. // So(len(result), ShouldEqual, 3)
  65. // })
  66. // })
  67. // }
  68. //
  69. // func copyDashboardToTempData(name string, destName string, dir string) {
  70. // if destName == "" {
  71. // destName = name
  72. // }
  73. // source, _ := filepath.Abs("../../data/dashboards/" + name)
  74. // dest := filepath.Join(dir, destName)
  75. // err := copyFile(dest, source)
  76. // if err != nil {
  77. // panic(fmt.Sprintf("failed to copy file %v", name))
  78. // }
  79. // }
  80. //
  81. // func GivenFileStore(desc string, t *testing.T, f func(store *fileStore)) {
  82. // Convey(desc, t, func() {
  83. // tempDir, _ := ioutil.TempDir("", "store")
  84. //
  85. // store := NewFileStore(tempDir)
  86. //
  87. // f(store)
  88. //
  89. // Reset(func() {
  90. // os.RemoveAll(tempDir)
  91. // })
  92. // })
  93. // }
  94. //
  95. // func copyFile(dst, src string) error {
  96. // in, err := os.Open(src)
  97. // if err != nil {
  98. // return err
  99. // }
  100. // defer in.Close()
  101. // out, err := os.Create(dst)
  102. // if err != nil {
  103. // return err
  104. // }
  105. // defer out.Close()
  106. // _, err = io.Copy(out, in)
  107. // cerr := out.Close()
  108. // if err != nil {
  109. // return err
  110. // }
  111. // return cerr
  112. // }