songs_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/read"
  7. "github.com/felamaslen/gmus-backend/pkg/repository"
  8. setup "github.com/felamaslen/gmus-backend/pkg/testing"
  9. )
  10. var _ = Describe("songs repository", func() {
  11. db := database.GetConnection()
  12. BeforeEach(func() {
  13. setup.PrepareDatabaseForTesting()
  14. })
  15. Describe("SelectSong", func() {
  16. var id int64
  17. BeforeEach(func() {
  18. db.QueryRowx(
  19. `
  20. insert into songs (track_number, title, artist, album, duration, modified_date, base_path, relative_path)
  21. values ($1, $2, $3, $4, $5, $6, $7, $8)
  22. returning id
  23. `,
  24. 7,
  25. "Hey Jude",
  26. "The Beatles",
  27. "",
  28. 431,
  29. 8876,
  30. "/path/to",
  31. "file.ogg",
  32. ).Scan(&id)
  33. })
  34. It("should retrieve a song from the database", func() {
  35. Expect(id).NotTo(BeZero())
  36. result, err := repository.SelectSong(db, int(id))
  37. Expect(err).To(BeNil())
  38. Expect(result).To(Equal(&read.Song{
  39. TrackNumber: 7,
  40. Title: "Hey Jude",
  41. Artist: "The Beatles",
  42. Album: "",
  43. Duration: 431,
  44. BasePath: "/path/to",
  45. RelativePath: "file.ogg",
  46. ModifiedDate: 8876,
  47. }))
  48. })
  49. Context("when the song does not exist", func() {
  50. It("should return an error", func() {
  51. result, err := repository.SelectSong(db, 88113)
  52. Expect(err).To(MatchError("No such ID"))
  53. Expect(result).To(BeNil())
  54. })
  55. })
  56. })
  57. Describe("BatchUpsertSongs", func() {
  58. songs := [100]*read.Song{
  59. {
  60. TrackNumber: 1,
  61. Title: "Title A",
  62. Artist: "Artist A",
  63. Album: "Album A",
  64. Duration: 123,
  65. BasePath: "/base/path/1",
  66. RelativePath: "song1.ogg",
  67. ModifiedDate: 8886663103,
  68. },
  69. {
  70. TrackNumber: 2,
  71. Title: "Title B",
  72. Artist: "Artist B",
  73. Album: "Album B",
  74. Duration: 456,
  75. BasePath: "/base/path/2",
  76. RelativePath: "song2.ogg",
  77. ModifiedDate: 2711291992,
  78. },
  79. }
  80. Context("when the songs do not already exist", func() {
  81. BeforeEach(func() {
  82. repository.BatchUpsertSongs(db, &songs, 2)
  83. })
  84. It("should insert the batch into the database", func() {
  85. var result []*read.Song
  86. db.Select(&result, `
  87. select track_number, title, artist, album, duration, base_path, relative_path, modified_date
  88. from songs
  89. `)
  90. Expect(result).To(HaveLen(2))
  91. Expect(songs[0]).To(BeElementOf(result))
  92. Expect(songs[1]).To(BeElementOf(result))
  93. })
  94. })
  95. Context("when the songs already exist", func() {
  96. var result []*read.Song
  97. var modifiedBatch [100]*read.Song
  98. modifiedBatch[0] = songs[0]
  99. modifiedBatch[1] = songs[1]
  100. modifiedBatch[0].Title = "Title A modified"
  101. BeforeEach(func() {
  102. repository.BatchUpsertSongs(db, &songs, 2)
  103. repository.BatchUpsertSongs(db, &modifiedBatch, 2)
  104. db.Select(&result, `
  105. select track_number, title, artist, album, duration, base_path, relative_path, modified_date
  106. from songs
  107. `)
  108. })
  109. It("should not create any additional rows", func() {
  110. Expect(result).To(HaveLen(2))
  111. })
  112. It("should update the rows with any changes", func() {
  113. Expect(modifiedBatch[0]).To(BeElementOf(result))
  114. })
  115. })
  116. })
  117. })