2018-06-10 11:11:41 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2018-10-31 13:42:38 +01:00
|
|
|
"code.vikunja.io/api/pkg/mail"
|
2018-06-10 11:11:41 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/go-xorm/core"
|
|
|
|
"github.com/go-xorm/xorm"
|
|
|
|
"gopkg.in/testfixtures.v2"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-10-28 17:11:13 +01:00
|
|
|
// IsTesting is set to true when we're running tests.
|
|
|
|
// We don't have a good solution to test email sending yet, so we disable email sending when testing
|
|
|
|
var IsTesting bool
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
// MainTest creates the test engine
|
|
|
|
func MainTest(m *testing.M, pathToRoot string) {
|
|
|
|
var err error
|
|
|
|
fixturesDir := filepath.Join(pathToRoot, "models", "fixtures")
|
|
|
|
if err = createTestEngine(fixturesDir); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error creating test engine: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-10-28 17:11:13 +01:00
|
|
|
IsTesting = true
|
|
|
|
|
|
|
|
// Start the pseudo mail queue
|
|
|
|
mail.StartMailDaemon()
|
|
|
|
|
2018-09-08 13:29:35 +02:00
|
|
|
// Create test database
|
|
|
|
PrepareTestDatabase()
|
|
|
|
|
2018-06-10 11:11:41 +02:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestEngine(fixturesDir string) error {
|
|
|
|
var err error
|
|
|
|
x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
|
|
|
|
//x, err = xorm.NewEngine("sqlite3", "db.db")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
x.SetMapper(core.GonicMapper{})
|
|
|
|
|
|
|
|
// Sync dat shit
|
2018-06-10 14:24:33 +02:00
|
|
|
if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
|
|
|
|
return fmt.Errorf("sync database struct error: %v", err)
|
|
|
|
}
|
2018-06-10 11:11:41 +02:00
|
|
|
|
2018-09-23 21:15:14 +02:00
|
|
|
// Show SQL-Queries if necessary
|
2018-06-10 11:11:41 +02:00
|
|
|
if os.Getenv("UNIT_TESTS_VERBOSE") == "1" {
|
|
|
|
x.ShowSQL(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
return InitFixtures(&testfixtures.SQLite{}, fixturesDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareTestDatabase load test fixtures into test database
|
|
|
|
func PrepareTestDatabase() error {
|
|
|
|
return LoadFixtures()
|
|
|
|
}
|