scan_test.go 1.8 KB

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