songs_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package repository_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/repository"
  8. setup "github.com/felamaslen/gmus-backend/pkg/testing"
  9. )
  10. var _ = Describe("songs repository", func() {
  11. db := database.GetConnection()
  12. BeforeEach(func() {
  13. setup.PrepareDatabaseForTesting()
  14. })
  15. Describe("SelectSong", func() {
  16. var id int64
  17. var id2 int64
  18. BeforeEach(func() {
  19. db.QueryRowx(
  20. `
  21. insert into songs (track_number, title, artist, album, duration, modified_date, base_path, relative_path)
  22. values ($1, $2, $3, $4, $5, $6, $7, $8)
  23. returning id
  24. `,
  25. 7,
  26. "Hey Jude",
  27. "The Beatles",
  28. "",
  29. 431,
  30. 8876,
  31. "/path/to",
  32. "file.ogg",
  33. ).Scan(&id)
  34. db.QueryRowx(
  35. `
  36. insert into songs (track_number, title, artist, album, duration, modified_date, base_path, relative_path)
  37. values ($1, $2, $3, $4, $5, $6, $7, $8)
  38. returning id
  39. `,
  40. 13,
  41. "Track 1",
  42. "Untitled Artist",
  43. "Some album",
  44. 218,
  45. 1993,
  46. "/path/different",
  47. "other.ogg",
  48. ).Scan(&id2)
  49. })
  50. It("should retrieve a song from the database", func() {
  51. Expect(id).NotTo(BeZero())
  52. result, err := repository.SelectSong(db, []int{int(id)})
  53. Expect(err).To(BeNil())
  54. Expect(*result).To(HaveLen(1))
  55. Expect((*result)[0]).To(Equal(&read.Song{
  56. Id: int(id),
  57. TrackNumber: 7,
  58. Title: "Hey Jude",
  59. Artist: "The Beatles",
  60. Album: "",
  61. Duration: 431,
  62. BasePath: "/path/to",
  63. RelativePath: "file.ogg",
  64. ModifiedDate: 8876,
  65. }))
  66. })
  67. It("should retrieve multiple songs from the database", func() {
  68. Expect(id).NotTo(BeZero())
  69. Expect(id2).NotTo(BeZero())
  70. result, err := repository.SelectSong(db, []int{int(id), int(id2)})
  71. Expect(err).To(BeNil())
  72. Expect(*result).To(HaveLen(2))
  73. Expect((*result)[0]).To(Equal(&read.Song{
  74. Id: int(id),
  75. TrackNumber: 7,
  76. Title: "Hey Jude",
  77. Artist: "The Beatles",
  78. Album: "",
  79. Duration: 431,
  80. BasePath: "/path/to",
  81. RelativePath: "file.ogg",
  82. ModifiedDate: 8876,
  83. }))
  84. Expect((*result)[1]).To(Equal(&read.Song{
  85. Id: int(id2),
  86. TrackNumber: 13,
  87. Title: "Track 1",
  88. Artist: "Untitled Artist",
  89. Album: "Some album",
  90. Duration: 218,
  91. BasePath: "/path/different",
  92. RelativePath: "other.ogg",
  93. ModifiedDate: 1993,
  94. }))
  95. })
  96. Context("when the song does not exist", func() {
  97. It("should return an empty array", func() {
  98. result, err := repository.SelectSong(db, []int{88113})
  99. Expect(err).To(BeNil())
  100. Expect(*result).To(HaveLen(0))
  101. })
  102. })
  103. })
  104. Describe("BatchUpsertSongs", func() {
  105. songs := [100]*read.Song{
  106. {
  107. TrackNumber: 1,
  108. Title: "Title A",
  109. Artist: "Artist A",
  110. Album: "Album A",
  111. Duration: 123,
  112. BasePath: "/base/path/1",
  113. RelativePath: "song1.ogg",
  114. ModifiedDate: 8886663103,
  115. },
  116. {
  117. TrackNumber: 2,
  118. Title: "Title B",
  119. Artist: "Artist B",
  120. Album: "Album B",
  121. Duration: 456,
  122. BasePath: "/base/path/2",
  123. RelativePath: "song2.ogg",
  124. ModifiedDate: 2711291992,
  125. },
  126. }
  127. Context("when the songs do not already exist", func() {
  128. BeforeEach(func() {
  129. repository.BatchUpsertSongs(db, &songs, 2)
  130. })
  131. It("should insert the batch into the database", func() {
  132. var result []*read.Song
  133. db.Select(&result, `
  134. select track_number, title, artist, album, duration, base_path, relative_path, modified_date
  135. from songs
  136. `)
  137. Expect(result).To(HaveLen(2))
  138. Expect(songs[0]).To(BeElementOf(result))
  139. Expect(songs[1]).To(BeElementOf(result))
  140. })
  141. })
  142. Context("when the songs already exist", func() {
  143. var result []*read.Song
  144. var modifiedBatch [100]*read.Song
  145. modifiedBatch[0] = songs[0]
  146. modifiedBatch[1] = songs[1]
  147. modifiedBatch[0].Title = "Title A modified"
  148. BeforeEach(func() {
  149. repository.BatchUpsertSongs(db, &songs, 2)
  150. repository.BatchUpsertSongs(db, &modifiedBatch, 2)
  151. result = []*read.Song{}
  152. db.Select(&result, `
  153. select track_number, title, artist, album, duration, base_path, relative_path, modified_date
  154. from songs
  155. where relative_path in ('song1.ogg', 'song2.ogg')
  156. `)
  157. })
  158. It("should not create any additional rows", func() {
  159. Expect(result).To(HaveLen(2))
  160. })
  161. It("should update the rows with any changes", func() {
  162. Expect(modifiedBatch[0]).To(BeElementOf(result))
  163. })
  164. })
  165. })
  166. })