homebox/backend/internal/repo/main_test.go

63 lines
1 KiB
Go
Raw Normal View History

2022-08-30 02:30:36 +00:00
package repo
import (
"context"
"log"
"math/rand"
"os"
"testing"
"time"
2022-08-30 02:40:54 +00:00
"github.com/hay-kot/content/backend/ent"
"github.com/hay-kot/content/backend/pkgs/faker"
2022-08-30 02:30:36 +00:00
_ "github.com/mattn/go-sqlite3"
)
2022-08-31 03:21:18 +00:00
var (
fk = faker.NewFaker()
tClient *ent.Client
tRepos *AllRepos
tUser *ent.User
tGroup *ent.Group
2022-08-31 03:21:18 +00:00
)
func bootstrap() {
var (
err error
ctx = context.Background()
)
2022-08-31 03:21:18 +00:00
tGroup, err = tRepos.Groups.Create(ctx, "test-group")
if err != nil {
log.Fatal(err)
2022-08-31 03:21:18 +00:00
}
tUser, err = tRepos.Users.Create(ctx, userFactory())
if err != nil {
log.Fatal(err)
}
2022-08-31 03:21:18 +00:00
}
2022-08-30 02:30:36 +00:00
func TestMain(m *testing.M) {
rand.Seed(int64(time.Now().Unix()))
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
err = client.Schema.Create(context.Background())
if err != nil {
2022-08-30 02:30:36 +00:00
log.Fatalf("failed creating schema resources: %v", err)
}
tClient = client
tRepos = EntAllRepos(tClient)
2022-08-30 02:30:36 +00:00
defer client.Close()
2022-08-31 03:21:18 +00:00
bootstrap()
2022-08-30 02:30:36 +00:00
os.Exit(m.Run())
}