Skip to content

Commit

Permalink
Fix sorting and add single key w/ filter test.
Browse files Browse the repository at this point in the history
  • Loading branch information
notbdu committed Mar 4, 2019
1 parent 8e97c6c commit 7802ee1
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ func TestGroupByQuerySingleKeyNoFilterNoOrder(t *testing.T) {
"op": "COUNT"
}
],
"filters": [
{
"field": "st",
"op": "=",
"value": true
}
],
"group_by": [
"tt"
]}`,
Expand Down
140 changes: 140 additions & 0 deletions integration/groupby_query_single_key_with_filter_no_order_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// +build integration

package integration

import (
"sort"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGroupByQuerySingleKeyWithFilterNoOrder(t *testing.T) {
if testing.Short() {
t.SkipNow()
}

cfg := loadConfig(t, testConfig1)
ts := newTestServerSetup(t, cfg)
defer ts.close(t)

log := ts.dbOpts.InstrumentOptions().Logger()
log.Info("testing groupby query w/ single-key, no filter and no order")
require.NoError(t, ts.startServer())
log.Info("server is now up")

testData := `
{"service":"testNamespace","@timestamp":"2019-01-22T13:25:42-08:00","st":true,"sid":{"foo":1,"bar":2},"tt":"active","tz":-6,"v":1.5}
{"service":"testNamespace","@timestamp":"2019-01-22T13:26:42-08:00","st":true,"sid":{"foo":1,"bar":2},"tt":"active","tz":-6,"v":1.5}
{"service":"testNamespace","@timestamp":"2019-01-22T13:27:42-08:00","st":true,"sid":{"foo":1,"bar":2},"tt":"active","tz":-6,"v":1.5}
{"service":"testNamespace","@timestamp":"2019-01-22T13:28:42-08:00","st":true,"sid":{"foo":1,"bar":2},"tt":"active","tz":-6,"v":1.5}
{"service":"testNamespace","@timestamp":"2019-01-22T13:29:42-08:00","st":true,"sid":{"foo":1,"bar":2},"tt":"active","tz":-6,"v":1.5}
{"service":"testNamespace","@timestamp":"2019-01-22T13:30:42-08:00","st":true,"sid":{"foo":2,"bar":4},"tt":"active","tz":-6,"v":1.5}
{"service":"testNamespace","@timestamp":"2019-01-22T13:31:42-08:00","st":true,"sid":{"foo":2,"bar":4},"tt":"active","tz":-6,"v":1.5}
{"service":"testNamespace","@timestamp":"2019-01-22T13:32:42-08:00","st":true,"sid":{"foo":2,"bar":4},"tt":"active","tz":-6,"v":1.5}
{"service":"testNamespace","@timestamp":"2019-01-22T13:33:42-08:00","st":true,"sid":{"foo":2,"bar":4},"tt":"active","tz":-6,"v":1.5}
{"service":"testNamespace","@timestamp":"2019-01-22T13:34:42-08:00","st":true,"sid":{"foo":2,"bar":4},"tt":"active","tz":-6,"v":1.5}
{"service":"testNamespace","@timestamp":"2019-01-22T13:35:42-08:00","st":false,"sid":{"foo":3,"bar":6},"tt":"inactive","tz":-6,"v":15}
{"service":"testNamespace","@timestamp":"2019-01-22T13:36:42-08:00","st":false,"sid":{"foo":3,"bar":6},"tt":"inactive","tz":-6,"v":15}
{"service":"testNamespace","@timestamp":"2019-01-22T13:37:42-08:00","st":false,"sid":{"foo":3,"bar":6},"tt":"inactive","tz":-6,"v":15}
{"service":"testNamespace","@timestamp":"2019-01-22T13:38:42-08:00","st":false,"sid":{"foo":3,"bar":6},"tt":"inactive","tz":-6,"v":15}
{"service":"testNamespace","@timestamp":"2019-01-22T13:39:42-08:00","st":false,"sid":{"foo":3,"bar":6},"tt":"inactive","tz":-6,"v":15}
{"service":"testNamespace","@timestamp":"2019-01-22T13:40:42-08:00","st":false,"sid":{"foo":4,"bar":8},"tt":"inactive","tz":-6,"v":15}
{"service":"testNamespace","@timestamp":"2019-01-22T13:41:42-08:00","st":false,"sid":{"foo":4,"bar":8},"tt":"inactive","tz":-6,"v":15}
{"service":"testNamespace","@timestamp":"2019-01-22T13:42:42-08:00","st":false,"sid":{"foo":4,"bar":8},"tt":"inactive","tz":-6,"v":15}
{"service":"testNamespace","@timestamp":"2019-01-22T13:43:42-08:00","st":false,"sid":{"foo":4,"bar":8},"tt":"inactive","tz":-6,"v":15}
{"service":"testNamespace","@timestamp":"2019-01-22T13:44:42-08:00","st":false,"sid":{"foo":4,"bar":8},"tt":"inactive","tz":-6,"v":15}`

tests := []struct {
queryJSON string
expectedResults groupByQuerySingleKeyResults
}{
{
queryJSON: `{
"namespace": "testNamespace",
"start_time": 1548115200,
"end_time": 1548201600,
"calculations": [
{
"op": "COUNT"
}
],
"filters": [
{
"filters": [
{
"field": "st",
"op": "=",
"value": true
}
],
"filter_combinator": "AND"
}
],
"group_by": [
"tt"
]}`,
expectedResults: groupByQuerySingleKeyResults{
Groups: []singleKeyResultGroup{
singleKeyResultGroup{
Key: "active",
Values: []interface{}{float64(10)},
},
},
},
},
{
queryJSON: `{
"namespace": "testNamespace",
"start_time": 1548115200,
"end_time": 1548201600,
"calculations": [
{
"op": "COUNT"
}
],
"filters": [
{
"filters": [
{
"field": "st",
"op": "=",
"value": true
}
],
"filter_combinator": "AND"
}
],
"group_by": [
"sid.bar"
]}`,
expectedResults: groupByQuerySingleKeyResults{
Groups: []singleKeyResultGroup{
singleKeyResultGroup{
Key: float64(2),
Values: []interface{}{float64(5)},
},
singleKeyResultGroup{
Key: float64(4),
Values: []interface{}{float64(5)},
},
},
},
},
}

client := ts.newHTTPClient()
require.NoError(t, client.write([]byte(strings.TrimSpace(testData))))

for _, test := range tests {
results, err := client.queryGroupBySingleKey([]byte(test.queryJSON))
assert.NoError(t, err)
sort.Sort(results)
assert.Equal(t, test.expectedResults, results)
}

require.NoError(t, ts.stopServer())
log.Info("server is now down")
}
4 changes: 3 additions & 1 deletion integration/query_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration

import (
"errors"
"log"
)

const (
Expand Down Expand Up @@ -53,6 +54,7 @@ func (g groupByQuerySingleKeyResults) Less(i, j int) bool {
g1 := g.Groups[i]
g2 := g.Groups[j]

log.Printf("comparing key1: %v to key2: %v, less than?: %v", g1.Key, g2.Key, mustCompareAnyTypeLessThan(g1.Key, g2.Key) == compareResultLessThan)
return mustCompareAnyTypeLessThan(g1.Key, g2.Key) == compareResultLessThan
}

Expand Down Expand Up @@ -153,7 +155,7 @@ func compareAnyTypeLessThan(v1, v2 interface{}) (int, error) {
if v1Typed < v2Typed {
return compareResultLessThan, nil
}
return compareResultLessThan, nil
return compareResultGreaterThan, nil
case bool:
v1Typed := v1.(bool)
_, ok := v2.(bool)
Expand Down

0 comments on commit 7802ee1

Please sign in to comment.