mirror of
https://github.com/astaxie/beego.git
synced 2024-11-23 15:00:54 +00:00
Merge pull request #4310 from gswcodeshow/develop
添加Condition单元测试 #4298
This commit is contained in:
commit
26bc9e7264
@ -2668,3 +2668,48 @@ 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
|
||||
break
|
||||
}
|
||||
condPointMap[adr] = true
|
||||
|
||||
}
|
||||
}
|
||||
if cycleFlag {
|
||||
return
|
||||
}
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user