testing.go 652 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package testing
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path"
  7. "runtime"
  8. "github.com/felamaslen/go-music-player/pkg/db"
  9. "github.com/jackc/pgx/v4/pgxpool"
  10. )
  11. func ChangeToRootDir() {
  12. _, filename, _, _ := runtime.Caller(0)
  13. dir := path.Join(path.Dir(filename), "../..")
  14. fmt.Printf("Changing dir to %v\n", dir)
  15. err := os.Chdir(dir)
  16. if err != nil {
  17. panic(err)
  18. }
  19. }
  20. func init() {
  21. ChangeToRootDir()
  22. }
  23. func PrepareDatabaseForTesting() *pgxpool.Conn {
  24. fmt.Println("Preparing database for testing")
  25. db.MigrateDatabase()
  26. conn := db.GetConnection()
  27. conn.Query(
  28. context.Background(),
  29. "truncate table songs",
  30. )
  31. return conn
  32. }