files_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package read_test
  2. import (
  3. "os"
  4. "path"
  5. . "github.com/onsi/ginkgo"
  6. . "github.com/onsi/gomega"
  7. "github.com/felamaslen/gmus-backend/pkg/database"
  8. "github.com/felamaslen/gmus-backend/pkg/read"
  9. setup "github.com/felamaslen/gmus-backend/pkg/testing"
  10. )
  11. var _ = Describe("reading files", func() {
  12. db := database.GetConnection()
  13. BeforeEach(func() {
  14. setup.PrepareDatabaseForTesting()
  15. })
  16. Describe("reading file info", func() {
  17. var results []*read.Song
  18. BeforeEach(func() {
  19. results = nil
  20. files := make(chan *read.File, 1)
  21. go func() {
  22. defer close(files)
  23. files <- &read.File{
  24. RelativePath: read.TestSong.RelativePath,
  25. ModifiedDate: 100123,
  26. }
  27. }()
  28. outputChan := read.ReadMultipleFiles(read.TestDirectory, files)
  29. outputDone := false
  30. for !outputDone {
  31. select {
  32. case result, more := <-outputChan:
  33. if more {
  34. results = append(results, result)
  35. }
  36. outputDone = !more
  37. }
  38. }
  39. })
  40. It("should return the correct number of results", func() {
  41. Expect(results).To(HaveLen(1))
  42. })
  43. It("should get the correct info from the file", func() {
  44. var expectedResult = read.TestSong
  45. expectedResult.ModifiedDate = 100123
  46. Expect(*results[0]).To(Equal(expectedResult))
  47. })
  48. })
  49. Describe("scanning a directory recursively", func() {
  50. var results []*read.File
  51. var testScanDirectory = func() {
  52. results = nil
  53. files := read.ScanDirectory(read.TestDirectory)
  54. done := false
  55. for !done {
  56. select {
  57. case result, more := <-files:
  58. if more {
  59. results = append(results, result)
  60. }
  61. done = !more
  62. }
  63. }
  64. }
  65. Context("when the database is empty", func() {
  66. BeforeEach(testScanDirectory)
  67. It("should return a channel with all the files in the directory", func() {
  68. Expect(results).To(HaveLen(2))
  69. if results[0].RelativePath == read.TestSong.RelativePath {
  70. Expect(results[0].RelativePath).To(Equal(read.TestSong.RelativePath))
  71. Expect(results[1].RelativePath).To(Equal(read.TestSongNested.RelativePath))
  72. } else {
  73. Expect(results[1].RelativePath).To(Equal(read.TestSong.RelativePath))
  74. Expect(results[0].RelativePath).To(Equal(read.TestSongNested.RelativePath))
  75. }
  76. })
  77. })
  78. Context("when the database already contains one of the files", func() {
  79. BeforeEach(func() {
  80. info, _ := os.Stat(path.Join(read.TestSong.BasePath, read.TestSong.RelativePath))
  81. db.MustExec(
  82. `
  83. insert into songs (title, artist, album, base_path, relative_path, modified_date)
  84. values ($1, $2, $3, $4, $5, $6)
  85. `,
  86. "old title",
  87. "old artist",
  88. "old album",
  89. read.TestSong.BasePath,
  90. read.TestSong.RelativePath,
  91. info.ModTime().Unix(),
  92. )
  93. testScanDirectory()
  94. })
  95. It("should only return those files which do not exist in the database", func() {
  96. Expect(results).To(HaveLen(1))
  97. Expect(results[0].RelativePath).To(Equal(read.TestSongNested.RelativePath))
  98. })
  99. })
  100. })
  101. })