scan_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package repository
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/felamaslen/go-music-player/pkg/db"
  7. "github.com/felamaslen/go-music-player/pkg/read"
  8. setup "github.com/felamaslen/go-music-player/pkg/testing"
  9. )
  10. func TestInsertMusicIntoDatabase(t *testing.T) {
  11. setup.PrepareDatabaseForTesting()
  12. songs := make(chan *read.Song)
  13. go func() {
  14. defer close(songs)
  15. songs <- &read.Song{
  16. Title: "Hey Jude",
  17. Artist: "The Beatles",
  18. Album: "",
  19. Duration: 431,
  20. DurationOk: true,
  21. BasePath: "/path/to",
  22. RelativePath: "file.ogg",
  23. }
  24. songs <- &read.Song{
  25. Title: "Starman",
  26. Artist: "David Bowie",
  27. Album: "The Rise and Fall of Ziggy Stardust and the Spiders from Mars",
  28. Duration: 256,
  29. DurationOk: true,
  30. BasePath: "/different/path",
  31. RelativePath: "otherFile.ogg",
  32. }
  33. }()
  34. InsertMusicIntoDatabase(songs)
  35. conn := db.GetConnection()
  36. rows, err := conn.Query(
  37. context.Background(),
  38. `
  39. select title, artist, album, duration, base_path, relative_path
  40. from songs
  41. order by title
  42. `,
  43. )
  44. assert.Nil(t, err)
  45. var row read.Song
  46. rows.Next()
  47. rows.Scan(&row.Title, &row.Artist, &row.Album, &row.Duration, &row.BasePath, &row.RelativePath)
  48. assert.Equal(t, read.Song{
  49. Title: "Hey Jude",
  50. Artist: "The Beatles",
  51. Album: "",
  52. Duration: 431,
  53. BasePath: "/path/to",
  54. RelativePath: "file.ogg",
  55. }, row)
  56. rows.Next()
  57. rows.Scan(&row.Title, &row.Artist, &row.Album, &row.Duration, &row.BasePath, &row.RelativePath)
  58. assert.Equal(t, read.Song{
  59. Title: "Starman",
  60. Artist: "David Bowie",
  61. Album: "The Rise and Fall of Ziggy Stardust and the Spiders from Mars",
  62. Duration: 256,
  63. BasePath: "/different/path",
  64. RelativePath: "otherFile.ogg",
  65. }, row)
  66. }