Task Attachments #104

Merged
konrad merged 63 commits from feature/attachments into master 2019-10-16 20:52:31 +00:00
4 changed files with 72 additions and 21 deletions
Showing only changes of commit 81068a810c - Show all commits

View File

@ -23,6 +23,7 @@ import (
"fmt"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
"os"
"strconv"
"time"
@ -32,8 +33,16 @@ import (
_ "github.com/mattn/go-sqlite3" // Because.
)
// We only want one instance of the engine, so we can reate it once and reuse it
var x *xorm.Engine
// CreateDBEngine initializes a db engine from the config
func CreateDBEngine() (engine *xorm.Engine, err error) {
if x != nil {
return x, nil
}
// If the database type is not set, this likely means we need to initialize the config first
if config.DatabaseType.GetString() == "" {
config.InitConfig()
@ -71,6 +80,27 @@ func CreateDBEngine() (engine *xorm.Engine, err error) {
log.Info("Did not find a valid cache type. Caching disabled. Please refer to the docs for poosible cache types.")
}
}
x = engine
return
}
// 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
}
engine, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
if err != nil {
return nil, err
}
engine.SetMapper(core.GonicMapper{})
engine.ShowSQL(os.Getenv("UNIT_TESTS_VERBOSE") == "1")
engine.SetLogger(xorm.NewSimpleLogger(log.GetLogWriter("database")))
x = engine
return
}

View File

@ -16,7 +16,13 @@
package files
import "github.com/spf13/afero"
import (
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"github.com/spf13/afero"
"os"
"testing"
)
// This file handling storing and retrieving a file for different backends
@ -28,3 +34,30 @@ func InitFileHandler() {
fs = afero.NewOsFs()
afs = &afero.Afero{Fs: fs}
}
// InitTestFileHandler initializes a new memory file system for testing
func InitTestFileHandler() {
fs = afero.NewMemMapFs()
afs = &afero.Afero{Fs: fs}
}
// InitTests handles the actual bootstrapping of the test env
func InitTests() {
var err error
x, err = db.CreateTestEngine()
if err != nil {
log.Fatal(err)
}
err = x.Sync2(GetTables()...)
if err != nil {
log.Fatal(err)
}
InitTestFileHandler()
}
// TestMain is the main test function used to bootstrap the test env
func TestMain(m *testing.M) {
InitTests()
os.Exit(m.Run())
}

View File

@ -30,7 +30,7 @@ type File struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
Name string `xorm:"text not null" json:"name"`
Mime string `xorm:"text null" json:"mime"`
Size int64 `xorm:"int(11) not null default 0" json:"size"`
Size int64 `xorm:"int(11) not null" json:"size"`
Created time.Time `xorm:"datetime" json:"created"`
Updated time.Time `xorm:"datetime" json:"updated"`

View File

@ -19,11 +19,11 @@ package models
import (
"code.vikunja.io/api/pkg/config"
_ "code.vikunja.io/api/pkg/config" // To trigger its init() which initializes the config
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/mail"
"fmt"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
"gopkg.in/testfixtures.v2"
"os"
@ -33,6 +33,9 @@ import (
// MainTest creates the test engine
func MainTest(m *testing.M, pathToRoot string) {
// Some tests use the file engine, so we'll need to initialize that
files.InitTests()
SetupTests(pathToRoot)
os.Exit(m.Run())
}
@ -75,36 +78,21 @@ func createTestEngine(fixturesDir string) error {
fixturesHelper = &testfixtures.MySQL{}
}
} else {
x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
x, err = db.CreateTestEngine()
if err != nil {
return err
return fmt.Errorf("error getting test engine: %v", err)
}
x.SetMapper(core.GonicMapper{})
// Sync dat shit
err = initSchema(x)
if err != nil {
return fmt.Errorf("sync database struct error: %v", err)
}
// Show SQL-Queries if necessary
if os.Getenv("UNIT_TESTS_VERBOSE") == "1" {
x.ShowSQL(true)
}
}
err = files.SetEngine()
if err != nil {
log.Fatal(err.Error())
}
return InitFixtures(fixturesHelper, fixturesDir)
}
func initSchema(tx *xorm.Engine) error {
schemeBeans := []interface{}{}
schemeBeans = append(schemeBeans, GetTables()...)
schemeBeans = append(schemeBeans, files.GetTables()...)
return tx.Sync2(schemeBeans...)
return tx.Sync2(GetTables()...)
}