player_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. })
  76. Describe("GetPrevSongId", func() {
  77. Context("when another song exists in the same album", func() {
  78. It("should return the correct song ID", func() {
  79. id, _ := repository.GetPrevSongId(db, ids[2])
  80. Expect(id).To(Equal(ids[0]))
  81. })
  82. })
  83. Context("when another song exists from the same artist", func() {
  84. It("should return the correct song ID", func() {
  85. id, _ := repository.GetPrevSongId(db, ids[3])
  86. Expect(id).To(Equal(ids[2]))
  87. })
  88. })
  89. Context("when another song exists by a different artist", func() {
  90. It("should return the correct song ID", func() {
  91. id, _ := repository.GetPrevSongId(db, ids[1])
  92. Expect(id).To(Equal(ids[3]))
  93. })
  94. })
  95. Context("when no further songs exist", func() {
  96. It("should return zero", func() {
  97. id, _ := repository.GetPrevSongId(db, ids[0])
  98. Expect(id).To(Equal(int64(0)))
  99. })
  100. })
  101. })
  102. })