build source
This commit is contained in:
commit
ee1fec43ed
4171 changed files with 1351288 additions and 0 deletions
57
internal/store/testhelper_test.go
Normal file
57
internal/store/testhelper_test.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
var testPool *pgxpool.Pool
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
postgres := embeddedpostgres.NewDatabase(
|
||||
embeddedpostgres.DefaultConfig().
|
||||
Port(15432).
|
||||
Database("devpod_test"),
|
||||
)
|
||||
if err := postgres.Start(); err != nil {
|
||||
log.Fatalf("start embedded postgres: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, "postgres://postgres:postgres@localhost:15432/devpod_test?sslmode=disable")
|
||||
if err != nil {
|
||||
postgres.Stop()
|
||||
log.Fatalf("connect to test db: %v", err)
|
||||
}
|
||||
testPool = pool
|
||||
|
||||
code := m.Run()
|
||||
|
||||
pool.Close()
|
||||
if err := postgres.Stop(); err != nil {
|
||||
log.Printf("stop embedded postgres: %v", err)
|
||||
}
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func newTestStore(t *testing.T) *Store {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
s := New(testPool)
|
||||
if err := s.Migrate(ctx); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
// Clean tables for test isolation (order matters: foreign keys)
|
||||
for _, table := range []string{"runners", "usage_records", "api_keys", "users"} {
|
||||
if _, err := testPool.Exec(ctx, fmt.Sprintf("DELETE FROM %s", table)); err != nil {
|
||||
t.Fatalf("clean %s: %v", table, err)
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue