From 882f1273c8e9afc26984802cc42a027358087a0d Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Mon, 10 Aug 2020 23:27:03 +0800 Subject: [PATCH] add UT for specifying indexes --- pkg/orm/models_test.go | 9 +++++++++ pkg/orm/orm_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/pkg/orm/models_test.go b/pkg/orm/models_test.go index 935c2073..52524501 100644 --- a/pkg/orm/models_test.go +++ b/pkg/orm/models_test.go @@ -382,6 +382,15 @@ type InLine struct { 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 { return new(InLine) } diff --git a/pkg/orm/orm_test.go b/pkg/orm/orm_test.go index 1d173426..58447adb 100644 --- a/pkg/orm/orm_test.go +++ b/pkg/orm/orm_test.go @@ -201,6 +201,7 @@ func TestSyncDb(t *testing.T) { RegisterModel(new(IntegerPk)) RegisterModel(new(UintPk)) RegisterModel(new(PtrPk)) + RegisterModel(new(Index)) err := RunSyncdb("default", true, Debug) throwFail(t, err) @@ -225,6 +226,7 @@ func TestRegisterModels(t *testing.T) { RegisterModel(new(IntegerPk)) RegisterModel(new(UintPk)) RegisterModel(new(PtrPk)) + RegisterModel(new(Index)) BootStrap() @@ -794,6 +796,32 @@ func TestExpr(t *testing.T) { // 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) { qs := dORM.QueryTable("user") num, err := qs.Filter("user_name", "slene").Count()