2017-05-03 07:16:45 +02:00
|
|
|
// Package that provides the functionality to open, close and use a database
|
2017-03-30 16:09:44 +02:00
|
|
|
package database
|
2017-03-30 18:44:09 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestModel struct {
|
|
|
|
ID int64
|
|
|
|
Value string `gorm:"type:varchar(255);column:value" json:"value"`
|
|
|
|
}
|
|
|
|
|
2017-04-28 10:02:42 +02:00
|
|
|
// Function to test the error handling for the database opening
|
2017-03-30 18:44:09 +02:00
|
|
|
func TestOpenNoDB(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
c := Config{}
|
|
|
|
|
|
|
|
err := Open(c)
|
|
|
|
assert.Error(err, "error")
|
|
|
|
}
|
2017-04-28 10:02:42 +02:00
|
|
|
|
|
|
|
// Function to test the opening of one database
|
2017-03-30 18:44:09 +02:00
|
|
|
func TestOpenOneDB(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
AddModel(&TestModel{})
|
|
|
|
|
|
|
|
c := Config{
|
|
|
|
Type: "sqlite3",
|
2017-03-31 11:06:40 +02:00
|
|
|
Logging: true,
|
2017-03-30 18:44:09 +02:00
|
|
|
Connection: "file:database?mode=memory",
|
|
|
|
}
|
|
|
|
var count int64
|
|
|
|
|
|
|
|
err := Open(c)
|
|
|
|
assert.NoError(err, "no error")
|
|
|
|
|
|
|
|
Write.Create(&TestModel{Value: "first"})
|
|
|
|
Write.Create(&TestModel{Value: "secound"})
|
|
|
|
|
|
|
|
var list []*TestModel
|
|
|
|
Read.Find(&list).Count(&count)
|
|
|
|
assert.Equal(int64(2), count, "not enought entries")
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2017-04-28 10:02:42 +02:00
|
|
|
// Function to test the opening of a second database
|
2017-03-30 18:44:09 +02:00
|
|
|
func TestOpenTwoDB(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
AddModel(&TestModel{})
|
|
|
|
c := Config{
|
2017-04-04 19:28:46 +02:00
|
|
|
Type: "sqlite3",
|
|
|
|
Logging: true,
|
|
|
|
Connection: "file:database?mode=memory",
|
|
|
|
ReadConnection: "file/",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := Open(c)
|
|
|
|
assert.Error(err, "no error found")
|
|
|
|
|
|
|
|
c = Config{
|
2017-03-30 18:44:09 +02:00
|
|
|
Type: "sqlite3",
|
2017-03-31 11:06:40 +02:00
|
|
|
Logging: true,
|
2017-03-30 18:44:09 +02:00
|
|
|
Connection: "file:database?mode=memory",
|
|
|
|
ReadConnection: "file:database2?mode=memory",
|
|
|
|
}
|
|
|
|
var count int64
|
|
|
|
|
2017-04-04 19:28:46 +02:00
|
|
|
err = Open(c)
|
2017-03-30 18:44:09 +02:00
|
|
|
assert.NoError(err, "no error")
|
|
|
|
|
|
|
|
Write.Create(&TestModel{Value: "first"})
|
|
|
|
Write.Create(&TestModel{Value: "secound"})
|
|
|
|
|
|
|
|
var list []*TestModel
|
|
|
|
Write.Find(&list).Count(&count)
|
|
|
|
assert.Equal(int64(2), count, "not enought entries")
|
|
|
|
|
|
|
|
result := Read.Find(&list)
|
|
|
|
assert.Error(result.Error, "error, because it is the wrong database")
|
|
|
|
Close()
|
|
|
|
}
|