1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-21 21:40:55 +00:00

add UT for specifying indexes

This commit is contained in:
Anker Jam 2020-08-10 23:27:03 +08:00
parent 5a1fa4e1ec
commit 882f1273c8
2 changed files with 37 additions and 0 deletions

View File

@ -382,6 +382,15 @@ type InLine struct {
Email string Email string
} }
type Index struct {
// Common Fields
Id int `orm:"column(id)"`
// Other Fields
F1 int `orm:"column(f1);unique"`
F2 int `orm:"column(f2);unique"`
}
func NewInLine() *InLine { func NewInLine() *InLine {
return new(InLine) return new(InLine)
} }

View File

@ -201,6 +201,7 @@ func TestSyncDb(t *testing.T) {
RegisterModel(new(IntegerPk)) RegisterModel(new(IntegerPk))
RegisterModel(new(UintPk)) RegisterModel(new(UintPk))
RegisterModel(new(PtrPk)) RegisterModel(new(PtrPk))
RegisterModel(new(Index))
err := RunSyncdb("default", true, Debug) err := RunSyncdb("default", true, Debug)
throwFail(t, err) throwFail(t, err)
@ -225,6 +226,7 @@ func TestRegisterModels(t *testing.T) {
RegisterModel(new(IntegerPk)) RegisterModel(new(IntegerPk))
RegisterModel(new(UintPk)) RegisterModel(new(UintPk))
RegisterModel(new(PtrPk)) RegisterModel(new(PtrPk))
RegisterModel(new(Index))
BootStrap() BootStrap()
@ -794,6 +796,32 @@ func TestExpr(t *testing.T) {
// throwFail(t, AssertIs(num, 3)) // throwFail(t, AssertIs(num, 3))
} }
func TestSpecifyIndex(t *testing.T) {
var index *Index
index = &Index{
F1: 1,
F2: 2,
}
_, _ = dORM.Insert(index)
throwFailNow(t, AssertIs(index.Id, 1))
index = &Index{
F1: 3,
F2: 4,
}
_, _ = dORM.Insert(index)
throwFailNow(t, AssertIs(index.Id, 2))
_ = dORM.QueryTable(&Index{}).Filter(`f1`, `1`).ForceIndex(`f1`).One(index)
throwFailNow(t, AssertIs(index.F2, 2))
_ = dORM.QueryTable(&Index{}).Filter(`f1`, `3`).UseIndex(`f1`, `f2`).One(index)
throwFailNow(t, AssertIs(index.F2, 4))
_ = dORM.QueryTable(&Index{}).Filter(`f1`, `1`).IgnoreIndex(`f1`, `f2`).One(index)
throwFailNow(t, AssertIs(index.F2, 2))
}
func TestOperators(t *testing.T) { func TestOperators(t *testing.T) {
qs := dORM.QueryTable("user") qs := dORM.QueryTable("user")
num, err := qs.Filter("user_name", "slene").Count() num, err := qs.Filter("user_name", "slene").Count()