player_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 []int64
  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]
  28. ,array['T1', 'T2', 'T3', 'T4']
  29. ,array['AR1', 'AR2', 'AR1', 'AR1']
  30. ,array['AL1', 'AL2', 'AL1', 'AL3']
  31. ,array[100, 200, 300, 250]
  32. ,array[123, 456, 789, 120]
  33. ,array['/music', '/music', '/music', '/music']
  34. ,array['file1.ogg', 'file2.ogg', 'file3.ogg', 'file4.ogg']
  35. )
  36. returning id
  37. `,
  38. )
  39. if err != nil {
  40. panic(err)
  41. }
  42. var id int64
  43. ids = []int64{}
  44. for rows.Next() {
  45. rows.Scan(&id)
  46. ids = append(ids, id)
  47. }
  48. rows.Close()
  49. })
  50. Describe("GetNextSongId", func() {
  51. Context("when another song exists in the same album", func() {
  52. It("should return the correct song ID", func() {
  53. id, _ := repository.GetNextSongId(db, ids[0])
  54. Expect(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. id, _ := repository.GetNextSongId(db, ids[2])
  60. Expect(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. id, _ := repository.GetNextSongId(db, ids[3])
  66. Expect(id).To(Equal(ids[1]))
  67. })
  68. })
  69. Context("when no further songs exist", func() {
  70. It("should return zero", func() {
  71. id, _ := repository.GetNextSongId(db, ids[1])
  72. Expect(id).To(Equal(int64(0)))
  73. })
  74. })
  75. Context("when the ID does not exist", func() {
  76. It("should return nil", func() {
  77. id, err := repository.GetNextSongId(db, 10000000)
  78. Expect(err).To(BeNil())
  79. Expect(id).To(BeZero())
  80. })
  81. })
  82. })
  83. Describe("GetPrevSongId", func() {
  84. Context("when another song exists in the same album", func() {
  85. It("should return the correct song ID", func() {
  86. id, _ := repository.GetPrevSongId(db, ids[2])
  87. Expect(id).To(Equal(ids[0]))
  88. })
  89. })
  90. Context("when another song exists from the same artist", func() {
  91. It("should return the correct song ID", func() {
  92. id, _ := repository.GetPrevSongId(db, ids[3])
  93. Expect(id).To(Equal(ids[2]))
  94. })
  95. })
  96. Context("when another song exists by a different artist", func() {
  97. It("should return the correct song ID", func() {
  98. id, _ := repository.GetPrevSongId(db, ids[1])
  99. Expect(id).To(Equal(ids[3]))
  100. })
  101. })
  102. Context("when no further songs exist", func() {
  103. It("should return zero", func() {
  104. id, _ := repository.GetPrevSongId(db, ids[0])
  105. Expect(id).To(Equal(int64(0)))
  106. })
  107. })
  108. Context("when the ID does not exist", func() {
  109. It("should return nil", func() {
  110. id, err := repository.GetPrevSongId(db, 10000000)
  111. Expect(err).To(BeNil())
  112. Expect(id).To(BeZero())
  113. })
  114. })
  115. })
  116. })