Fix where group literals WhereOr and WhereAnd, and include more tests.

This commit is contained in:
Mosen
2016-07-22 23:16:58 +10:00
parent 4c10a0302f
commit 2ff0fd3ef2
2 changed files with 43 additions and 5 deletions

View File

@@ -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
}

View File

@@ -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,
)
}
}
}