scanner_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package services_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. "github.com/felamaslen/gmus-backend/pkg/services"
  8. setup "github.com/felamaslen/gmus-backend/pkg/testing"
  9. "github.com/felamaslen/gmus-backend/pkg/types"
  10. )
  11. var _ = Describe("Music scanner service", func() {
  12. db := database.GetConnection()
  13. BeforeEach(func() {
  14. setup.PrepareDatabaseForTesting()
  15. })
  16. Describe("UpsertSongsFromChannel", func() {
  17. var songs chan *types.Song
  18. var testScanSongs = func() {
  19. songs = make(chan *types.Song)
  20. go func() {
  21. defer close(songs)
  22. songs <- &types.Song{
  23. TrackNumber: 7,
  24. Title: "Hey Jude",
  25. Artist: "The Beatles",
  26. Album: "",
  27. Duration: 431,
  28. BasePath: "/path/to",
  29. RelativePath: "file.ogg",
  30. ModifiedDate: 8876,
  31. }
  32. songs <- &types.Song{
  33. TrackNumber: 11,
  34. Title: "Starman",
  35. Artist: "David Bowie",
  36. Album: "The Rise and Fall of Ziggy Stardust and the Spiders from Mars",
  37. Duration: 256,
  38. BasePath: "/different/path",
  39. RelativePath: "otherFile.ogg",
  40. ModifiedDate: 11883,
  41. }
  42. }()
  43. services.UpsertSongsFromChannel(songs)
  44. }
  45. Context("when the songs do not already exist in the database", func() {
  46. BeforeEach(testScanSongs)
  47. It("should insert the correct number of songs", func() {
  48. var count int
  49. db.Get(&count, "select count(*) from songs")
  50. Expect(count).To(Equal(2))
  51. })
  52. It("should insert both songs", func() {
  53. var songs []types.Song
  54. db.Select(&songs, `
  55. select track_number, title, artist, album, duration, base_path, relative_path, modified_date
  56. from songs
  57. order by title
  58. `)
  59. Expect(songs[0]).To(Equal(types.Song{
  60. TrackNumber: 7,
  61. Title: "Hey Jude",
  62. Artist: "The Beatles",
  63. Album: "",
  64. Duration: 431,
  65. BasePath: "/path/to",
  66. RelativePath: "file.ogg",
  67. ModifiedDate: 8876,
  68. }))
  69. Expect(songs[1]).To(Equal(types.Song{
  70. TrackNumber: 11,
  71. Title: "Starman",
  72. Artist: "David Bowie",
  73. Album: "The Rise and Fall of Ziggy Stardust and the Spiders from Mars",
  74. Duration: 256,
  75. BasePath: "/different/path",
  76. RelativePath: "otherFile.ogg",
  77. ModifiedDate: 11883,
  78. }))
  79. })
  80. })
  81. Context("when there is already a file in the database with the same name", func() {
  82. BeforeEach(func() {
  83. db.MustExec(
  84. `
  85. insert into songs (title, artist, album, base_path, relative_path, modified_date)
  86. values ($1, $2, $3, $4, $5, $6)
  87. `,
  88. "my title",
  89. "my artist",
  90. "my album",
  91. "/path/to",
  92. "file.ogg",
  93. 7782,
  94. )
  95. testScanSongs()
  96. })
  97. It("should not add an additional row for the same file", func() {
  98. var count int
  99. db.Get(&count, `
  100. select count(*) from songs
  101. where base_path = '/path/to' and relative_path = 'file.ogg'
  102. `)
  103. Expect(count).To(Equal(1))
  104. })
  105. It("should upsert the existing item", func() {
  106. var songs []types.Song
  107. db.Select(&songs, `
  108. select
  109. track_number
  110. ,title
  111. ,artist
  112. ,album
  113. ,duration
  114. ,base_path
  115. ,relative_path
  116. ,modified_date
  117. from songs
  118. where base_path = '/path/to' and relative_path = 'file.ogg'
  119. `)
  120. Expect(songs).To(HaveLen(1))
  121. var song = songs[0]
  122. Expect(song.TrackNumber).To(Equal(7))
  123. Expect(song.Title).To(Equal("Hey Jude"))
  124. Expect(song.Artist).To(Equal("The Beatles"))
  125. Expect(song.Album).To(Equal(""))
  126. Expect(song.Duration).To(Equal(431))
  127. Expect(song.ModifiedDate).To(Equal(int64(8876)))
  128. })
  129. })
  130. })
  131. Describe("ScanAndInsert", func() {
  132. It("should recursively scan files from a directory and add them to the database", func() {
  133. services.ScanAndInsert(read.TestDirectory)
  134. var songs []types.Song
  135. err := db.Select(&songs, `
  136. select title, artist, album, duration, base_path, relative_path
  137. from songs
  138. `)
  139. Expect(err).To(BeNil())
  140. Expect(songs).To(HaveLen(2))
  141. Expect(types.Song{
  142. Title: read.TestSong.Title,
  143. Artist: read.TestSong.Artist,
  144. Album: read.TestSong.Album,
  145. Duration: read.TestSong.Duration,
  146. BasePath: read.TestSong.BasePath,
  147. RelativePath: read.TestSong.RelativePath,
  148. }).To(BeElementOf(songs))
  149. Expect(types.Song{
  150. Title: read.TestSongNested.Title,
  151. Artist: read.TestSongNested.Artist,
  152. Album: read.TestSongNested.Album,
  153. Duration: read.TestSongNested.Duration,
  154. BasePath: read.TestSongNested.BasePath,
  155. RelativePath: read.TestSongNested.RelativePath,
  156. }).To(BeElementOf(songs))
  157. })
  158. })
  159. })