Skip to content

Commit

Permalink
Merge pull request #31 from rmg/empty-result-on-empty-bucket
Browse files Browse the repository at this point in the history
Give empty results on empty bucket queries
  • Loading branch information
timshannon authored May 1, 2018
2 parents 66d5d23 + acc7aa3 commit 3e24834
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ install:

script:
- go test -v -covermode=count -coverprofile=coverage.out
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci
15 changes: 15 additions & 0 deletions find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,21 @@ func TestFindOnInvalidIndex(t *testing.T) {
})
}

func TestFindOnEmptyBucketWithIndex(t *testing.T) {
testWrap(t, func(store *bolthold.Store, t *testing.T) {
// DO NOT INSERT DATA
var result []ItemTest

err := store.Find(&result, bolthold.Where("Category").Eq("animal").Index("Category"))
if err != nil {
t.Fatalf("Find query against a valid index name but an empty data bucket return an error!")
}
if len(result) > 0 {
t.Fatalf("Find query against an empty bucket returned results!")
}
})
}

func TestQueryStringPrint(t *testing.T) {
q := bolthold.Where("FirstField").Eq("first value").And("SecondField").Gt("Second Value").And("ThirdField").
Lt("Third Value").And("FourthField").Ge("FourthValue").And("FifthField").Le("FifthValue").And("SixthField").
Expand Down
7 changes: 7 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,13 @@ type record struct {

func runQuery(tx *bolt.Tx, dataType interface{}, query *Query, retrievedKeys keyList, skip int, action func(r *record) error) error {
storer := newStorer(dataType)

bkt := tx.Bucket([]byte(storer.Type()))
if bkt == nil || bkt.Stats().KeyN == 0 {
// if the bucket doesn't exist or is empty then our job is really easy!
return nil
}

if query.index != "" && tx.Bucket(indexBucketName(storer.Type(), query.index)) == nil {
return fmt.Errorf("The index %s does not exist", query.index)
}
Expand Down

0 comments on commit 3e24834

Please sign in to comment.