2021-10-19 09:44:49 +00:00
|
|
|
// Copyright 2021 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-11-13 19:18:06 +00:00
|
|
|
"testing"
|
2022-01-31 13:39:53 +00:00
|
|
|
"time"
|
2021-11-13 19:18:06 +00:00
|
|
|
|
2024-01-14 17:22:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-11-13 19:18:06 +00:00
|
|
|
"xorm.io/xorm"
|
2022-01-31 13:39:53 +00:00
|
|
|
"xorm.io/xorm/schemas"
|
2021-10-19 09:44:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func testDriverConfig() (driver, config string) {
|
|
|
|
driver = "sqlite3"
|
|
|
|
config = ":memory:"
|
|
|
|
|
|
|
|
if os.Getenv("WOODPECKER_DATABASE_DRIVER") != "" {
|
|
|
|
driver = os.Getenv("WOODPECKER_DATABASE_DRIVER")
|
2021-12-20 15:15:21 +00:00
|
|
|
config = os.Getenv("WOODPECKER_DATABASE_DATASOURCE")
|
2021-10-19 09:44:49 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
// newTestStore creates a new database connection for testing purposes.
|
2021-10-19 09:44:49 +00:00
|
|
|
// The database driver and connection string are provided by
|
|
|
|
// environment variables, with fallback to in-memory sqlite.
|
2023-11-12 17:23:48 +00:00
|
|
|
func newTestStore(t *testing.T, tables ...any) (*storage, func()) {
|
2021-11-13 19:18:06 +00:00
|
|
|
engine, err := xorm.NewEngine(testDriverConfig())
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2021-10-19 09:44:49 +00:00
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
for _, table := range tables {
|
2023-05-31 19:27:57 +00:00
|
|
|
if err := engine.Sync(table); err != nil {
|
2021-11-13 19:18:06 +00:00
|
|
|
t.Error(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2021-10-19 09:44:49 +00:00
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
|
|
|
|
return &storage{
|
|
|
|
engine: engine,
|
|
|
|
}, func() {
|
2022-01-08 19:21:22 +00:00
|
|
|
for _, bean := range tables {
|
|
|
|
if err := engine.DropIndexes(bean); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := engine.DropTables(tables...); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if err := engine.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2022-01-31 13:39:53 +00:00
|
|
|
|
|
|
|
dbType := engine.Dialect().URI().DBType
|
|
|
|
if dbType == schemas.MYSQL || dbType == schemas.POSTGRES {
|
|
|
|
// wait for mysql/postgres to sync ...
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
}
|
2021-10-19 09:44:49 +00:00
|
|
|
}
|