player_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. )
  9. var _ = Describe("Player repository", func() {
  10. db := database.GetConnection()
  11. var ids []int
  12. BeforeEach(func() {
  13. testing.PrepareDatabaseForTesting()
  14. rows, err := db.Queryx(
  15. `
  16. insert into songs (
  17. track_number
  18. ,title
  19. ,artist
  20. ,album
  21. ,duration
  22. ,modified_date
  23. ,base_path
  24. ,relative_path
  25. )
  26. select * from unnest(
  27. array[1, 1, 2, 4, 0, 0]
  28. ,array['T1', 'T2', 'T3', 'T4', '', '']
  29. ,array['AR1', 'AR2', 'AR1', 'AR1', '', '']
  30. ,array['AL1', 'AL2', 'AL1', 'AL3', '', '']
  31. ,array[100, 200, 300, 250, 103, 107]
  32. ,array[123, 456, 789, 120, 883, 1443]
  33. ,array['/music', '/music', '/music', '/music', '/music', '/music']
  34. ,array['file1.ogg', 'file2.ogg', 'file3.ogg', 'file4.ogg', 'file5.ogg', 'file6.ogg']
  35. )
  36. returning id
  37. `,
  38. )
  39. if err != nil {
  40. panic(err)
  41. }
  42. var id int
  43. ids = []int{}
  44. for rows.Next() {
  45. rows.Scan(&id)
  46. ids = append(ids, id)
  47. }
  48. rows.Close()
  49. })
  50. Describe("GetNextSong", func() {
  51. Context("when another song exists in the same album", func() {
  52. It("should return the correct song ID", func() {
  53. song, _ := repository.GetNextSong(db, ids[0])
  54. Expect(song.Id).To(Equal(ids[2]))
  55. })
  56. })
  57. Context("when another song exists from the same artist", func() {
  58. It("should return the correct song ID", func() {
  59. song, _ := repository.GetNextSong(db, ids[2])
  60. Expect(song.Id).To(Equal(ids[3]))
  61. })
  62. })
  63. Context("when another song exists by a different artist", func() {
  64. It("should return the correct song ID", func() {
  65. song, _ := repository.GetNextSong(db, ids[3])
  66. Expect(song.Id).To(Equal(ids[1]))
  67. })
  68. })
  69. Context("when no further songs exist", func() {
  70. It("should return zero", func() {
  71. song, _ := repository.GetNextSong(db, ids[1])
  72. Expect(song.Id).To(Equal(0))
  73. })
  74. })
  75. Context("when the song has no information", func() {
  76. It("should return the next song by ID", func() {
  77. song, _ := repository.GetNextSong(db, ids[4])
  78. Expect(song.Id).To(Equal(ids[5]))
  79. })
  80. })
  81. Context("when the ID does not exist", func() {
  82. It("should return nil", func() {
  83. song, err := repository.GetNextSong(db, 10000000)
  84. Expect(err).To(BeNil())
  85. Expect(song.Id).To(BeZero())
  86. })
  87. })
  88. })
  89. Describe("GetPrevSong", func() {
  90. Context("when another song exists in the same album", func() {
  91. It("should return the correct song ID", func() {
  92. song, _ := repository.GetPrevSong(db, ids[2])
  93. Expect(song.Id).To(Equal(ids[0]))
  94. })
  95. })
  96. Context("when another song exists from the same artist", func() {
  97. It("should return the correct song ID", func() {
  98. song, _ := repository.GetPrevSong(db, ids[3])
  99. Expect(song.Id).To(Equal(ids[2]))
  100. })
  101. })
  102. Context("when another song exists by a different artist", func() {
  103. It("should return the correct song ID", func() {
  104. song, _ := repository.GetPrevSong(db, ids[1])
  105. Expect(song.Id).To(Equal(ids[3]))
  106. })
  107. })
  108. Context("when the song has no information", func() {
  109. It("should return the previous song by ID", func() {
  110. song, _ := repository.GetPrevSong(db, ids[5])
  111. Expect(song.Id).To(Equal(ids[4]))
  112. })
  113. })
  114. Context("when no further songs exist", func() {
  115. It("should return zero", func() {
  116. song, _ := repository.GetPrevSong(db, ids[4])
  117. Expect(song.Id).To(Equal(0))
  118. })
  119. })
  120. Context("when the ID does not exist", func() {
  121. It("should return nil", func() {
  122. song, err := repository.GetPrevSong(db, 10000000)
  123. Expect(err).To(BeNil())
  124. Expect(song.Id).To(BeZero())
  125. })
  126. })
  127. })
  128. })