2022-09-06 19:15:07 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hay-kot/content/backend/ent"
|
|
|
|
"github.com/hay-kot/content/backend/internal/repo"
|
|
|
|
"github.com/hay-kot/content/backend/internal/types"
|
|
|
|
"github.com/hay-kot/content/backend/pkgs/faker"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
fk = faker.NewFaker()
|
|
|
|
|
|
|
|
tClient *ent.Client
|
|
|
|
tRepos *repo.AllRepos
|
|
|
|
tUser *ent.User
|
|
|
|
tGroup *ent.Group
|
2022-09-12 22:47:27 +00:00
|
|
|
tSvc *AllServices
|
2022-09-06 19:15:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func bootstrap() {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
ctx = context.Background()
|
|
|
|
)
|
|
|
|
|
|
|
|
tGroup, err = tRepos.Groups.Create(ctx, "test-group")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tUser, err = tRepos.Users.Create(ctx, types.UserCreate{
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Email: fk.Email(),
|
|
|
|
Password: fk.Str(10),
|
|
|
|
IsSuperuser: fk.Bool(),
|
2022-09-06 19:15:07 +00:00
|
|
|
GroupID: tGroup.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Fatalf("failed creating schema resources: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tClient = client
|
|
|
|
tRepos = repo.EntAllRepos(tClient)
|
2022-09-12 22:47:27 +00:00
|
|
|
tSvc = NewServices(tRepos)
|
2022-09-06 19:15:07 +00:00
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
bootstrap()
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|