scan_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. TrackNumber: 7,
  23. Title: "Hey Jude",
  24. Artist: "The Beatles",
  25. Album: "",
  26. Duration: 431,
  27. DurationOk: true,
  28. BasePath: "/path/to",
  29. RelativePath: "file.ogg",
  30. ModifiedDate: 8876,
  31. }
  32. songs <- &read.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. DurationOk: true,
  39. BasePath: "/different/path",
  40. RelativePath: "otherFile.ogg",
  41. ModifiedDate: 11883,
  42. }
  43. }()
  44. repository.InsertMusicIntoDatabase(songs)
  45. }
  46. Context("when the songs do not already exist in the database", func() {
  47. BeforeEach(testInsertSongs)
  48. It("should insert the correct number of songs", func() {
  49. var count int
  50. db.Get(&count, "select count(*) from songs")
  51. Expect(count).To(Equal(2))
  52. })
  53. It("should insert both songs", func() {
  54. var songs []read.Song
  55. db.Select(&songs, `
  56. select track_number, title, artist, album, duration, base_path, relative_path, modified_date
  57. from songs
  58. order by title
  59. `)
  60. Expect(songs[0]).To(Equal(read.Song{
  61. TrackNumber: 7,
  62. Title: "Hey Jude",
  63. Artist: "The Beatles",
  64. Album: "",
  65. Duration: 431,
  66. BasePath: "/path/to",
  67. RelativePath: "file.ogg",
  68. ModifiedDate: 8876,
  69. }))
  70. Expect(songs[1]).To(Equal(read.Song{
  71. TrackNumber: 11,
  72. Title: "Starman",
  73. Artist: "David Bowie",
  74. Album: "The Rise and Fall of Ziggy Stardust and the Spiders from Mars",
  75. Duration: 256,
  76. BasePath: "/different/path",
  77. RelativePath: "otherFile.ogg",
  78. ModifiedDate: 11883,
  79. }))
  80. })
  81. })
  82. Context("when there is already a file in the database with the same name", func() {
  83. BeforeEach(func() {
  84. db.MustExec(
  85. `
  86. insert into songs (title, artist, album, base_path, relative_path, modified_date)
  87. values ($1, $2, $3, $4, $5, $6)
  88. `,
  89. "my title",
  90. "my artist",
  91. "my album",
  92. "/path/to",
  93. "file.ogg",
  94. 7782,
  95. )
  96. testInsertSongs()
  97. })
  98. It("should not add an additional row for the same file", func() {
  99. var count int
  100. db.Get(&count, `
  101. select count(*) from songs
  102. where base_path = '/path/to' and relative_path = 'file.ogg'
  103. `)
  104. Expect(count).To(Equal(1))
  105. })
  106. It("should upsert the existing item", func() {
  107. var songs []read.Song
  108. db.Select(&songs, `
  109. select
  110. track_number
  111. ,title
  112. ,artist
  113. ,album
  114. ,duration
  115. ,base_path
  116. ,relative_path
  117. ,modified_date
  118. from songs
  119. where base_path = '/path/to' and relative_path = 'file.ogg'
  120. `)
  121. Expect(songs).To(HaveLen(1))
  122. var song = songs[0]
  123. Expect(song.TrackNumber).To(Equal(7))
  124. Expect(song.Title).To(Equal("Hey Jude"))
  125. Expect(song.Artist).To(Equal("The Beatles"))
  126. Expect(song.Album).To(Equal(""))
  127. Expect(song.Duration).To(Equal(431))
  128. Expect(song.ModifiedDate).To(Equal(int64(8876)))
  129. })
  130. })
  131. })
  132. })