scan_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 TestInsertMusicIntoDatabase(t *testing.T) {
  9. conn := GetConnection()
  10. conn.Query(
  11. context.Background(),
  12. "truncate table songs",
  13. )
  14. defer conn.Close(context.Background())
  15. songs := make(chan *read.Song)
  16. go func() {
  17. defer close(songs)
  18. songs <- &read.Song{
  19. Title: "Hey Jude",
  20. Artist: "The Beatles",
  21. Album: "",
  22. Duration: 431,
  23. DurationOk: true,
  24. BasePath: "/path/to",
  25. RelativePath: "file.ogg",
  26. }
  27. }()
  28. InsertMusicIntoDatabase(songs)
  29. conn = GetConnection()
  30. type Row struct {
  31. id int
  32. title string
  33. artist string
  34. album string
  35. duration int
  36. base_path string
  37. relative_path string
  38. }
  39. var row Row
  40. err := conn.QueryRow(
  41. context.Background(),
  42. "select * from songs",
  43. ).Scan(&row.id, &row.title, &row.artist, &row.album, &row.duration, &row.base_path, &row.relative_path)
  44. assert.Nil(t, err)
  45. assert.Equal(t, row.title, "Hey Jude")
  46. assert.Equal(t, row.artist, "The Beatles")
  47. assert.Equal(t, row.album, "")
  48. assert.Equal(t, row.duration, 431)
  49. assert.Equal(t, row.base_path, "/path/to")
  50. assert.Equal(t, row.relative_path, "file.ogg")
  51. }