songs_test.go 4.9 KB

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