go: use enttest in tests
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
leo 2023-05-03 23:21:47 +02:00
parent 0ac668d001
commit 0a5d4e7d30
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
2 changed files with 29 additions and 49 deletions

@ -10,10 +10,12 @@ import (
"git.dotya.ml/mirre-mt/pcmt/app/settings"
"git.dotya.ml/mirre-mt/pcmt/config"
"git.dotya.ml/mirre-mt/pcmt/ent"
"git.dotya.ml/mirre-mt/pcmt/ent/enttest"
"git.dotya.ml/mirre-mt/pcmt/slogging"
)
const connstr = "file:ent_tests?mode=memory&_fk=1"
var conf = &config.Config{
Port: 3005,
AppName: "pcmt-test",
@ -35,12 +37,16 @@ func TestStaticRoute(t *testing.T) {
templatesPath: "../templates",
assetsPath: "../assets",
}
db := supplyDBClient()
log := slogging.GetLogger()
log := slogging.Init(false)
tstRoute := "/static/"
db := enttest.Open(t, "sqlite3", connstr)
defer db.Close()
if err := db.Schema.Create(context.Background()); err != nil {
t.Fatalf("failed creating schema resources: %v", err)
}
*host = ""
*port = 3500
*devel = true
@ -84,11 +90,15 @@ func BenchmarkStatic(b *testing.B) {
templatesPath: "../templates",
assetsPath: "../assets",
}
db := supplyDBClient()
log := slogging.GetLogger()
log := slogging.Init(false)
db := enttest.Open(b, "sqlite3", connstr)
defer db.Close()
if err := db.Schema.Create(context.Background()); err != nil {
b.Fatalf("failed creating schema resources: %v", err)
}
setting.Consolidate(conf, host, port, devel, "bench")
err := a.Init(setting, log, db)
@ -112,11 +122,15 @@ func BenchmarkStatic2(b *testing.B) {
templatesPath: "../templates",
assetsPath: "../assets",
}
log := slogging.GetLogger()
db := supplyDBClient()
log := slogging.Init(false)
db := enttest.Open(b, "sqlite3", connstr)
defer db.Close()
if err := db.Schema.Create(context.Background()); err != nil {
b.Fatalf("failed creating schema resources: %v", err)
}
setting.Consolidate(conf, host, port, devel, "bench")
err := a.Init(setting, log, db)
@ -133,22 +147,3 @@ func BenchmarkStatic2(b *testing.B) {
a.E().ServeHTTP(rec, req)
}
}
func supplyDBClient() *ent.Client {
connstr := "file:ent_tests?mode=memory&cache=shared&_fk=1"
log := slogging.Init(false)
db, err := ent.Open("sqlite3", connstr)
if err != nil {
log.Infof("failed to open a connection to sqlite %q\n", err)
return nil
}
if err = db.Schema.Create(context.Background()); err != nil {
log.Infof("failed creating schema resources: %v", err)
return nil
}
return db
}

@ -2,10 +2,9 @@ package user
import (
"context"
"log"
"testing"
"git.dotya.ml/mirre-mt/pcmt/ent"
"git.dotya.ml/mirre-mt/pcmt/ent/enttest"
"git.dotya.ml/mirre-mt/pcmt/slogging"
_ "github.com/xiaoqidun/entps"
)
@ -13,12 +12,15 @@ import (
func TestUserExists(t *testing.T) {
t.Parallel()
db := supplyDBClient()
if db == nil {
t.Error("could not connect to db")
}
connstr := "file:ent_tests?mode=memory&_fk=1"
db := enttest.Open(t, "sqlite3", connstr)
defer db.Close()
if err := db.Schema.Create(context.Background()); err != nil {
t.Fatalf("failed creating schema resources: %v", err)
}
username := "dude"
email := "dude@b.cc"
ctx := getCtx()
@ -78,23 +80,6 @@ func TestUserExists(t *testing.T) {
}
}
func supplyDBClient() *ent.Client {
connstr := "file:ent_tests?mode=memory&cache=shared&_fk=1"
db, err := ent.Open("sqlite3", connstr)
if err != nil {
log.Printf("failed to open a connection to sqlite %q\n", err)
return nil
}
if err = db.Schema.Create(context.Background()); err != nil {
log.Printf("failed creating schema resources: %v", err)
return nil
}
return db
}
func getCtx() context.Context {
l := slogging.Init(false)