mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-08 10:45:34 +08:00
Fix where group literals WhereOr and WhereAnd, and include more tests.
This commit is contained in:
@@ -53,12 +53,28 @@ type whereer interface {
|
||||
|
||||
type WhereAnd []Where
|
||||
|
||||
func (wa WhereAnd) String() {
|
||||
return strings.Join(wa, " AND ")
|
||||
func (wa WhereAnd) String() string {
|
||||
var result string
|
||||
for i, cond := range wa {
|
||||
result += cond.String()
|
||||
if i < len(wa)-1 {
|
||||
result += " AND "
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
type WhereOr []Where
|
||||
|
||||
func (wo WhereOr) String() {
|
||||
return strings.Join(wo, " OR ")
|
||||
func (wo WhereOr) String() string {
|
||||
var result string
|
||||
for i, cond := range wo {
|
||||
result += cond.String()
|
||||
if i < len(wo)-1 {
|
||||
result += " OR "
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -31,7 +31,10 @@ func TestWhere(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
var waTests = []testpair{
|
||||
var waTests = []struct {
|
||||
when WhereAnd
|
||||
then string
|
||||
}{
|
||||
{WhereAnd{Where{"field", "value", "="}, Where{"field", "bar", "="}}, "field = 'value' AND field = 'bar'"},
|
||||
}
|
||||
|
||||
@@ -46,3 +49,22 @@ func TestWhereAnd_String(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var woTests = []struct {
|
||||
when WhereOr
|
||||
then string
|
||||
}{
|
||||
{WhereOr{Where{"field", "value", "="}, Where{"field", "bar", "="}}, "field = 'value' OR field = 'bar'"},
|
||||
}
|
||||
|
||||
func TestWhereOr_String(t *testing.T) {
|
||||
for _, test := range woTests {
|
||||
v := test.when.String()
|
||||
if v != test.then {
|
||||
t.Error(
|
||||
"Expected", test.then,
|
||||
"got", v,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user