2020-12-29 02:04:20 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2019-10-16 22:52:29 +02:00
|
|
|
//
|
2020-12-29 02:04:20 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-10-16 22:52:29 +02:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2020-12-29 02:04:20 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
2019-10-16 22:52:29 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2019-10-16 22:52:29 +02:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-12-29 02:04:20 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-10-16 22:52:29 +02:00
|
|
|
|
|
|
|
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...)
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 22:56:02 +02:00
|
|
|
return nil
|
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)
|
|
|
|
}
|