Refactor User and DB handling #123
|
@ -22,9 +22,7 @@ import (
|
|||
"code.vikunja.io/api/pkg/log"
|
||||
"github.com/go-xorm/core"
|
||||
"github.com/go-xorm/xorm"
|
||||
"gopkg.in/testfixtures.v2"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// CreateTestEngine creates an instance of the db engine which lives in memory
|
||||
|
@ -55,19 +53,14 @@ func CreateTestEngine() (engine *xorm.Engine, err error) {
|
|||
}
|
||||
|
||||
// InitTestFixtures populates the db with all fixtures from the fixtures folder
|
||||
func InitTestFixtures() (err error) {
|
||||
func InitTestFixtures(tablenames ...string) (err error) {
|
||||
// Create all fixtures
|
||||
config.InitDefaultConfig()
|
||||
// We need to set the root path even if we're not using the config, otherwise fixtures are not loaded correctly
|
||||
config.ServiceRootpath.Set(os.Getenv("VIKUNJA_SERVICE_ROOTPATH"))
|
||||
|
||||
// Sync fixtures
|
||||
var fixturesHelper testfixtures.Helper = &testfixtures.SQLite{}
|
||||
if config.DatabaseType.GetString() == "mysql" {
|
||||
fixturesHelper = &testfixtures.MySQL{}
|
||||
}
|
||||
fixturesDir := filepath.Join(config.ServiceRootpath.GetString(), "pkg", "db", "fixtures")
|
||||
err = InitFixtures(fixturesHelper, fixturesDir)
|
||||
err = InitFixtures(tablenames...)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -18,15 +18,34 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
"gopkg.in/testfixtures.v2"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var fixtures *testfixtures.Context
|
||||
|
||||
// InitFixtures initialize test fixtures for a test database
|
||||
func InitFixtures(helper testfixtures.Helper, dir string) (err error) {
|
||||
func InitFixtures(tablenames ...string) (err error) {
|
||||
|
||||
var helper testfixtures.Helper = &testfixtures.SQLite{}
|
||||
if config.DatabaseType.GetString() == "mysql" {
|
||||
helper = &testfixtures.MySQL{}
|
||||
}
|
||||
dir := filepath.Join(config.ServiceRootpath.GetString(), "pkg", "db", "fixtures")
|
||||
|
||||
testfixtures.SkipDatabaseNameCheck(true)
|
||||
fixtures, err = testfixtures.NewFolder(x.DB().DB, helper, dir)
|
||||
|
||||
// If fixture table names are specified, load them
|
||||
// Otherwise, load all fixtures
|
||||
if len(tablenames) > 0 {
|
||||
for i, name := range tablenames {
|
||||
tablenames[i] = filepath.Join(dir, name+".yml")
|
||||
}
|
||||
fixtures, err = testfixtures.NewFiles(x.DB().DB, helper, tablenames...)
|
||||
} else {
|
||||
fixtures, err = testfixtures.NewFolder(x.DB().DB, helper, dir)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -71,7 +71,7 @@ func InitTests() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.InitTestFixtures()
|
||||
err = db.InitTestFixtures("files")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -39,7 +39,24 @@ func SetupTests() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.InitTestFixtures()
|
||||
err = db.InitTestFixtures(
|
||||
"label_task",
|
||||
"labels",
|
||||
"link_sharing",
|
||||
"list",
|
||||
"namespaces",
|
||||
"task_assignees",
|
||||
"task_attachments",
|
||||
"task_relations",
|
||||
"task_reminders",
|
||||
"tasks",
|
||||
"team_list",
|
||||
"team_members",
|
||||
"team_namespaces",
|
||||
"teams",
|
||||
"users",
|
||||
"users_list",
|
||||
"users_namespace")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ func InitTests() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.InitTestFixtures()
|
||||
err = db.InitTestFixtures("users")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue