scan_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. var testInsertSongs = 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. Context("when the songs do not already exist in the database", func() {
  43. BeforeEach(testInsertSongs)
  44. It("should insert the correct number of songs", func() {
  45. var count int
  46. db.Get(&count, "select count(*) from songs")
  47. Expect(count).To(Equal(2))
  48. })
  49. It("should insert both songs", func() {
  50. var song read.Song
  51. rows, _ := db.Queryx(`
  52. select title, artist, album, duration, base_path, relative_path
  53. from songs
  54. order by title
  55. `)
  56. rows.Next()
  57. rows.StructScan(&song)
  58. Expect(song).To(Equal(read.Song{
  59. Title: "Hey Jude",
  60. Artist: "The Beatles",
  61. Album: "",
  62. Duration: 431,
  63. BasePath: "/path/to",
  64. RelativePath: "file.ogg",
  65. }))
  66. rows.Next()
  67. rows.StructScan(&song)
  68. Expect(song).To(Equal(read.Song{
  69. Title: "Starman",
  70. Artist: "David Bowie",
  71. Album: "The Rise and Fall of Ziggy Stardust and the Spiders from Mars",
  72. Duration: 256,
  73. BasePath: "/different/path",
  74. RelativePath: "otherFile.ogg",
  75. }))
  76. rows.Close()
  77. })
  78. })
  79. Context("when there is already a file in the database with the same name", func() {
  80. BeforeEach(func() {
  81. db.MustExec(
  82. `
  83. insert into songs (title, artist, album, base_path, relative_path)
  84. values ($1, $2, $3, $4, $5)
  85. `,
  86. "my title",
  87. "my artist",
  88. "my album",
  89. "/path/to",
  90. "file.ogg",
  91. )
  92. testInsertSongs()
  93. })
  94. It("should not add an additional row for the same file", func() {
  95. var count int
  96. db.Get(&count, `
  97. select count(*) from songs
  98. where base_path = '/path/to' and relative_path = 'file.ogg'
  99. `)
  100. Expect(count).To(Equal(1))
  101. })
  102. It("should upsert the existing item", func() {
  103. rows, _ := db.Queryx(`
  104. select title, artist, album, duration, base_path, relative_path from songs
  105. where base_path = '/path/to' and relative_path = 'file.ogg'
  106. `)
  107. var song read.Song
  108. rows.Next()
  109. rows.StructScan(&song)
  110. Expect(song.Title).To(Equal("Hey Jude"))
  111. Expect(song.Artist).To(Equal("The Beatles"))
  112. Expect(song.Album).To(Equal(""))
  113. Expect(song.Duration).To(Equal(431))
  114. rows.Close()
  115. })
  116. })
  117. })
  118. })