scan_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 song read.Song
  53. rows, _ := db.Queryx(`
  54. select title, artist, album, duration, base_path, relative_path, modified_date
  55. from songs
  56. order by title
  57. `)
  58. rows.Next()
  59. rows.StructScan(&song)
  60. Expect(song).To(Equal(read.Song{
  61. Title: "Hey Jude",
  62. Artist: "The Beatles",
  63. Album: "",
  64. Duration: 431,
  65. BasePath: "/path/to",
  66. RelativePath: "file.ogg",
  67. ModifiedDate: 8876,
  68. }))
  69. rows.Next()
  70. rows.StructScan(&song)
  71. Expect(song).To(Equal(read.Song{
  72. Title: "Starman",
  73. Artist: "David Bowie",
  74. Album: "The Rise and Fall of Ziggy Stardust and the Spiders from Mars",
  75. Duration: 256,
  76. BasePath: "/different/path",
  77. RelativePath: "otherFile.ogg",
  78. ModifiedDate: 11883,
  79. }))
  80. rows.Close()
  81. })
  82. })
  83. Context("when there is already a file in the database with the same name", func() {
  84. BeforeEach(func() {
  85. db.MustExec(
  86. `
  87. insert into songs (title, artist, album, base_path, relative_path, modified_date)
  88. values ($1, $2, $3, $4, $5, $6)
  89. `,
  90. "my title",
  91. "my artist",
  92. "my album",
  93. "/path/to",
  94. "file.ogg",
  95. 7782,
  96. )
  97. testInsertSongs()
  98. })
  99. It("should not add an additional row for the same file", func() {
  100. var count int
  101. db.Get(&count, `
  102. select count(*) from songs
  103. where base_path = '/path/to' and relative_path = 'file.ogg'
  104. `)
  105. Expect(count).To(Equal(1))
  106. })
  107. It("should upsert the existing item", func() {
  108. rows, _ := db.Queryx(`
  109. select title, artist, album, duration, base_path, relative_path, modified_date from songs
  110. where base_path = '/path/to' and relative_path = 'file.ogg'
  111. `)
  112. var song read.Song
  113. rows.Next()
  114. rows.StructScan(&song)
  115. Expect(song.Title).To(Equal("Hey Jude"))
  116. Expect(song.Artist).To(Equal("The Beatles"))
  117. Expect(song.Album).To(Equal(""))
  118. Expect(song.Duration).To(Equal(431))
  119. Expect(song.ModifiedDate).To(Equal(int64(8876)))
  120. rows.Close()
  121. })
  122. })
  123. })
  124. })