scan_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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("scanning repository", func() {
  11. db := database.GetConnection()
  12. BeforeEach(func() {
  13. setup.PrepareDatabaseForTesting()
  14. })
  15. Describe("when the channel sends two files", func() {
  16. var songs chan *read.Song
  17. var testInsertSongs = func() {
  18. songs = make(chan *read.Song)
  19. go func() {
  20. defer close(songs)
  21. songs <- &read.Song{
  22. Title: "Hey Jude",
  23. Artist: "The Beatles",
  24. Album: "",
  25. Duration: 431,
  26. DurationOk: true,
  27. BasePath: "/path/to",
  28. RelativePath: "file.ogg",
  29. ModifiedDate: 8876,
  30. }
  31. songs <- &read.Song{
  32. Title: "Starman",
  33. Artist: "David Bowie",
  34. Album: "The Rise and Fall of Ziggy Stardust and the Spiders from Mars",
  35. Duration: 256,
  36. DurationOk: true,
  37. BasePath: "/different/path",
  38. RelativePath: "otherFile.ogg",
  39. ModifiedDate: 11883,
  40. }
  41. }()
  42. repository.InsertMusicIntoDatabase(songs)
  43. }
  44. Context("when the songs do not already exist in the database", func() {
  45. BeforeEach(testInsertSongs)
  46. It("should insert the correct number of songs", func() {
  47. var count int
  48. db.Get(&count, "select count(*) from songs")
  49. Expect(count).To(Equal(2))
  50. })
  51. It("should insert both songs", func() {
  52. var songs []read.Song
  53. db.Select(&songs, `
  54. select title, artist, album, duration, base_path, relative_path, modified_date
  55. from songs
  56. order by title
  57. `)
  58. Expect(songs[0]).To(Equal(read.Song{
  59. Title: "Hey Jude",
  60. Artist: "The Beatles",
  61. Album: "",
  62. Duration: 431,
  63. BasePath: "/path/to",
  64. RelativePath: "file.ogg",
  65. ModifiedDate: 8876,
  66. }))
  67. Expect(songs[1]).To(Equal(read.Song{
  68. Title: "Starman",
  69. Artist: "David Bowie",
  70. Album: "The Rise and Fall of Ziggy Stardust and the Spiders from Mars",
  71. Duration: 256,
  72. BasePath: "/different/path",
  73. RelativePath: "otherFile.ogg",
  74. ModifiedDate: 11883,
  75. }))
  76. })
  77. })
  78. Context("when there is already a file in the database with the same name", func() {
  79. BeforeEach(func() {
  80. db.MustExec(
  81. `
  82. insert into songs (title, artist, album, base_path, relative_path, modified_date)
  83. values ($1, $2, $3, $4, $5, $6)
  84. `,
  85. "my title",
  86. "my artist",
  87. "my album",
  88. "/path/to",
  89. "file.ogg",
  90. 7782,
  91. )
  92. testInsertSongs()
  93. })
  94. It("should not add an additional row for the same file", func() {
  95. var count int
  96. db.Get(&count, `
  97. select count(*) from songs
  98. where base_path = '/path/to' and relative_path = 'file.ogg'
  99. `)
  100. Expect(count).To(Equal(1))
  101. })
  102. It("should upsert the existing item", func() {
  103. var songs []read.Song
  104. db.Select(&songs, `
  105. select title, artist, album, duration, base_path, relative_path, modified_date from songs
  106. where base_path = '/path/to' and relative_path = 'file.ogg'
  107. `)
  108. Expect(songs).To(HaveLen(1))
  109. var song = songs[0]
  110. Expect(song.Title).To(Equal("Hey Jude"))
  111. Expect(song.Artist).To(Equal("The Beatles"))
  112. Expect(song.Album).To(Equal(""))
  113. Expect(song.Duration).To(Equal(431))
  114. Expect(song.ModifiedDate).To(Equal(int64(8876)))
  115. })
  116. })
  117. })
  118. })