player_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. "github.com/felamaslen/gmus-backend/pkg/testing"
  8. "github.com/felamaslen/gmus-backend/pkg/types"
  9. )
  10. var _ = Describe("Player repository", func() {
  11. db := database.GetConnection()
  12. var ids []int
  13. BeforeEach(func() {
  14. testing.PrepareDatabaseForTesting()
  15. rows, err := db.Queryx(
  16. `
  17. insert into songs (
  18. track_number
  19. ,title
  20. ,artist
  21. ,album
  22. ,duration
  23. ,modified_date
  24. ,base_path
  25. ,relative_path
  26. )
  27. select * from unnest(
  28. array[1, 1, 2, 4, 0, 0]
  29. ,array['T1', 'T2', 'T3', 'T4', '', '']
  30. ,array['AR1', 'AR2', 'AR1', 'AR1', '', '']
  31. ,array['AL1', 'AL2', 'AL1', 'AL3', '', '']
  32. ,array[100, 200, 300, 250, 103, 107]
  33. ,array[123, 456, 789, 120, 883, 1443]
  34. ,array['/music', '/music', '/music', '/music', '/music', '/music']
  35. ,array['file1.ogg', 'file2.ogg', 'file3.ogg', 'file4.ogg', 'file5.ogg', 'file6.ogg']
  36. )
  37. returning id
  38. `,
  39. )
  40. if err != nil {
  41. panic(err)
  42. }
  43. var id int
  44. ids = []int{}
  45. for rows.Next() {
  46. rows.Scan(&id)
  47. ids = append(ids, id)
  48. }
  49. rows.Close()
  50. })
  51. Describe("GetNextSong", func() {
  52. Context("when another song exists in the same album", func() {
  53. It("should return the correct song ID", func() {
  54. song, _ := repository.GetNextSong(db, ids[0])
  55. Expect(song.Id).To(Equal(ids[2]))
  56. })
  57. })
  58. Context("when another song exists from the same artist", func() {
  59. It("should return the correct song ID", func() {
  60. song, _ := repository.GetNextSong(db, ids[2])
  61. Expect(song.Id).To(Equal(ids[3]))
  62. })
  63. })
  64. Context("when another song exists by a different artist", func() {
  65. It("should return the correct song ID", func() {
  66. song, _ := repository.GetNextSong(db, ids[3])
  67. Expect(song.Id).To(Equal(ids[1]))
  68. })
  69. })
  70. Context("when no further songs exist", func() {
  71. It("should return zero", func() {
  72. song, _ := repository.GetNextSong(db, ids[1])
  73. Expect(song.Id).To(Equal(0))
  74. })
  75. })
  76. Context("when the song has no information", func() {
  77. It("should return the next song by ID", func() {
  78. song, _ := repository.GetNextSong(db, ids[4])
  79. Expect(song.Id).To(Equal(ids[5]))
  80. })
  81. })
  82. Context("when the ID does not exist", func() {
  83. It("should return nil", func() {
  84. song, err := repository.GetNextSong(db, 10000000)
  85. Expect(err).To(BeNil())
  86. Expect(song.Id).To(BeZero())
  87. })
  88. })
  89. })
  90. Describe("GetPrevSong", func() {
  91. Context("when another song exists in the same album", func() {
  92. It("should return the correct song ID", func() {
  93. song, _ := repository.GetPrevSong(db, ids[2])
  94. Expect(song.Id).To(Equal(ids[0]))
  95. })
  96. })
  97. Context("when another song exists from the same artist", func() {
  98. It("should return the correct song ID", func() {
  99. song, _ := repository.GetPrevSong(db, ids[3])
  100. Expect(song.Id).To(Equal(ids[2]))
  101. })
  102. })
  103. Context("when another song exists by a different artist", func() {
  104. It("should return the correct song ID", func() {
  105. song, _ := repository.GetPrevSong(db, ids[1])
  106. Expect(song.Id).To(Equal(ids[3]))
  107. })
  108. })
  109. Context("when the song has no information", func() {
  110. It("should return the previous song by ID", func() {
  111. song, _ := repository.GetPrevSong(db, ids[5])
  112. Expect(song.Id).To(Equal(ids[4]))
  113. })
  114. })
  115. Context("when no further songs exist", func() {
  116. It("should return zero", func() {
  117. song, _ := repository.GetPrevSong(db, ids[4])
  118. Expect(song.Id).To(Equal(0))
  119. })
  120. })
  121. Context("when the ID does not exist", func() {
  122. It("should return nil", func() {
  123. song, err := repository.GetPrevSong(db, 10000000)
  124. Expect(err).To(BeNil())
  125. Expect(song.Id).To(BeZero())
  126. })
  127. })
  128. })
  129. Describe("GetShuffledSong", func() {
  130. It("should return a random song", func() {
  131. result, _ := repository.GetShuffledSong(db, &ids[0])
  132. Expect(result).NotTo(BeNil())
  133. Expect(result.Id).To(BeElementOf(ids))
  134. })
  135. It("should not return the given song", func() {
  136. // Iterate 10 times to be quite confident - it's not a mathematical proof
  137. // but it doesn't need to be
  138. for i := 0; i < 10; i++ {
  139. result0, _ := repository.GetShuffledSong(db, &ids[0])
  140. result4, _ := repository.GetShuffledSong(db, &ids[4])
  141. Expect(result0).NotTo(BeNil())
  142. Expect(result4).NotTo(BeNil())
  143. Expect(result0.Id).NotTo(Equal(ids[0]))
  144. Expect(result4.Id).NotTo(Equal(ids[4]))
  145. }
  146. })
  147. Context("when no currentSongId is given", func() {
  148. It("should return a random song", func() {
  149. result, _ := repository.GetShuffledSong(db, nil)
  150. Expect(result).NotTo(BeNil())
  151. Expect(result.Id).To(BeElementOf(ids))
  152. })
  153. })
  154. Context("when there are no songs in the database", func() {
  155. BeforeEach(func() {
  156. db.MustExec(`truncate songs`)
  157. })
  158. It("should return an empty result", func() {
  159. result, err := repository.GetShuffledSong(db, nil)
  160. Expect(err).To(BeNil())
  161. Expect(result).To(Equal(&types.Song{
  162. Id: 0,
  163. }))
  164. })
  165. })
  166. })
  167. })