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.
|
2020-01-26 18:08:06 +01: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
|
2020-01-26 18:08:06 +01: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,
|
2020-01-26 18:08:06 +01: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.
|
2020-01-26 18:08:06 +01: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/>.
|
2020-01-26 18:08:06 +01:00
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2020-09-26 23:02:17 +02:00
|
|
|
"fmt"
|
2020-01-26 18:08:06 +01:00
|
|
|
"os"
|
2020-09-26 23:02:17 +02:00
|
|
|
"testing"
|
2020-10-11 22:10:03 +02:00
|
|
|
|
|
|
|
"code.vikunja.io/api/pkg/config"
|
|
|
|
"code.vikunja.io/api/pkg/log"
|
2022-06-30 16:14:30 +02:00
|
|
|
|
2020-10-11 22:10:03 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-02-14 17:34:25 +01:00
|
|
|
"xorm.io/xorm"
|
2022-06-30 16:14:30 +02:00
|
|
|
"xorm.io/xorm/names"
|
2020-01-26 18:08:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateTestEngine creates an instance of the db engine which lives in memory
|
|
|
|
func CreateTestEngine() (engine *xorm.Engine, err error) {
|
|
|
|
|
|
|
|
if x != nil {
|
|
|
|
return x, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("VIKUNJA_TESTS_USE_CONFIG") == "1" {
|
|
|
|
config.InitConfig()
|
|
|
|
engine, err = CreateDBEngine()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
engine, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 16:14:30 +02:00
|
|
|
engine.SetMapper(names.GonicMapper{})
|
2020-04-19 09:27:28 +02:00
|
|
|
logger := log.NewXormLogger("DEBUG")
|
2020-02-14 17:34:25 +01:00
|
|
|
logger.ShowSQL(os.Getenv("UNIT_TESTS_VERBOSE") == "1")
|
|
|
|
engine.SetLogger(logger)
|
2020-06-27 19:04:01 +02:00
|
|
|
engine.SetTZLocation(config.GetTimeZone())
|
2020-01-26 18:08:06 +01:00
|
|
|
x = engine
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitTestFixtures populates the db with all fixtures from the fixtures folder
|
|
|
|
func InitTestFixtures(tablenames ...string) (err error) {
|
|
|
|
// Create all fixtures
|
|
|
|
config.InitDefaultConfig()
|
|
|
|
// We need to set the root path even if we're not using the config, otherwise fixtures are not loaded correctly
|
|
|
|
config.ServiceRootpath.Set(os.Getenv("VIKUNJA_SERVICE_ROOTPATH"))
|
|
|
|
|
|
|
|
// Sync fixtures
|
|
|
|
err = InitFixtures(tablenames...)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-26 23:02:17 +02:00
|
|
|
|
2020-09-27 17:45:17 +02:00
|
|
|
// AssertExists checks and asserts the existence of certain entries in the db
|
|
|
|
func AssertExists(t *testing.T, table string, values map[string]interface{}, custom bool) {
|
2020-09-26 23:02:17 +02:00
|
|
|
var exists bool
|
|
|
|
var err error
|
|
|
|
v := make(map[string]interface{})
|
|
|
|
// Postgres sometimes needs to build raw sql. Because it won't always need to do this and this isn't fun, it's a flag.
|
|
|
|
if custom {
|
|
|
|
//#nosec
|
|
|
|
sql := "SELECT * FROM " + table + " WHERE "
|
|
|
|
for col, val := range values {
|
|
|
|
sql += col + "=" + fmt.Sprintf("%v", val) + " AND "
|
|
|
|
}
|
|
|
|
sql = sql[:len(sql)-5]
|
|
|
|
exists, err = x.SQL(sql).Get(&v)
|
|
|
|
} else {
|
|
|
|
exists, err = x.Table(table).Where(values).Get(&v)
|
|
|
|
}
|
|
|
|
assert.NoError(t, err, fmt.Sprintf("Failed to assert entries exist in db, error was: %s", err))
|
|
|
|
assert.True(t, exists, fmt.Sprintf("Entries %v do not exist in table %s", values, table))
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:45:17 +02:00
|
|
|
// AssertMissing checks and asserts the nonexiste nce of certain entries in the db
|
|
|
|
func AssertMissing(t *testing.T, table string, values map[string]interface{}) {
|
2020-09-26 23:02:17 +02:00
|
|
|
v := make(map[string]interface{})
|
|
|
|
exists, err := x.Table(table).Where(values).Exist(&v)
|
|
|
|
assert.NoError(t, err, fmt.Sprintf("Failed to assert entries don't exist in db, error was: %s", err))
|
|
|
|
assert.False(t, exists, fmt.Sprintf("Entries %v exist in table %s", values, table))
|
|
|
|
}
|