From 647e21b0c401833c3cbf5143e072c07bb195894f Mon Sep 17 00:00:00 2001 From: cruis Date: Fri, 13 Nov 2020 00:13:35 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Condition=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/orm/orm_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/client/orm/orm_test.go b/client/orm/orm_test.go index 565f6c60..8bea63d9 100644 --- a/client/orm/orm_test.go +++ b/client/orm/orm_test.go @@ -2668,3 +2668,45 @@ func TestPSQueryBuilder(t *testing.T) { throwFailNow(t, AssertIs(l[0].UserName, "astaxie")) throwFailNow(t, AssertIs(l[0].Age, 30)) } + +func TestCondition(t *testing.T) { + // test Condition whether to include yourself + cond := NewCondition() + cond = cond.AndCond(cond.Or("id", 1)) + cond = cond.AndCond(cond.Or("id", 2)) + cond = cond.AndCond(cond.Or("id", 3)) + cond = cond.AndCond(cond.Or("id", 4)) + + cycleFlag := false + var hasCycle func(*Condition) + hasCycle = func(c *Condition) { + if nil == c || cycleFlag { + return + } + condPointMap := make(map[string]bool) + condPointMap[fmt.Sprintf("%p", c)] = true + for _, p := range c.params { + if p.isCond { + adr := fmt.Sprintf("%p", p.cond) + if condPointMap[adr] { + // self as sub cond was cycle + cycleFlag = true + return + } else { + condPointMap[adr] = true + } + } + } + for _, p := range c.params { + if p.isCond { + // check next cond + hasCycle(p.cond) + } + } + return + } + hasCycle(cond) + // cycleFlag was true,meaning use self as sub cond + throwFail(t, AssertIs(!cycleFlag, true)) + return +} From 0a852912b4280d58369f5f66967e438c3d55a4dd Mon Sep 17 00:00:00 2001 From: cruis Date: Fri, 13 Nov 2020 07:25:21 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=B0=83=E6=95=B4CI=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/orm/orm_test.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/client/orm/orm_test.go b/client/orm/orm_test.go index 8bea63d9..46863190 100644 --- a/client/orm/orm_test.go +++ b/client/orm/orm_test.go @@ -2672,10 +2672,10 @@ func TestPSQueryBuilder(t *testing.T) { func TestCondition(t *testing.T) { // test Condition whether to include yourself cond := NewCondition() - cond = cond.AndCond(cond.Or("id", 1)) - cond = cond.AndCond(cond.Or("id", 2)) - cond = cond.AndCond(cond.Or("id", 3)) - cond = cond.AndCond(cond.Or("id", 4)) + cond = cond.AndCond(cond.Or("ID", 1)) + cond = cond.AndCond(cond.Or("ID", 2)) + cond = cond.AndCond(cond.Or("ID", 3)) + cond = cond.AndCond(cond.Or("ID", 4)) cycleFlag := false var hasCycle func(*Condition) @@ -2691,12 +2691,15 @@ func TestCondition(t *testing.T) { if condPointMap[adr] { // self as sub cond was cycle cycleFlag = true - return - } else { - condPointMap[adr] = true + break } + condPointMap[adr] = true + } } + if cycleFlag { + return + } for _, p := range c.params { if p.isCond { // check next cond