summaryrefslogtreecommitdiffstats
path: root/models/test_fixtures.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2021-03-24 18:27:22 +0000
committerGitHub <noreply@github.com>2021-03-24 19:27:22 +0100
commit39ef6f83d50e9e641bf36342c532e8a4ad7cf3f7 (patch)
tree999a02645862d0700567a8a817232b5119fe2681 /models/test_fixtures.go
parent750ac52db2a48fb7be4a508feb527a1ffac952c6 (diff)
downloadgitea-39ef6f83d50e9e641bf36342c532e8a4ad7cf3f7.tar.gz
gitea-39ef6f83d50e9e641bf36342c532e8a4ad7cf3f7.zip
Create Proper Migration Tests (#15116)
* Create Proper Migration tests Unfortunately our testing regime has so far meant that migrations do not get proper testing. This PR begins the process of creating migration tests for this. * Add test for v176 * fix mssql drop db Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'models/test_fixtures.go')
-rw-r--r--models/test_fixtures.go26
1 files changed, 18 insertions, 8 deletions
diff --git a/models/test_fixtures.go b/models/test_fixtures.go
index 5cca7c16e8..c1f84c6617 100644
--- a/models/test_fixtures.go
+++ b/models/test_fixtures.go
@@ -10,16 +10,22 @@ import (
"time"
"github.com/go-testfixtures/testfixtures/v3"
+ "xorm.io/xorm"
"xorm.io/xorm/schemas"
)
var fixtures *testfixtures.Loader
// InitFixtures initialize test fixtures for a test database
-func InitFixtures(dir string) (err error) {
+func InitFixtures(dir string, engine ...*xorm.Engine) (err error) {
+ e := x
+ if len(engine) == 1 {
+ e = engine[0]
+ }
+
testfiles := testfixtures.Directory(dir)
dialect := "unknown"
- switch x.Dialect().URI().DBType {
+ switch e.Dialect().URI().DBType {
case schemas.POSTGRES:
dialect = "postgres"
case schemas.MYSQL:
@@ -33,13 +39,13 @@ func InitFixtures(dir string) (err error) {
os.Exit(1)
}
loaderOptions := []func(loader *testfixtures.Loader) error{
- testfixtures.Database(x.DB().DB),
+ testfixtures.Database(e.DB().DB),
testfixtures.Dialect(dialect),
testfixtures.DangerousSkipTestDatabaseCheck(),
testfiles,
}
- if x.Dialect().URI().DBType == schemas.POSTGRES {
+ if e.Dialect().URI().DBType == schemas.POSTGRES {
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())
}
@@ -52,7 +58,11 @@ func InitFixtures(dir string) (err error) {
}
// LoadFixtures load fixtures for a test database
-func LoadFixtures() error {
+func LoadFixtures(engine ...*xorm.Engine) error {
+ e := x
+ if len(engine) == 1 {
+ e = engine[0]
+ }
var err error
// Database transaction conflicts could occur and result in ROLLBACK
// As a simple workaround, we just retry 20 times.
@@ -67,8 +77,8 @@ func LoadFixtures() error {
fmt.Printf("LoadFixtures failed after retries: %v\n", err)
}
// Now if we're running postgres we need to tell it to update the sequences
- if x.Dialect().URI().DBType == schemas.POSTGRES {
- results, err := x.QueryString(`SELECT 'SELECT SETVAL(' ||
+ if e.Dialect().URI().DBType == schemas.POSTGRES {
+ results, err := e.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)|| ';'
@@ -90,7 +100,7 @@ func LoadFixtures() error {
}
for _, r := range results {
for _, value := range r {
- _, err = x.Exec(value)
+ _, err = e.Exec(value)
if err != nil {
fmt.Printf("Failed to update sequence: %s Error: %v\n", value, err)
return err