You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

setup_for_test.go 1.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "os"
  8. "testing"
  9. "github.com/go-xorm/core"
  10. "github.com/go-xorm/xorm"
  11. _ "github.com/mattn/go-sqlite3" // for the test engine
  12. "gopkg.in/testfixtures.v2"
  13. )
  14. func TestMain(m *testing.M) {
  15. if err := CreateTestEngine(); err != nil {
  16. fmt.Printf("Error creating test engine: %v\n", err)
  17. os.Exit(1)
  18. }
  19. os.Exit(m.Run())
  20. }
  21. var fixtures *testfixtures.Context
  22. // CreateTestEngine create an xorm engine for testing
  23. func CreateTestEngine() error {
  24. testfixtures.SkipDatabaseNameCheck(true)
  25. var err error
  26. x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
  27. if err != nil {
  28. return err
  29. }
  30. x.SetMapper(core.GonicMapper{})
  31. if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
  32. return err
  33. }
  34. fixtures, err = testfixtures.NewFolder(x.DB().DB, &testfixtures.SQLite{}, "fixtures/")
  35. return err
  36. }
  37. // PrepareTestDatabase load test fixtures into test database
  38. func PrepareTestDatabase() error {
  39. return fixtures.Load()
  40. }