scan_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package repository_test
  2. import (
  3. . "github.com/onsi/ginkgo"
  4. . "github.com/onsi/gomega"
  5. "github.com/felamaslen/go-music-player/pkg/database"
  6. "github.com/felamaslen/go-music-player/pkg/read"
  7. "github.com/felamaslen/go-music-player/pkg/repository"
  8. setup "github.com/felamaslen/go-music-player/pkg/testing"
  9. )
  10. var _ = Describe("scanning repository", func() {
  11. db := database.GetConnection()
  12. BeforeEach(func() {
  13. setup.PrepareDatabaseForTesting()
  14. })
  15. Describe("when the channel sends two files", func() {
  16. var songs chan *read.Song
  17. BeforeEach(func() {
  18. songs = make(chan *read.Song)
  19. go func() {
  20. defer close(songs)
  21. songs <- &read.Song{
  22. Title: "Hey Jude",
  23. Artist: "The Beatles",
  24. Album: "",
  25. Duration: 431,
  26. DurationOk: true,
  27. BasePath: "/path/to",
  28. RelativePath: "file.ogg",
  29. }
  30. songs <- &read.Song{
  31. Title: "Starman",
  32. Artist: "David Bowie",
  33. Album: "The Rise and Fall of Ziggy Stardust and the Spiders from Mars",
  34. Duration: 256,
  35. DurationOk: true,
  36. BasePath: "/different/path",
  37. RelativePath: "otherFile.ogg",
  38. }
  39. }()
  40. repository.InsertMusicIntoDatabase(songs)
  41. })
  42. It("should insert the correct number of songs", func() {
  43. var count int
  44. db.Get(&count, "select count(*) from songs")
  45. Expect(count).To(Equal(2))
  46. })
  47. It("should insert both songs", func() {
  48. var song read.Song
  49. rows, _ := db.Queryx(`
  50. select title, artist, album, duration, base_path, relative_path
  51. from songs
  52. order by title
  53. `)
  54. rows.Next()
  55. rows.StructScan(&song)
  56. Expect(song).To(Equal(read.Song{
  57. Title: "Hey Jude",
  58. Artist: "The Beatles",
  59. Album: "",
  60. Duration: 431,
  61. BasePath: "/path/to",
  62. RelativePath: "file.ogg",
  63. }))
  64. rows.Next()
  65. rows.StructScan(&song)
  66. Expect(song).To(Equal(read.Song{
  67. Title: "Starman",
  68. Artist: "David Bowie",
  69. Album: "The Rise and Fall of Ziggy Stardust and the Spiders from Mars",
  70. Duration: 256,
  71. BasePath: "/different/path",
  72. RelativePath: "otherFile.ogg",
  73. }))
  74. rows.Close()
  75. })
  76. })
  77. })