Integration test framework (#1290)

* Integration test framework

* udpate drone sign

* Formatting fixes and move router.go to routers/

* update sign for drone
This commit is contained in:
Ethan Koenig
2017-04-25 03:24:51 -04:00
committed by Lunny Xiao
parent 3012971e92
commit c58708d3ee
17 changed files with 1044 additions and 975 deletions

View File

@ -25,6 +25,17 @@ pipeline:
when:
event: [ push, tag, pull_request ]
test-sqlite:
image: webhippie/golang:edge
pull: true
environment:
TAGS: bindata
GOPATH: /srv/app
commands:
- make test-sqlite
when:
event: [ push, tag, pull_request ]
test-mysql:
image: webhippie/golang:edge
pull: true
@ -32,7 +43,7 @@ pipeline:
TAGS: bindata
GOPATH: /srv/app
commands:
- make test-mysql
- echo make test-mysql # Not ready yet
when:
event: [ push, tag, pull_request ]
@ -43,7 +54,7 @@ pipeline:
TAGS: bindata
GOPATH: /srv/app
commands:
- make test-pgsql
- echo make test-pqsql # Not ready yet
when:
event: [ push, tag, pull_request ]

View File

@ -1 +1 @@
eyJhbGciOiJIUzI1NiJ9.d29ya3NwYWNlOgogIGJhc2U6IC9zcnYvYXBwCiAgcGF0aDogc3JjL2NvZGUuZ2l0ZWEuaW8vZ2l0ZWEKCnBpcGVsaW5lOgogIGNsb25lOgogICAgaW1hZ2U6IHBsdWdpbnMvZ2l0CiAgICB0YWdzOiB0cnVlCgogIHRlc3Q6CiAgICBpbWFnZTogd2ViaGlwcGllL2dvbGFuZzplZGdlCiAgICBwdWxsOiB0cnVlCiAgICBlbnZpcm9ubWVudDoKICAgICAgVEFHUzogYmluZGF0YSBzcWxpdGUKICAgICAgR09QQVRIOiAvc3J2L2FwcAogICAgY29tbWFuZHM6CiAgICAgIC0gYXBrIC1VIGFkZCBvcGVuc3NoLWNsaWVudAogICAgICAtIG1ha2UgY2xlYW4KICAgICAgLSBtYWtlIGdlbmVyYXRlCiAgICAgIC0gbWFrZSB2ZXQKICAgICAgLSBtYWtlIGxpbnQKICAgICAgLSBtYWtlIHRlc3QtdmVuZG9yCiAgICAgIC0gbWFrZSB0ZXN0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lbGVhc2UvCiAgICBzb3VyY2U6IGRpc3QvcmVsZWFzZS8qCiAgICB0YXJnZXQ6IC9naXRlYS9tYXN0ZXIKICAgIHdoZW46CiAgICAgIGV2ZW50OiBbIHB1c2ggXQogICAgICBicmFuY2g6IFsgbWFzdGVyIF0KCiAgZ2l0aHViOgogICAgaW1hZ2U6IHBsdWdpbnMvZ2l0aHViLXJlbGVhc2UKICAgIGZpbGVzOgogICAgICAtIGRpc3QvcmVsZWFzZS8qCiAgICB3aGVuOgogICAgICBldmVudDogWyB0YWcgXQogICAgICBicmFuY2g6IFsgcmVmcy90YWdzLyogXQoKICBnaXR0ZXI6CiAgICBpbWFnZTogcGx1Z2lucy9naXR0ZXIKCnNlcnZpY2VzOgogIG15c3FsOgogICAgaW1hZ2U6IG15c3FsOjUuNwogICAgZW52aXJvbm1lbnQ6CiAgICAgIC0gTVlTUUxfREFUQUJBU0U9dGVzdAogICAgICAtIE1ZU1FMX0FMTE9XX0VNUFRZX1BBU1NXT1JEPXllcwogICAgd2hlbjoKICAgICAgZXZlbnQ6IFsgcHVzaCwgdGFnLCBwdWxsX3JlcXVlc3QgXQoKICBwZ3NxbDoKICAgIGltYWdlOiBwb3N0Z3Jlczo5LjUKICAgIGVudmlyb25tZW50OgogICAgICAtIFBPU1RHUkVTX0RCPXRlc3QKICAgIHdoZW46CiAgICAgIGV2ZW50OiBbIHB1c2gsIHRhZywgcHVsbF9yZXF1ZXN0IF0K.dA2VK6LdoPXvBTYAUywWervhOZmgOjU32uiiPrBbVdQ

1
.gitignore vendored
View File

@ -44,3 +44,4 @@ coverage.out
/indexers
/log
/public/img/avatar
/integrations/gitea-integration*

View File

@ -86,13 +86,27 @@ test-vendor:
fi
govendor status +outside +unused || exit 1
.PHONY: test-sqlite
test-sqlite: integrations.test integrations/gitea-integration
GITEA_CONF=integrations/sqlite.ini ./integrations.test
.PHONY: test-mysql
test-mysql:
@echo "Not integrated yet!"
test-mysql: integrations.test integrations/gitea-integration
echo "CREATE DATABASE IF NOT EXISTS testgitea" | mysql -u root
GITEA_CONF=integrations/mysql.ini ./integrations.test
.PHONY: test-pgsql
test-pgsql:
@echo "Not integrated yet!"
test-pgsql: integrations.test integrations/gitea-integration
GITEA_CONF=integrations/pgsql.ini ./integrations.test
integrations.test: $(SOURCES)
go test -c code.gitea.io/gitea/integrations -tags 'sqlite'
integrations/gitea-integration:
curl -L https://github.com/ethantkoenig/gitea-integration/archive/v2.tar.gz > integrations/gitea-integration.tar.gz
mkdir -p integrations/gitea-integration
tar -xf integrations/gitea-integration.tar.gz -C integrations/gitea-integration --strip-components 1
.PHONY: check
check: test

File diff suppressed because it is too large Load Diff

View File

@ -1,91 +0,0 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package integration
import (
"fmt"
"net/http"
"os"
"os/user"
"testing"
"time"
"code.gitea.io/gitea/integrations/internal/utils"
)
// The HTTP port listened by the Gitea server.
const ServerHTTPPort = "3001"
const _RetryLimit = 10
func makeSimpleSettings(user, port string) map[string][]string {
return map[string][]string{
"db_type": {"SQLite3"},
"db_host": {"localhost"},
"db_path": {"data/gitea.db"},
"app_name": {"Gitea: Git with a cup of tea"},
"repo_root_path": {"repositories"},
"run_user": {user},
"domain": {"localhost"},
"ssh_port": {"22"},
"http_port": {port},
"app_url": {"http://localhost:" + port},
"log_root_path": {"log"},
}
}
func install(t *utils.T) error {
var r *http.Response
var err error
for i := 1; i <= _RetryLimit; i++ {
r, err = http.Get("http://:" + ServerHTTPPort + "/")
if err == nil {
fmt.Fprintln(os.Stderr)
break
}
// Give the server some amount of time to warm up.
time.Sleep(100 * time.Millisecond)
fmt.Fprint(os.Stderr, ".")
}
if err != nil {
return err
}
defer r.Body.Close()
_user, err := user.Current()
if err != nil {
return err
}
settings := makeSimpleSettings(_user.Username, ServerHTTPPort)
r, err = http.PostForm("http://:"+ServerHTTPPort+"/install", settings)
if err != nil {
return err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
return fmt.Errorf("'/install': %s", r.Status)
}
return nil
}
func TestInstall(t *testing.T) {
conf := utils.Config{
Program: "../gitea",
WorkDir: "",
Args: []string{"web", "--port", ServerHTTPPort},
LogFile: os.Stderr,
}
if err := utils.New(t, &conf).RunTest(install); err != nil {
t.Fatal(err)
}
}

View File

@ -0,0 +1,92 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package integrations
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"testing"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/routers"
"code.gitea.io/gitea/routers/routes"
"gopkg.in/macaron.v1"
"gopkg.in/testfixtures.v2"
)
var mac *macaron.Macaron
func TestMain(m *testing.M) {
appIniPath := os.Getenv("GITEA_CONF")
if appIniPath == "" {
fmt.Println("Environment variable $GITEA_CONF not set")
os.Exit(1)
}
setting.CustomConf = appIniPath
routers.GlobalInit()
mac = routes.NewMacaron()
routes.RegisterRoutes(mac)
var helper testfixtures.Helper
if setting.UseMySQL {
helper = &testfixtures.MySQL{}
} else if setting.UsePostgreSQL {
helper = &testfixtures.PostgreSQL{}
} else if setting.UseSQLite3 {
helper = &testfixtures.SQLite{}
} else {
fmt.Println("Unsupported RDBMS for integration tests")
os.Exit(1)
}
err := models.InitFixtures(
helper,
"integrations/gitea-integration/fixtures/",
)
if err != nil {
fmt.Printf("Error initializing test database: %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
type TestResponseWriter struct {
HeaderCode int
Writer io.Writer
}
func (w *TestResponseWriter) Header() http.Header {
return make(map[string][]string)
}
func (w *TestResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func (w *TestResponseWriter) WriteHeader(n int) {
w.HeaderCode = n
}
type TestResponse struct {
HeaderCode int
Body []byte
}
func MakeRequest(req *http.Request) *TestResponse {
buffer := bytes.NewBuffer(nil)
respWriter := &TestResponseWriter{
Writer: buffer,
}
mac.ServeHTTP(respWriter, req)
return &TestResponse{
HeaderCode: respWriter.HeaderCode,
Body: buffer.Bytes(),
}
}

View File

@ -1,156 +0,0 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package utils
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"syscall"
"testing"
)
// T wraps testing.T and the configurations of the testing instance.
type T struct {
*testing.T
Config *Config
}
// New create an instance of T
func New(t *testing.T, c *Config) *T {
return &T{T: t, Config: c}
}
// Config Settings of the testing program
type Config struct {
// The executable path of the tested program.
Program string
// Working directory prepared for the tested program.
// If empty, a directory named with random suffixes is picked, and created under the platform-dependent default temporary directory.
// The directory will be removed when the test finishes.
WorkDir string
// Command-line arguments passed to the tested program.
Args []string
// Where to redirect the stdout/stderr to. For debugging purposes.
LogFile *os.File
}
func redirect(cmd *exec.Cmd, f *os.File) error {
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
go io.Copy(f, stdout)
go io.Copy(f, stderr)
return nil
}
// RunTest Helper function for setting up a running Gitea server for functional testing and then gracefully terminating it.
func (t *T) RunTest(tests ...func(*T) error) (err error) {
if t.Config.Program == "" {
return errors.New("Need input file")
}
path, err := filepath.Abs(t.Config.Program)
if err != nil {
return err
}
workdir := t.Config.WorkDir
if workdir == "" {
workdir, err = ioutil.TempDir(os.TempDir(), "gitea_tests-")
if err != nil {
return err
}
defer os.RemoveAll(workdir)
}
newpath := filepath.Join(workdir, filepath.Base(path))
if err := os.Symlink(path, newpath); err != nil {
return err
}
log.Printf("Starting the server: %s args:%s workdir:%s", newpath, t.Config.Args, workdir)
cmd := exec.Command(newpath, t.Config.Args...)
cmd.Dir = workdir
if t.Config.LogFile != nil && testing.Verbose() {
if err := redirect(cmd, t.Config.LogFile); err != nil {
return err
}
}
if err := cmd.Start(); err != nil {
return err
}
log.Println("Server started.")
defer func() {
// Do not early return. We have to call Wait anyway.
_ = cmd.Process.Signal(syscall.SIGTERM)
if _err := cmd.Wait(); _err != nil {
if _err.Error() != "signal: terminated" {
err = _err
return
}
}
log.Println("Server exited")
}()
for _, fn := range tests {
if err := fn(t); err != nil {
return err
}
}
// Note that the return value 'err' may be updated by the 'defer' statement before despite it's returning nil here.
return nil
}
// GetAndPost provides a convenient helper function for testing an HTTP endpoint with GET and POST method.
// The function sends GET first and then POST with the given form.
func GetAndPost(url string, form map[string][]string) error {
var err error
var r *http.Response
r, err = http.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
return fmt.Errorf("GET '%s': %s", url, r.Status)
}
r, err = http.PostForm(url, form)
if err != nil {
return err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
return fmt.Errorf("POST '%s': %s", url, r.Status)
}
return nil
}

56
integrations/mysql.ini Normal file
View File

@ -0,0 +1,56 @@
APP_NAME = Gitea: Git with a cup of tea
RUN_MODE = prod
[database]
DB_TYPE = mysql
HOST = 127.0.0.1:3306
NAME = testgitea
USER = root
PASSWD =
SSL_MODE = disable
PATH = data/gitea.db
[repository]
ROOT = integrations/gitea-integration/gitea-repositories
[server]
SSH_DOMAIN = localhost
HTTP_PORT = 3000
ROOT_URL = http://localhost:3000/
DISABLE_SSH = false
SSH_PORT = 22
LFS_START_SERVER = false
OFFLINE_MODE = false
[mailer]
ENABLED = false
[service]
REGISTER_EMAIL_CONFIRM = false
ENABLE_NOTIFY_MAIL = false
DISABLE_REGISTRATION = false
ENABLE_CAPTCHA = false
REQUIRE_SIGNIN_VIEW = false
DEFAULT_KEEP_EMAIL_PRIVATE = false
NO_REPLY_ADDRESS = noreply.example.org
[picture]
DISABLE_GRAVATAR = false
ENABLE_FEDERATED_AVATAR = false
[session]
PROVIDER = file
[log]
MODE = console,file
[log.console]
LEVEL = Warn
[log.file]
LEVEL = Info
ROOT_PATH = log
[security]
INSTALL_LOCK = true
SECRET_KEY = 9pCviYTWSb

56
integrations/pgsql.ini Normal file
View File

@ -0,0 +1,56 @@
APP_NAME = Gitea: Git with a cup of tea
RUN_MODE = prod
[database]
DB_TYPE = postgres
HOST = 127.0.0.1:5432
NAME = testgitea
USER = postgres
PASSWD = postgres
SSL_MODE = disable
PATH = data/gitea.db
[repository]
ROOT = integrations/gitea-integration/gitea-repositories
[server]
SSH_DOMAIN = localhost
HTTP_PORT = 3000
ROOT_URL = http://localhost:3000/
DISABLE_SSH = false
SSH_PORT = 22
LFS_START_SERVER = false
OFFLINE_MODE = false
[mailer]
ENABLED = false
[service]
REGISTER_EMAIL_CONFIRM = false
ENABLE_NOTIFY_MAIL = false
DISABLE_REGISTRATION = false
ENABLE_CAPTCHA = false
REQUIRE_SIGNIN_VIEW = false
DEFAULT_KEEP_EMAIL_PRIVATE = false
NO_REPLY_ADDRESS = noreply.example.org
[picture]
DISABLE_GRAVATAR = false
ENABLE_FEDERATED_AVATAR = false
[session]
PROVIDER = file
[log]
MODE = console,file
[log.console]
LEVEL = Warn
[log.file]
LEVEL = Info
ROOT_PATH = log
[security]
INSTALL_LOCK = true
SECRET_KEY = 9pCviYTWSb

View File

@ -2,34 +2,40 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package integration
package integrations
import (
"os"
"bytes"
"net/http"
"net/url"
"testing"
"code.gitea.io/gitea/integrations/internal/utils"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting"
"github.com/stretchr/testify/assert"
)
var signupFormSample map[string][]string = map[string][]string{
"Name": {"tester"},
"Email": {"user1@example.com"},
"Passwd": {"12345678"},
}
func signup(t *utils.T) error {
return utils.GetAndPost("http://:"+ServerHTTPPort+"/user/sign_up", signupFormSample)
}
func TestSignup(t *testing.T) {
conf := utils.Config{
Program: "../gitea",
WorkDir: "",
Args: []string{"web", "--port", ServerHTTPPort},
LogFile: os.Stderr,
}
assert.NoError(t, models.LoadFixtures())
setting.Service.EnableCaptcha = false
if err := utils.New(t, &conf).RunTest(install, signup); err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", "/user/sign_up",
bytes.NewBufferString(url.Values{
"user_name": []string{"exampleUser"},
"email": []string{"exampleUser@example.com"},
"password": []string{"examplePassword"},
"retype": []string{"examplePassword"},
}.Encode()),
)
assert.NoError(t, err)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp := MakeRequest(req)
assert.EqualValues(t, http.StatusFound, resp.HeaderCode)
// should be able to view new user's page
req, err = http.NewRequest("GET", "/exampleUser", nil)
assert.NoError(t, err)
resp = MakeRequest(req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
}

58
integrations/sqlite.ini Normal file
View File

@ -0,0 +1,58 @@
APP_NAME = Gitea: Git with a cup of tea
RUN_MODE = prod
[database]
DB_TYPE = sqlite3
HOST = 127.0.0.1:3306
NAME = testgitea
USER = gitea
PASSWD =
SSL_MODE = disable
PATH = :memory:
[repository]
ROOT = integrations/gitea-integration/gitea-repositories
[server]
SSH_DOMAIN = localhost
HTTP_PORT = 3000
ROOT_URL = http://localhost:3000/
DISABLE_SSH = false
SSH_PORT = 22
LFS_START_SERVER = false
OFFLINE_MODE = false
[mailer]
ENABLED = false
[service]
REGISTER_EMAIL_CONFIRM = false
ENABLE_NOTIFY_MAIL = false
DISABLE_REGISTRATION = false
ENABLE_CAPTCHA = false
REQUIRE_SIGNIN_VIEW = false
DEFAULT_KEEP_EMAIL_PRIVATE = false
NO_REPLY_ADDRESS = noreply.example.org
[picture]
DISABLE_GRAVATAR = false
ENABLE_FEDERATED_AVATAR = false
[session]
PROVIDER = file
[log]
MODE = console,file
[log.console]
LEVEL = Warn
[log.file]
LEVEL = Info
ROOT_PATH = log
[security]
INSTALL_LOCK = true
SECRET_KEY = 9pCviYTWSb
INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0OTI3OTU5ODN9.OQkH5UmzID2XBdwQ9TAI6Jj2t1X-wElVTjbE7aoN4I8

View File

@ -2,81 +2,33 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package integration
package integrations
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"code.gitea.io/gitea/integrations/internal/utils"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/sdk/gitea"
"github.com/stretchr/testify/assert"
)
func version(t *utils.T) error {
var err error
path, err := filepath.Abs(t.Config.Program)
if err != nil {
return err
}
cmd := exec.Command(path, "--version")
out, err := cmd.Output()
if err != nil {
return err
}
fields := strings.Fields(string(out))
if !strings.HasPrefix(string(out), "Gitea version") {
return fmt.Errorf("unexpected version string '%s' of the gitea executable", out)
}
expected := fields[2]
var r *http.Response
r, err = http.Get("http://:" + ServerHTTPPort + "/api/v1/version")
if err != nil {
return err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
return fmt.Errorf("'/api/v1/version': %s\n", r.Status)
}
var v gitea.ServerVersion
dec := json.NewDecoder(r.Body)
if err := dec.Decode(&v); err != nil {
return err
}
actual := v.Version
log.Printf("Actual: \"%s\" Expected: \"%s\"\n", actual, expected)
assert.Equal(t, expected, actual)
return nil
}
func TestVersion(t *testing.T) {
conf := utils.Config{
Program: "../gitea",
WorkDir: "",
Args: []string{"web", "--port", ServerHTTPPort},
LogFile: os.Stderr,
}
assert.NoError(t, models.LoadFixtures())
if err := utils.New(t, &conf).RunTest(install, version); err != nil {
t.Fatal(err)
}
setting.AppVer = "1.1.0+dev"
req, err := http.NewRequest("GET", "/api/v1/version", nil)
assert.NoError(t, err)
resp := MakeRequest(req)
var version gitea.ServerVersion
decoder := json.NewDecoder(bytes.NewBuffer(resp.Body))
assert.NoError(t, decoder.Decode(&version))
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
assert.Equal(t, setting.AppVer, string(version.Version))
}

32
integrations/view_test.go Normal file
View File

@ -0,0 +1,32 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package integrations
import (
"net/http"
"testing"
"code.gitea.io/gitea/models"
"github.com/stretchr/testify/assert"
)
func TestViewRepo(t *testing.T) {
assert.NoError(t, models.LoadFixtures())
req, err := http.NewRequest("GET", "/user1/repo1", nil)
assert.NoError(t, err)
resp := MakeRequest(req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
}
func TestViewUser(t *testing.T) {
assert.NoError(t, models.LoadFixtures())
req, err := http.NewRequest("GET", "/user1", nil)
assert.NoError(t, err)
resp := MakeRequest(req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
}

View File

@ -37,11 +37,8 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}
var fixtures *testfixtures.Context
// CreateTestEngine create an xorm engine for testing
func CreateTestEngine() error {
testfixtures.SkipDatabaseNameCheck(true)
var err error
x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
if err != nil {
@ -52,8 +49,7 @@ func CreateTestEngine() error {
return err
}
fixtures, err = testfixtures.NewFolder(x.DB().DB, &testfixtures.SQLite{}, "fixtures/")
return err
return InitFixtures(&testfixtures.SQLite{}, "fixtures/")
}
func TestFixturesAreConsistent(t *testing.T) {
@ -63,7 +59,7 @@ func TestFixturesAreConsistent(t *testing.T) {
// PrepareTestDatabase load test fixtures into test database
func PrepareTestDatabase() error {
return fixtures.Load()
return LoadFixtures()
}
func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {

23
models/test_fixtures.go Normal file
View File

@ -0,0 +1,23 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
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()
}

645
routers/routes/routes.go Normal file

File diff suppressed because it is too large Load Diff