Refactor User and DB handling #123

Merged
konrad merged 19 commits from refactor/user into master 2020-01-26 17:08:08 +00:00
6 changed files with 95 additions and 43 deletions
Showing only changes of commit 1c1632ec99 - Show all commits

View File

@ -23,7 +23,6 @@ import (
"fmt"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
"os"
"strconv"
"time"
@ -85,33 +84,6 @@ func CreateDBEngine() (engine *xorm.Engine, err error) {
return
}
// CreateTestEngine creates an instance of the db engine which lives in memory
func CreateTestEngine() (engine *xorm.Engine, err error) {
if x != nil {
return x, nil
}
if os.Getenv("VIKUNJA_TESTS_USE_CONFIG") == "1" {
config.InitConfig()
engine, err = CreateDBEngine()
if err != nil {
return nil, err
}
} else {
engine, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
if err != nil {
return nil, err
}
}
engine.SetMapper(core.GonicMapper{})
engine.ShowSQL(os.Getenv("UNIT_TESTS_VERBOSE") == "1")
engine.SetLogger(xorm.NewSimpleLogger(log.GetLogWriter("database")))
x = engine
return
}
// RegisterTableStructsForCache registers tables in gob encoding for redis cache
func RegisterTableStructsForCache(val interface{}) {
gob.Register(val)

88
pkg/db/test.go Normal file
View File

@ -0,0 +1,88 @@
// Copyright 2020 Vikunja and contriubtors. All rights reserved.
//
// This file is part of Vikunja.
//
// Vikunja is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vikunja is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vikunja. If not, see <https://www.gnu.org/licenses/>.
package db
import (
"code.vikunja.io/api/pkg/config"
"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
func CreateTestEngine() (engine *xorm.Engine, err error) {
if x != nil {
return x, nil
}
if os.Getenv("VIKUNJA_TESTS_USE_CONFIG") == "1" {
config.InitConfig()
engine, err = CreateDBEngine()
if err != nil {
return nil, err
}
} else {
engine, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
if err != nil {
return nil, err
}
}
engine.SetMapper(core.GonicMapper{})
engine.ShowSQL(os.Getenv("UNIT_TESTS_VERBOSE") == "1")
engine.SetLogger(xorm.NewSimpleLogger(log.GetLogWriter("database")))
x = engine
return
}
func InitTestFixtures() (err error) {
// Create the db schema
// migration.Migrate(x)
// 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)
if err != nil {
log.Fatal(err)
}
return nil
}
func InitTestDB() (x *xorm.Engine, err error) {
x, err = CreateTestEngine()
if err != nil {
return
}
err = InitTestFixtures()
return x, err
}

View File

@ -22,9 +22,7 @@ import (
"code.vikunja.io/api/pkg/log"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"gopkg.in/testfixtures.v2"
"os"
"path/filepath"
"testing"
)
@ -73,17 +71,7 @@ func InitTests() {
log.Fatal(err)
}
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", "files", "fixtures")
err = db.InitFixtures(fixturesHelper, fixturesDir)
err = db.InitTestFixtures()
if err != nil {
log.Fatal(err)
}

View File

@ -82,7 +82,7 @@ func setupTestEnv() (e *echo.Echo, err error) {
config.ServiceRootpath.Set(os.Getenv("VIKUNJA_SERVICE_ROOTPATH"))
// Some tests use the file engine, so we'll need to initialize that
files.InitTests()
models.SetupTests(config.ServiceRootpath.GetString())
models.SetupTests()
err = db.LoadFixtures()
if err != nil {

View File

@ -22,6 +22,8 @@ import (
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/modules/migration"
"code.vikunja.io/api/pkg/user"
"github.com/go-xorm/xorm"
"github.com/olekukonko/tablewriter"
"os"
@ -138,5 +140,7 @@ func initSchema(tx *xorm.Engine) error {
schemeBeans := []interface{}{}
schemeBeans = append(schemeBeans, models.GetTables()...)
schemeBeans = append(schemeBeans, files.GetTables()...)
schemeBeans = append(schemeBeans, migration.GetTables()...)
schemeBeans = append(schemeBeans, user.GetTables()...)
return tx.Sync2(schemeBeans...)
}

View File

@ -33,7 +33,7 @@ func TestMain(m *testing.M) {
// Some tests use the file engine, so we'll need to initialize that
files.InitTests()
SetupTests(config.ServiceRootpath.GetString())
SetupTests()
os.Exit(m.Run())
}