Skip to content

Commit

Permalink
fix: skip and limit (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
yakumioto authored and sneljo1 committed Sep 6, 2018
1 parent 0077c94 commit 6763700
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 6 deletions.
20 changes: 14 additions & 6 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,37 @@ func ParseCouchDBQueryString(data map[string]interface{}, userQueryString string
var limit = len(response)

if userQuery["skip"] != nil {
skip, ok := userQuery["skip"].(int)
skipF64, ok := userQuery["skip"].(float64)

if ok {
skip = skip
skip = int(skipF64)
} else {
return StateCouchDBQueryResult{}, errors.New("Skip must be an integer")
}
}

if userQuery["limit"] != nil {
limit, ok := userQuery["limit"].(int)
limitF64, ok := userQuery["limit"].(float64)

if ok {
limit = limit
limit = int(limitF64)
} else {
return StateCouchDBQueryResult{}, errors.New("Limit must be an integer")
}

}

response = response[skip:limit]
if skip < len(response) {
response = response[skip:]
} else {
return StateCouchDBQueryResult{}, nil
}

if limit > len(response) {
limit = len(response)
}

return response, nil
return response[:limit], nil

}

Expand Down
68 changes: 68 additions & 0 deletions test/limit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package test

import (
"testing"

"github.com/wearetheledger/go-couchdb-query-engine/query"
)

func TestLimit(t *testing.T) {

t.Run("Element should be returned when not equal owner bob & limit 1", func(t *testing.T) {

res, err := query.ParseCouchDBQuery(TestData, map[string]interface{}{
"selector": map[string]interface{}{
"$and": []interface{}{
map[string]interface{}{
"owner": map[string]interface{}{
"$ne": "bob",
},
},
map[string]interface{}{
"size": map[string]interface{}{
"$ne": 3,
},
},
},
},
"limit": 1,
})

if err != nil {
t.Error(err)
}

if len(res) != 1 {
t.Error("Query should have returned 2 results")
}
})

t.Run("Element should be returned when not equal owner bob & limit 5", func(t *testing.T) {

res, err := query.ParseCouchDBQuery(TestData, map[string]interface{}{
"selector": map[string]interface{}{
"$and": []interface{}{
map[string]interface{}{
"owner": map[string]interface{}{
"$ne": "bob",
},
},
map[string]interface{}{
"size": map[string]interface{}{
"$ne": 3,
},
},
},
},
"limit": 5,
})

if err != nil {
t.Error(err)
}

if len(res) != 2 {
t.Error("Query should have returned 2 results")
}
})
}
68 changes: 68 additions & 0 deletions test/skip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package test

import (
"testing"

"github.com/wearetheledger/go-couchdb-query-engine/query"
)

func TestSkip(t *testing.T) {
t.Run("Element should be returned when not equal owner bob & skip 1", func(t *testing.T) {

res, err := query.ParseCouchDBQuery(TestData, map[string]interface{}{
"selector": map[string]interface{}{
"$and": []interface{}{
map[string]interface{}{
"owner": map[string]interface{}{
"$ne": "bob",
},
},
map[string]interface{}{
"size": map[string]interface{}{
"$ne": 3,
},
},
},
},
"skip": 1,
})

if err != nil {
t.Error(err)
}

if len(res) != 1 {
t.Log(len(res))
t.Error("Query should have returned 2 results")
}
})

t.Run("Element should be returned when not equal owner bob & skip 5", func(t *testing.T) {

res, err := query.ParseCouchDBQuery(TestData, map[string]interface{}{
"selector": map[string]interface{}{
"$and": []interface{}{
map[string]interface{}{
"owner": map[string]interface{}{
"$ne": "bob",
},
},
map[string]interface{}{
"size": map[string]interface{}{
"$ne": 3,
},
},
},
},
"skip": 5,
})

if err != nil {
t.Error(err)
}

if len(res) != 0 {
t.Error("Query should have returned 2 results")
}
})
}

0 comments on commit 6763700

Please sign in to comment.