Library/models/unit_tests.go

58 lines
1.2 KiB
Go
Raw Permalink Normal View History

2018-01-16 10:41:11 +00:00
package models
import (
2018-01-16 10:55:12 +00:00
"fmt"
2018-01-16 10:41:11 +00:00
"github.com/go-xorm/core"
2018-01-16 10:55:12 +00:00
"github.com/go-xorm/xorm"
2018-01-16 10:41:11 +00:00
"gopkg.in/testfixtures.v2"
2018-01-16 10:55:12 +00:00
"os"
2018-01-16 10:41:11 +00:00
"path/filepath"
2018-01-16 10:55:12 +00:00
"testing"
2018-01-16 10:41:11 +00:00
)
2018-01-16 10:55:12 +00:00
// MainTest creates the test engine
2018-01-16 10:41:11 +00:00
func MainTest(m *testing.M, pathToRoot string) {
var err error
2018-04-13 13:03:22 +00:00
fixturesDir := filepath.Join(pathToRoot, "models", "fixtures")
2018-01-16 10:41:11 +00:00
if err = createTestEngine(fixturesDir); err != nil {
fmt.Fprintf(os.Stderr, "Error creating test engine: %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
func createTestEngine(fixturesDir string) error {
var err error
x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
2018-01-26 15:44:58 +00:00
//x, err = xorm.NewEngine("sqlite3", "db.db")
2018-01-16 10:41:11 +00:00
if err != nil {
return err
}
x.SetMapper(core.GonicMapper{})
// Sync dat shit
x.Sync(&Book{})
x.Sync(&User{})
x.Sync(&Publisher{})
x.Sync(&Author{})
x.Sync(&AuthorBook{})
x.Sync(&Status{})
x.Sync(&Quantity{})
x.Sync(&quantityRelation{})
x.Sync(&Item{})
x.Sync(&UserLog{})
// Show SQL-Queries if nessecary
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()
2018-01-16 10:55:12 +00:00
}