files_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package read_test
  2. import (
  3. . "github.com/onsi/ginkgo"
  4. . "github.com/onsi/gomega"
  5. "github.com/felamaslen/gmus-backend/pkg/database"
  6. "github.com/felamaslen/gmus-backend/pkg/read"
  7. setup "github.com/felamaslen/gmus-backend/pkg/testing"
  8. "github.com/felamaslen/gmus-backend/pkg/types"
  9. )
  10. var _ = Describe("Reading files", func() {
  11. db := database.GetConnection()
  12. BeforeEach(func() {
  13. setup.PrepareDatabaseForTesting()
  14. })
  15. Describe("ReadMultipleFiles", func() {
  16. var results []*types.Song
  17. var files chan *types.File
  18. BeforeEach(func() {
  19. results = nil
  20. files = make(chan *types.File, 1)
  21. })
  22. Context("when all the files are readable", func() {
  23. BeforeEach(func() {
  24. go func() {
  25. defer close(files)
  26. files <- &types.File{
  27. RelativePath: read.TestSong.RelativePath,
  28. ModifiedDate: 100123,
  29. }
  30. }()
  31. outputChan := read.ReadMultipleFiles(read.TestDirectory, files)
  32. outputDone := false
  33. for !outputDone {
  34. select {
  35. case result, more := <-outputChan:
  36. if more {
  37. results = append(results, result)
  38. }
  39. outputDone = !more
  40. }
  41. }
  42. })
  43. It("should return the correct number of results", func() {
  44. Expect(results).To(HaveLen(1))
  45. })
  46. It("should get the correct info from the file", func() {
  47. var expectedResult = read.TestSong
  48. expectedResult.ModifiedDate = 100123
  49. Expect(*results[0]).To(Equal(expectedResult))
  50. })
  51. })
  52. Context("when an error occurs reading a file", func() {
  53. BeforeEach(func() {
  54. go func() {
  55. defer close(files)
  56. files <- &types.File{
  57. RelativePath: "test/file/does/not/exist.ogg",
  58. ModifiedDate: 123,
  59. }
  60. }()
  61. outputChan := read.ReadMultipleFiles(read.TestDirectory, files)
  62. outputDone := false
  63. for !outputDone {
  64. select {
  65. case _, more := <-outputChan:
  66. outputDone = !more
  67. }
  68. }
  69. })
  70. It("should add the file to the scan_errors table", func() {
  71. var scanError []*types.ScanError
  72. db.Select(&scanError, "select relative_path, base_path, error from scan_errors")
  73. Expect(scanError).To(HaveLen(1))
  74. Expect(scanError[0]).To(Equal(&types.ScanError{
  75. RelativePath: "test/file/does/not/exist.ogg",
  76. BasePath: read.TestDirectory,
  77. Error: "open pkg/read/testdata/test/file/does/not/exist.ogg: no such file or directory",
  78. }))
  79. })
  80. })
  81. })
  82. })