player_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. })