2020-01-09 18:33:22 +01:00
|
|
|
// Copyright 2018-2020 Vikunja and contriubtors. All rights reserved.
|
2019-10-16 22:52:29 +02:00
|
|
|
//
|
|
|
|
// 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 (
|
2020-02-16 22:42:04 +01:00
|
|
|
"fmt"
|
2020-01-26 18:08:06 +01:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2020-10-11 22:10:03 +02:00
|
|
|
|
|
|
|
"code.vikunja.io/api/pkg/config"
|
|
|
|
"github.com/go-testfixtures/testfixtures/v3"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-04-12 19:29:24 +02:00
|
|
|
"xorm.io/xorm/schemas"
|
2019-10-16 22:52:29 +02:00
|
|
|
)
|
|
|
|
|
2020-02-16 22:42:04 +01:00
|
|
|
var fixtures *testfixtures.Loader
|
2019-10-16 22:52:29 +02:00
|
|
|
|
|
|
|
// InitFixtures initialize test fixtures for a test database
|
2020-01-26 18:08:06 +01:00
|
|
|
func InitFixtures(tablenames ...string) (err error) {
|
|
|
|
|
2020-02-16 22:42:04 +01:00
|
|
|
var testfiles func(loader *testfixtures.Loader) error
|
2020-01-26 18:08:06 +01:00
|
|
|
dir := filepath.Join(config.ServiceRootpath.GetString(), "pkg", "db", "fixtures")
|
|
|
|
|
|
|
|
// If fixture table names are specified, load them
|
|
|
|
// Otherwise, load all fixtures
|
|
|
|
if len(tablenames) > 0 {
|
|
|
|
for i, name := range tablenames {
|
|
|
|
tablenames[i] = filepath.Join(dir, name+".yml")
|
|
|
|
}
|
2020-02-16 22:42:04 +01:00
|
|
|
testfiles = testfixtures.Files(tablenames...)
|
2020-01-26 18:08:06 +01:00
|
|
|
} else {
|
2020-02-16 22:42:04 +01:00
|
|
|
testfiles = testfixtures.Directory(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
loaderOptions := []func(loader *testfixtures.Loader) error{
|
|
|
|
testfixtures.Database(x.DB().DB),
|
|
|
|
testfixtures.Dialect(config.DatabaseType.GetString()),
|
|
|
|
testfixtures.DangerousSkipTestDatabaseCheck(),
|
2020-06-27 19:04:01 +02:00
|
|
|
testfixtures.Location(config.GetTimeZone()),
|
2020-02-16 22:42:04 +01:00
|
|
|
testfiles,
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.DatabaseType.GetString() == "postgres" {
|
|
|
|
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())
|
|
|
|
}
|
|
|
|
|
|
|
|
fixtures, err = testfixtures.New(loaderOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-01-26 18:08:06 +01:00
|
|
|
}
|
2020-02-16 22:42:04 +01:00
|
|
|
|
2019-10-16 22:52:29 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFixtures load fixtures for a test database
|
|
|
|
func LoadFixtures() error {
|
2020-02-16 22:42:04 +01:00
|
|
|
err := fixtures.Load()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copied from https://github.com/go-gitea/gitea/blob/master/models/test_fixtures.go#L39
|
|
|
|
// Now if we're running postgres we need to tell it to update the sequences
|
2020-04-12 19:29:24 +02:00
|
|
|
if x.Dialect().URI().DBType == schemas.POSTGRES {
|
2020-02-16 22:42:04 +01:00
|
|
|
results, err := x.QueryString(`SELECT 'SELECT SETVAL(' ||
|
|
|
|
quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
|
|
|
|
', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM ' ||
|
|
|
|
quote_ident(PGT.schemaname)|| '.'||quote_ident(T.relname)|| ';'
|
|
|
|
FROM pg_class AS S,
|
|
|
|
pg_depend AS D,
|
|
|
|
pg_class AS T,
|
|
|
|
pg_attribute AS C,
|
|
|
|
pg_tables AS PGT
|
|
|
|
WHERE S.relkind = 'S'
|
|
|
|
AND S.oid = D.objid
|
|
|
|
AND D.refobjid = T.oid
|
|
|
|
AND D.refobjid = C.attrelid
|
|
|
|
AND D.refobjsubid = C.attnum
|
|
|
|
AND T.relname = PGT.tablename
|
|
|
|
ORDER BY S.relname;`)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to generate sequence update: %v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, r := range results {
|
|
|
|
for _, value := range r {
|
|
|
|
_, err = x.Exec(value)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to update sequence: %s Error: %v\n", value, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2019-10-16 22:52:29 +02:00
|
|
|
}
|
2020-01-26 18:08:06 +01:00
|
|
|
|
|
|
|
// LoadAndAssertFixtures loads all fixtures defined before and asserts they are correctly loaded
|
|
|
|
func LoadAndAssertFixtures(t *testing.T) {
|
|
|
|
err := LoadFixtures()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|