Skip to content

Commit

Permalink
Fixed a few issues with aggregates
Browse files Browse the repository at this point in the history
  • Loading branch information
timshannon committed Dec 31, 2016
1 parent 1d56cf4 commit ec1de03
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
10 changes: 9 additions & 1 deletion aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (a *AggregateResult) Max(field string, result interface{}) {
panic("result argument must not be nil")
}

resultVal.Elem().Set(a.reduction[:len(a.reduction)-1][0].Elem())
resultVal.Elem().Set(a.reduction[len(a.reduction)-1].Elem())
}

// Min returns the minimum value of the Aggregate Grouping, uses the Comparer interface if field
Expand Down Expand Up @@ -162,3 +162,11 @@ func (s *Store) FindAggregate(dataType interface{}, query *Query, groupBy string
func (s *Store) TxFindAggregate(tx *bolt.Tx, dataType interface{}, query *Query, groupBy string) ([]*AggregateResult, error) {
return aggregateQuery(tx, dataType, query, groupBy)
}

// Print ...
func (a *AggregateResult) Print(label string) {
fmt.Println(label + ": ")
for i := range a.reduction {
fmt.Println(a.reduction[i])
}
}
36 changes: 27 additions & 9 deletions aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package bolthold_test

import (
"fmt"
"testing"

"github.com/timshannon/bolthold"
Expand Down Expand Up @@ -40,7 +39,7 @@ func TestFindAggregateGroup(t *testing.T) {
}
}

//test min
//test min / max / count
for i := range result {
min := &ItemTest{}
max := &ItemTest{}
Expand All @@ -51,26 +50,45 @@ func TestFindAggregateGroup(t *testing.T) {
result[i].Min("ID", min)
result[i].Max("ID", max)

fmt.Printf("Max %s: %d\n", group, max.Key)
fmt.Printf("Min %s: %d\n", group, min.Key)

switch group {
case "animal":
result[i].Print("animal")
if !min.equal(&testData[5]) {
t.Fatalf("Expected min value of %v Got %v", testData[5], min)
t.Fatalf("Expected animal min value of %v Got %v", testData[5], min)
}
if !max.equal(&testData[14]) {
t.Fatalf("Expected animal max value of %v Got %v", testData[5], max)
}

if result[i].Count() != 7 {
t.Fatalf("Expected animal count of %d got %d", 7, result[i].Count())
}

case "food":
if !min.equal(&testData[7]) {
t.Fatalf("Expected min value of %v Got %v", testData[7], min)
t.Fatalf("Expected food min value of %v Got %v", testData[7], min)
}
if !max.equal(&testData[15]) {
t.Fatalf("Expected food max value of %v Got %v", testData[7], max)
}

if result[i].Count() != 5 {
t.Fatalf("Expected food count of %d got %d", 5, result[i].Count())
}

case "vehicle":
if !min.equal(&testData[1]) {
t.Fatalf("Expected min value of %v Got %v", testData[1], min)
t.Fatalf("Expected vehicle min value of %v Got %v", testData[1], min)
}
if !max.equal(&testData[11]) {
t.Fatalf("Expected vehicle max value of %v Got %v", testData[1], max)
}

if result[i].Count() != 5 {
t.Fatalf("Expected vehicle count of %d got %d", 5, result[i].Count())
}
}
}
// test max
// test avg
// test count

Expand Down

0 comments on commit ec1de03

Please sign in to comment.