aboutsummaryrefslogtreecommitdiff
path: root/internal/db/main_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/db/main_test.go')
-rw-r--r--internal/db/main_test.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/internal/db/main_test.go b/internal/db/main_test.go
index d141d3cd..8b4190da 100644
--- a/internal/db/main_test.go
+++ b/internal/db/main_test.go
@@ -7,12 +7,17 @@ package db
import (
"flag"
"fmt"
+ "io/ioutil"
"os"
+ "path/filepath"
"testing"
+ "time"
"github.com/jinzhu/gorm"
log "unknwon.dev/clog/v2"
+ "gogs.io/gogs/internal/conf"
+ "gogs.io/gogs/internal/dbutil"
"gogs.io/gogs/internal/testutil"
)
@@ -27,6 +32,10 @@ func TestMain(m *testing.M) {
os.Exit(1)
}
}
+
+ now := time.Now().Truncate(time.Second)
+ gorm.NowFunc = func() time.Time { return now }
+
os.Exit(m.Run())
}
@@ -39,3 +48,38 @@ func deleteTables(db *gorm.DB, tables ...interface{}) error {
}
return nil
}
+
+func initTestDB(t *testing.T, suite string, tables ...interface{}) *gorm.DB {
+ t.Helper()
+
+ dbpath := filepath.Join(os.TempDir(), fmt.Sprintf("gogs-%s-%d.db", suite, time.Now().Unix()))
+ db, err := openDB(conf.DatabaseOpts{
+ Type: "sqlite3",
+ Path: dbpath,
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() {
+ _ = db.Close()
+
+ if t.Failed() {
+ t.Logf("Database %q left intact for inspection", dbpath)
+ return
+ }
+
+ _ = os.Remove(dbpath)
+ })
+
+ db.SingularTable(true)
+ if !testing.Verbose() {
+ db.SetLogger(&dbutil.Writer{Writer: ioutil.Discard})
+ }
+
+ err = db.AutoMigrate(tables...).Error
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ return db
+}