Skip to content

Commit

Permalink
Add groupby query integration test.
Browse files Browse the repository at this point in the history
  • Loading branch information
notbdu committed Feb 13, 2019
1 parent 4caa1c4 commit 5289bfd
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
17 changes: 17 additions & 0 deletions integration/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ func (c client) queryRaw(data []byte) (rawQueryResults, error) {
return results, nil
}

func (c client) queryGroupBy(data []byte) (groupByQueryResults, error) {
req, err := http.NewRequest(http.MethodPost, c.queryURL, bytes.NewReader(data))
if err != nil {
return groupByQueryResults{}, err
}
resp, err := c.do(req)
if err != nil {
return groupByQueryResults{}, err
}
var results groupByQueryResults
err = json.Unmarshal(resp, &results)
if err != nil {
return groupByQueryResults{}, fmt.Errorf("unable to unmarshal response: %v", err)
}
return results, nil
}

func (c client) do(req *http.Request) ([]byte, error) {
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
Expand Down
85 changes: 85 additions & 0 deletions integration/groupby_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// +build integration

package integration

import (
"encoding/json"
"strings"
"testing"

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

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

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

log := ts.dbOpts.InstrumentOptions().Logger()
log.Info("testing various groupby query")
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 string
}{
{
queryJSON: `{
"namespace": "testNamespace",
"start_time": 1548115200,
"end_time": 1548201600,
"calculations": [
{
"op": "COUNT"
}
],
"group_by": [
"st",
"tt"
]}`,
expectedResults: `{"groups":[{"key":[false,"inactive"],"values":[10]},{"key":[true,"active"],"values":[10]}]}`,
},
}

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

for _, test := range tests {
resp, err := client.queryGroupBy([]byte(test.queryJSON))
assert.NoError(t, err)
b, err := json.Marshal(resp)
assert.NoError(t, err)
log.Infof("FINAL RESP: %s", string(b))
assert.Equal(t, string(b), test.expectedResults)
}

require.NoError(t, ts.stopServer())
log.Info("server is now down")
}
4 changes: 4 additions & 0 deletions integration/query_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ package integration
type rawQueryResults struct {
Raw []string `json:"raw"`
}

type groupByQueryResults struct {
Groups []interface{} `json:"groups"`
}

0 comments on commit 5289bfd

Please sign in to comment.