testing.go 511 B

1234567891011121314151617181920212223242526272829303132
  1. package testing
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "runtime"
  7. "github.com/felamaslen/go-music-player/pkg/database"
  8. )
  9. func ChangeToRootDir() {
  10. _, filename, _, _ := runtime.Caller(0)
  11. dir := path.Join(path.Dir(filename), "../..")
  12. fmt.Printf("Changing dir to %v\n", dir)
  13. err := os.Chdir(dir)
  14. if err != nil {
  15. panic(err)
  16. }
  17. }
  18. func init() {
  19. ChangeToRootDir()
  20. }
  21. func PrepareDatabaseForTesting() {
  22. database.MigrateDatabase()
  23. db := database.GetConnection()
  24. db.MustExec("truncate table songs")
  25. }