songs_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package repository_test
  2. import (
  3. . "github.com/onsi/ginkgo"
  4. . "github.com/onsi/gomega"
  5. "github.com/felamaslen/go-music-player/pkg/database"
  6. "github.com/felamaslen/go-music-player/pkg/read"
  7. "github.com/felamaslen/go-music-player/pkg/repository"
  8. setup "github.com/felamaslen/go-music-player/pkg/testing"
  9. )
  10. var _ = Describe("songs repository", func() {
  11. db := database.GetConnection()
  12. BeforeEach(func() {
  13. setup.PrepareDatabaseForTesting()
  14. })
  15. Context("when reading", 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. })