fetch_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package server_test
  2. import (
  3. "fmt"
  4. "github.com/lib/pq"
  5. . "github.com/onsi/ginkgo"
  6. . "github.com/onsi/gomega"
  7. "github.com/felamaslen/gmus-backend/pkg/database"
  8. "github.com/felamaslen/gmus-backend/pkg/server"
  9. setup "github.com/felamaslen/gmus-backend/pkg/testing"
  10. )
  11. var _ = Describe("Fetching functions", func() {
  12. db := database.GetConnection()
  13. BeforeEach(func() {
  14. setup.PrepareDatabaseForTesting()
  15. })
  16. Describe("GetPagedArtists", func() {
  17. var insertArtists = func(artists []string) {
  18. var trackNumbers = make([]int, len(artists))
  19. var titles = make([]string, len(artists))
  20. var albums = make([]string, len(artists))
  21. var durations = make([]int, len(artists))
  22. var basePaths = make([]string, len(artists))
  23. var relativePaths = make([]string, len(artists))
  24. var modifiedDates = make([]int, len(artists))
  25. for i := 0; i < len(artists); i++ {
  26. trackNumbers[i] = i + 1
  27. titles[i] = fmt.Sprintf("Title %d", i+1)
  28. albums[i] = fmt.Sprintf("Album %d", i+1)
  29. durations[i] = 403 + i
  30. basePaths[i] = "/music"
  31. relativePaths[i] = fmt.Sprintf("file%d.ogg", i)
  32. modifiedDates[i] = 177712347 + i
  33. }
  34. db.MustExec(
  35. `
  36. insert into songs (
  37. track_number
  38. ,title
  39. ,artist
  40. ,album
  41. ,duration
  42. ,modified_date
  43. ,base_path
  44. ,relative_path
  45. )
  46. select * from unnest(
  47. $1::integer[]
  48. ,$2::varchar[]
  49. ,$3::varchar[]
  50. ,$4::varchar[]
  51. ,$5::integer[]
  52. ,$6::bigint[]
  53. ,$7::varchar[]
  54. ,$8::varchar[]
  55. )
  56. `,
  57. pq.Array(trackNumbers),
  58. pq.Array(titles),
  59. pq.Array(artists),
  60. pq.Array(albums),
  61. pq.Array(durations),
  62. pq.Array(modifiedDates),
  63. pq.Array(basePaths),
  64. pq.Array(relativePaths),
  65. )
  66. }
  67. Context("when there are no songs", func() {
  68. It("should return an empty slice and set more to false", func() {
  69. artists, more := server.GetPagedArtists(100, 0)
  70. Expect(*artists).To(HaveLen(0))
  71. Expect(more).To(BeFalse())
  72. })
  73. })
  74. Context("when there are no songs with artists", func() {
  75. BeforeEach(func() {
  76. insertArtists([]string{"", ""})
  77. })
  78. It("should return an empty string", func() {
  79. artists, more := server.GetPagedArtists(100, 0)
  80. Expect(*artists).To(HaveLen(1))
  81. Expect((*artists)[0]).To(Equal(""))
  82. Expect(more).To(BeFalse())
  83. })
  84. })
  85. Context("when there are fewer artists than the limit given", func() {
  86. BeforeEach(func() {
  87. insertArtists([]string{"Artist A", "Artist B", "Artist C", "Artist D"})
  88. })
  89. It("should return an ordered set matching the limit", func() {
  90. artists, _ := server.GetPagedArtists(3, 0)
  91. Expect(*artists).To(HaveLen(3))
  92. Expect((*artists)[0]).To(Equal("Artist A"))
  93. Expect((*artists)[1]).To(Equal("Artist B"))
  94. Expect((*artists)[2]).To(Equal("Artist C"))
  95. })
  96. It("should set more to true", func() {
  97. _, more := server.GetPagedArtists(3, 0)
  98. Expect(more).To(BeTrue())
  99. })
  100. Context("when paging", func() {
  101. It("should return the next set of results", func() {
  102. artists, _ := server.GetPagedArtists(3, 1)
  103. Expect(*artists).To(HaveLen(1))
  104. Expect((*artists)[0]).To(Equal("Artist D"))
  105. })
  106. It("should set more to false at the end", func() {
  107. _, more := server.GetPagedArtists(3, 1)
  108. Expect(more).To(BeFalse())
  109. })
  110. })
  111. })
  112. })
  113. })