Refactored test handling

This commit is contained in:
kolaente 2019-10-13 14:32:40 +02:00
parent 8619502d39
commit 1d3defe630
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
11 changed files with 119 additions and 66 deletions

View File

@ -124,10 +124,9 @@ func (k Key) setDefault(i interface{}) {
viper.SetDefault(string(k), i)
}
// InitConfig initializes the config, sets defaults etc.
func InitConfig() {
// Set defaults
// InitDefaultConfig sets default config values
// This is an extra function so we can call it when initializing tests without initializing the full config
func InitDefaultConfig() {
// Service config
random, err := random(32)
if err != nil {
@ -195,6 +194,13 @@ func InitConfig() {
RateLimitLimit.setDefault(100)
RateLimitPeriod.setDefault(60)
RateLimitStore.setDefault("memory")
}
// InitConfig initializes the config, sets defaults etc.
func InitConfig() {
// Set defaults
InitDefaultConfig()
// Init checking for environment variables
viper.SetEnvPrefix("vikunja")
@ -207,7 +213,7 @@ func InitConfig() {
viper.AddConfigPath("~/.config/vikunja")
viper.AddConfigPath(".")
viper.SetConfigName("config")
err = viper.ReadInConfig()
err := viper.ReadInConfig()
if err != nil {
log.Println(err.Error())
log.Println("Using defaults.")

36
pkg/db/test_fixtures.go Normal file
View File

@ -0,0 +1,36 @@
// Copyright 2019 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 (
"gopkg.in/testfixtures.v2"
)
var fixtures *testfixtures.Context
// InitFixtures initialize test fixtures for a test database
func InitFixtures(helper testfixtures.Helper, dir string) (err error) {
testfixtures.SkipDatabaseNameCheck(true)
fixtures, err = testfixtures.NewFolder(x.DB().DB, helper, dir)
return err
}
// LoadFixtures load fixtures for a test database
func LoadFixtures() error {
return fixtures.Load()
}

View File

@ -21,8 +21,9 @@ import (
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"github.com/spf13/afero"
"gopkg.in/testfixtures.v2"
"os"
"testing"
"path/filepath"
)
// This file handles storing and retrieving a file for different backends
@ -59,13 +60,27 @@ func InitTests() {
if err != nil {
log.Fatal(err)
}
InitTestFileHandler()
}
// TestMain is the main test function used to bootstrap the test env
func TestMain(m *testing.M) {
InitTests()
os.Exit(m.Run())
config.InitDefaultConfig()
// 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)
if err != nil {
log.Fatal(err)
}
// Load the fixtures
err = db.LoadFixtures()
if err != nil {
log.Fatalf("Error preparing test database: %v", err.Error())
}
InitTestFileHandler()
}
// FileStat stats a file. This is an exported function to be able to test this from outide of the package

29
pkg/files/main_test.go Normal file
View File

@ -0,0 +1,29 @@
// Copyright 2019 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 files
import (
"os"
"testing"
)
// TestMain is the main test function used to bootstrap the test env
func TestMain(m *testing.M) {
InitTests()
os.Exit(m.Run())
}

View File

@ -18,6 +18,7 @@ package integrations
import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/routes"
@ -74,12 +75,12 @@ var (
)
func setupTestEnv() (e *echo.Echo, err error) {
config.InitConfig()
config.InitDefaultConfig()
// Some tests use the file engine, so we'll need to initialize that
files.InitTests()
models.SetupTests(config.ServiceRootpath.GetString())
err = models.LoadFixtures()
err = db.LoadFixtures()
if err != nil {
return
}

View File

@ -18,10 +18,20 @@ package models
import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/files"
"os"
"testing"
)
func TestMain(m *testing.M) {
config.InitConfig()
MainTest(m, config.ServiceRootpath.GetString())
// Set default config
config.InitDefaultConfig()
// Some tests use the file engine, so we'll need to initialize that
files.InitTests()
SetupTests(config.ServiceRootpath.GetString())
os.Exit(m.Run())
}

View File

@ -7,6 +7,7 @@
package models
import (
"code.vikunja.io/api/pkg/db"
"github.com/stretchr/testify/assert"
"gopkg.in/d4l3k/messagediff.v1"
"sort"
@ -434,7 +435,7 @@ func sortTasksForTesting(by SortBy) (tasks []*Task) {
}
func TestTask_ReadAll(t *testing.T) {
assert.NoError(t, LoadFixtures())
assert.NoError(t, db.LoadFixtures())
// Dummy users
user1 := &User{

View File

@ -1,35 +0,0 @@
// Vikunja is a todo-list application to facilitate your life.
// Copyright 2018 Vikunja and contributors. All rights reserved.
//
// This program 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.
//
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"gopkg.in/testfixtures.v2"
)
var fixtures *testfixtures.Context
// InitFixtures initialize test fixtures for a test database
func InitFixtures(helper testfixtures.Helper, dir string) (err error) {
testfixtures.SkipDatabaseNameCheck(true)
fixtures, err = testfixtures.NewFolder(x.DB().DB, helper, dir)
return err
}
// LoadFixtures load fixtures for a test database
func LoadFixtures() error {
return fixtures.Load()
}

View File

@ -20,7 +20,6 @@ import (
"code.vikunja.io/api/pkg/config"
_ "code.vikunja.io/api/pkg/config" // To trigger its init() which initializes the config
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/mail"
"fmt"
@ -28,18 +27,8 @@ import (
"gopkg.in/testfixtures.v2"
"os"
"path/filepath"
"testing"
)
// MainTest creates the test engine
func MainTest(m *testing.M, pathToRoot string) {
// Some tests use the file engine, so we'll need to initialize that
files.InitTests()
SetupTests(pathToRoot)
os.Exit(m.Run())
}
// SetupTests takes care of seting up the db, fixtures etc.
// This is an extra function to be able to call the fixtures setup from the integration tests.
func SetupTests(pathToRoot string) {
@ -53,7 +42,7 @@ func SetupTests(pathToRoot string) {
mail.StartMailDaemon()
// Create test database
if err = LoadFixtures(); err != nil {
if err = db.LoadFixtures(); err != nil {
log.Fatalf("Error preparing test database: %v", err.Error())
}
}
@ -89,7 +78,7 @@ func createTestEngine(fixturesDir string) error {
}
}
return InitFixtures(fixturesHelper, fixturesDir)
return db.InitFixtures(fixturesHelper, fixturesDir)
}
func initSchema(tx *xorm.Engine) error {

View File

@ -1,6 +1,7 @@
package models
import (
"code.vikunja.io/api/pkg/db"
"github.com/stretchr/testify/assert"
"gopkg.in/d4l3k/messagediff.v1"
"testing"
@ -8,7 +9,7 @@ import (
func TestListUsersFromList(t *testing.T) {
err := LoadFixtures()
err := db.LoadFixtures()
assert.NoError(t, err)
testuser1 := &User{