113 lines
2.0 KiB
Go
113 lines
2.0 KiB
Go
package simpleorm_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
_ "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
|
|
}
|
|
|
|
func (t *Test) SetPk(pks ...any) {
|
|
t.ID = pks[0].(int)
|
|
}
|
|
|
|
type TestWithFk struct {
|
|
ID int `sql:"pk"`
|
|
TestID int `sql:"nn;fk=Test.ID"`
|
|
}
|
|
|
|
func (t *TestWithFk) IsInsertable() bool {
|
|
return t.ID == 0
|
|
}
|
|
|
|
func (t *TestWithFk) SetPk(pks ...any) {
|
|
t.ID = pks[0].(int)
|
|
}
|
|
|
|
type TestWithFkAndFkId struct {
|
|
ID int `sql:"pk"`
|
|
TestID int `sql:"nn;fk=Test.ID;fk_id=custom_fk"`
|
|
}
|
|
|
|
func (t *TestWithFkAndFkId) IsInsertable() bool {
|
|
return t.ID == 0
|
|
}
|
|
|
|
func (t *TestWithFkAndFkId) SetPk(pks ...any) {
|
|
t.ID = pks[0].(int)
|
|
}
|
|
|
|
type TestWithCompositePk struct {
|
|
ID int `sql:"nn;pk;"`
|
|
Name string `sql:"nn;pk"`
|
|
}
|
|
|
|
func (t *TestWithCompositePk) IsInsertable() bool {
|
|
return t.ID == 0 && t.Name == ""
|
|
}
|
|
|
|
func (t *TestWithCompositePk) SetPk(pks ...any) {
|
|
t.ID = pks[0].(int)
|
|
t.Name = pks[1].(string)
|
|
}
|
|
|
|
type TestWithCompositeFk struct {
|
|
ID int `sql:"nn;pk"`
|
|
CompositeFkId int `sql:"nn;fk=TestWithCompositePk.ID;"`
|
|
CompositeFkName string `sql:"nn;fk=TestWithCompositePk.Name;"`
|
|
}
|
|
|
|
func (t *TestWithCompositeFk) IsInsertable() bool {
|
|
return t.ID == 0
|
|
}
|
|
|
|
func (t *TestWithCompositeFk) SetPk(pks ...any) {
|
|
t.ID = pks[0].(int)
|
|
}
|
|
|
|
type TestWithBool struct {
|
|
ID int `sql:"pk"`
|
|
BoolField bool `sql:"nn"`
|
|
}
|
|
|
|
func (t *TestWithBool) IsInsertable() bool {
|
|
return t.ID == 0
|
|
}
|
|
|
|
func (t *TestWithBool) SetPk(pks ...any) {
|
|
t.ID = pks[0].(int)
|
|
}
|
|
|
|
type TestWithTime struct {
|
|
ID int `sql:"pk"`
|
|
TimeField int64 `sql:"nn"`
|
|
}
|
|
|
|
func (t *TestWithTime) IsInsertable() bool {
|
|
return t.ID == 0
|
|
}
|
|
|
|
func (t *TestWithTime) SetPk(pks ...any) {
|
|
t.ID = pks[0].(int)
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
log.SetupLogs("Info")
|
|
|
|
code := m.Run()
|
|
|
|
os.Exit(code)
|
|
}
|