scan_test.go 3.4 KB

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