Skip to content

Commit

Permalink
Added last of aggregate tests
Browse files Browse the repository at this point in the history
Add panic for cases where MatchFunc Record is accessed from an index.
  • Loading branch information
timshannon committed Jan 14, 2017
1 parent 967d881 commit f483553
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
6 changes: 2 additions & 4 deletions aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ func (a *AggregateResult) Sort(field string) {
sort.Sort((*aggregateResultSort)(a))
}

// Max Returns the maxiumum value of the Aggregate Grouping, uses the Comparer interface if field
// can be automatically compared
// Max Returns the maxiumum value of the Aggregate Grouping, uses the Comparer interface
func (a *AggregateResult) Max(field string, result interface{}) {
a.Sort(field)

Expand All @@ -127,8 +126,7 @@ func (a *AggregateResult) Max(field string, result interface{}) {
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
// can be automatically compared
// Min returns the minimum value of the Aggregate Grouping, uses the Comparer interface
func (a *AggregateResult) Min(field string, result interface{}) {
a.Sort(field)

Expand Down
11 changes: 6 additions & 5 deletions find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,16 +433,17 @@ var tests = []test{
},
test{
name: "Find item with max ID in each category - sub aggregate query",
query: bolthold.Where("Category").MatchFunc(func(ra *bolthold.RecordAccess) (bool, error) {
grp, err := ra.SubAggregateQuery(bolthold.Where("Category").Eq(ra.Field()), "Category")
query: bolthold.Where("ID").MatchFunc(func(ra *bolthold.RecordAccess) (bool, error) {
grp, err := ra.SubAggregateQuery(bolthold.Where("Category").
Eq(ra.Record().(*ItemTest).Category), "Category")
if err != nil {
return false, err
}

maxID := 0
max := &ItemTest{}

grp[0].Max("ID", &maxID)
return ra.Record().(*ItemTest).ID == maxID, nil
grp[0].Max("ID", max)
return ra.Field().(int) == max.ID, nil
}),
result: []int{11, 14, 15},
},
Expand Down
1 change: 0 additions & 1 deletion index.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ func newIterator(tx *bolt.Tx, typeName string, query *Query) *iterator {
return iter
}

// 3 scenarios
// Key field
if query.index == Key {
iter.indexCursor = tx.Bucket([]byte(typeName)).Cursor()
Expand Down
8 changes: 6 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ func (r *RecordAccess) Field() interface{} {

// Record is the complete record for a given row in bolthold
func (r *RecordAccess) Record() interface{} {
if r.record == nil {
// I don't like this, but it's better than not having access to Record for all other query types
panic("Current Record doesn't exists, because this criteria was executed on an index")
}
return r.record
}

Expand All @@ -293,8 +297,9 @@ func (r *RecordAccess) SubAggregateQuery(query *Query, groupBy ...string) ([]*Ag
// MatchFunc will test if a field matches the passed in function
func (c *Criterion) MatchFunc(match MatchFunc) *Query {
if c.query.currentField == Key {
panic("Match func cannot be used against Keys, as the Key type is unknown at runtime, and there is no value compare against.")
panic("Match func cannot be used against Keys, as the Key type is unknown at runtime, and there is no value compare against")
}

return c.op(fn, match)
}

Expand Down Expand Up @@ -354,7 +359,6 @@ func (c *Criterion) test(testValue interface{}, encoded bool) (bool, error) {
case re:
return c.value.(*regexp.Regexp).Match([]byte(fmt.Sprintf("%s", value))), nil
case fn:
//FIXME: c.query.currentRow not set on indexed queries
return c.value.(MatchFunc)(&RecordAccess{
field: value,
record: c.query.currentRow,
Expand Down

0 comments on commit f483553

Please sign in to comment.