81 lines
1.6 KiB
Go
81 lines
1.6 KiB
Go
package simpleorm_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
simpleorm "git.gdulai.com/gdulai/simpleorm"
|
|
"git.gdulai.com/gdulai/simpleorm/exec"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
log "gitlab.com/gdulai/simpleloglvl"
|
|
)
|
|
|
|
type Test struct {
|
|
ID int `sql:"pk"`
|
|
Int64Field int64 `sql:"nn"`
|
|
IntField int `sql:"nn"`
|
|
StringField string `sql:"nn"`
|
|
}
|
|
|
|
func (t Test) IsInsertable() bool {
|
|
return t.ID == 0
|
|
}
|
|
|
|
type TestWithFk struct {
|
|
ID int `sql:"pk"`
|
|
TestID int `sql:"nn;fk=Test.ID"`
|
|
}
|
|
|
|
type TestWithFkAndFkId struct {
|
|
ID int `sql:"pk"`
|
|
TestID int `sql:"nn;fk=Test.ID;fk_id=custom_fk"`
|
|
}
|
|
|
|
type TestWithCompositePk struct {
|
|
ID int `sql:"nn;pk;"`
|
|
Name string `sql:"nn;pk"`
|
|
}
|
|
|
|
func (t TestWithCompositePk) IsInsertable() bool {
|
|
return t.ID == 0 && t.Name == ""
|
|
}
|
|
|
|
type TestWithCompositeFk struct {
|
|
ID int `sql:"nn;pk"`
|
|
CompositeFkId int `sql:"nn;fk=TestWithCompositePk.ID;"`
|
|
CompositeFkName string `sql:"nn;fk=TestWithCompositePk.Name;"`
|
|
}
|
|
|
|
type TestWithBool struct {
|
|
ID int `sql:"pk"`
|
|
BoolField bool `sql:"nn"`
|
|
}
|
|
|
|
type TestWithTime struct {
|
|
ID int `sql:"pk"`
|
|
TimeField int64 `sql:"nn"`
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
log.SetupLogs("Info")
|
|
|
|
code := m.Run()
|
|
|
|
os.Exit(code)
|
|
}
|
|
|
|
func testSetup() (*simpleorm.ORM, *simpleorm.DBConnection) {
|
|
conn := simpleorm.OpenConnection("sqlite3", "file:test.db")
|
|
|
|
orm := simpleorm.NewORM(Test{}, TestWithFk{}, TestWithFkAndFkId{},
|
|
TestWithCompositePk{}, TestWithCompositeFk{}, TestWithBool{}, TestWithTime{})
|
|
exec.ExecuteDDL(conn, orm)
|
|
|
|
return orm, conn
|
|
}
|
|
|
|
func cleanUp(dbFile string, conn *simpleorm.DBConnection) {
|
|
conn.Close()
|
|
os.Remove(dbFile)
|
|
}
|