Skip to content

Commit 4c31f4c

Browse files
committed
fix #1 - implicit was not being tested
1 parent 0608ed7 commit 4c31f4c

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
2-
chaincode
2+
vendor
3+
.vscode

query/query.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package query
22

33
import (
4+
"encoding/json"
45
"errors"
5-
"github.com/wearetheledger/go-couchdb-query-engine/query/rules"
6-
"reflect"
76
"fmt"
7+
"reflect"
88
"strings"
9-
"encoding/json"
9+
10+
"github.com/wearetheledger/go-couchdb-query-engine/query/rules"
1011
)
1112

1213
var Registry []rules.IGenericRule
@@ -141,6 +142,8 @@ func ParseCouchDBQueryString(data map[string]interface{}, userQueryString string
141142

142143
func test(data interface{}, userQuery interface{}) (bool, error) {
143144

145+
last := true
146+
144147
if canDecend(userQuery) && reflect.TypeOf(userQuery).Kind() == reflect.Map {
145148

146149
userQuery, ok := userQuery.(map[string]interface{})
@@ -164,23 +167,44 @@ func test(data interface{}, userQuery interface{}) (bool, error) {
164167
return false, err1
165168
}
166169

167-
return matched, nil
170+
last = last && matched
168171

169172
} else {
170173

171174
dvp, dk := resolveSubdocumentQuery(data, k)
172175

176+
var result bool
177+
var err1 error
178+
173179
if dvp != nil && len(dk) == 1 {
174-
return test(dvp[dk[0]], v)
180+
result, err1 = test(dvp[dk[0]], v)
181+
182+
} else {
183+
result, err1 = test(nil, v)
175184
}
176185

177-
return test(nil, v)
186+
if err1 != nil {
187+
return false, err1
188+
}
189+
190+
last = last && result
191+
178192
}
179193
}
180194

195+
return last, nil
196+
181197
} else {
182198

183-
return rules.Eq.Match(data, userQuery)
199+
bool, err1 := rules.Eq.Match(data, userQuery)
200+
201+
if err1 != nil {
202+
return false, err1
203+
}
204+
205+
last = last && bool
206+
207+
return last, nil
184208
}
185209

186210
return false, nil

test/implicit_and_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/wearetheledger/go-couchdb-query-engine/query"
7+
)
8+
9+
func TestImplicitAnd(t *testing.T) {
10+
11+
t.Run("Element should be returned when owner bob & size 3 with implicit and and explicit eq", func(t *testing.T) {
12+
13+
res, err := query.ParseCouchDBQuery(TestData, map[string]interface{}{
14+
"selector": map[string]interface{}{
15+
"owner": map[string]interface{}{
16+
"$eq": "bob",
17+
},
18+
"size": map[string]interface{}{
19+
"$eq": 3,
20+
},
21+
},
22+
})
23+
24+
if err != nil {
25+
t.Error(err)
26+
}
27+
28+
if len(res) != 1 {
29+
t.Error("Query should have returned 1 results")
30+
}
31+
32+
})
33+
34+
t.Run("Element should be returned when owner bob & size 3 with implicit and and implicit eq", func(t *testing.T) {
35+
36+
res, err := query.ParseCouchDBQuery(TestData, map[string]interface{}{
37+
"selector": map[string]interface{}{
38+
"owner": "bob",
39+
"size": 3,
40+
},
41+
})
42+
43+
if err != nil {
44+
t.Error(err)
45+
}
46+
47+
if len(res) != 1 {
48+
t.Error("Query should have returned 1 results")
49+
}
50+
})
51+
}

0 commit comments

Comments
 (0)