-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix builder.Intervals marshal and filter.Interval (#68)
* exclude examples from build * fix outputType on expressions * Fix filtered aggregator * clean up * Support for quantilesDoublesSketch * remove extras * Fix Intervals marshal * fix filter intervals * add test for marhsal load chaining * fix intervals tests Co-authored-by: Shrivardhan Rao <[email protected]>
- Loading branch information
1 parent
e4912c6
commit 9ab16a4
Showing
4 changed files
with
169 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,80 @@ | ||
package filter | ||
|
||
import ( | ||
"testing" | ||
|
||
"encoding/json" | ||
"github.com/grafadruid/go-druid/builder" | ||
"github.com/grafadruid/go-druid/builder/intervals" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestLoadUnsupportedType(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
f, err := Load([]byte("{\"type\": \"blahblahType\"}")) | ||
|
||
assert.Nil(f, "filter should be nil") | ||
assert.NotNil(err, "error should not be nil") | ||
assert.Error(err, "unsupported filter type") | ||
assert.Nil(f, | ||
"filter should be nil") | ||
assert.NotNil(err, | ||
"error should not be nil") | ||
assert.Error(err, | ||
"unsupported filter type") | ||
} | ||
|
||
func TestNewInterval(t *testing.T) { | ||
location, _ := time.LoadLocation("UTC") | ||
start, _ := time.ParseInLocation(time.RFC3339Nano, | ||
"2022-06-16T08:28:53.33441Z", | ||
location) | ||
end, _ := time.ParseInLocation(time.RFC3339Nano, | ||
"2022-06-16T15:28:53.33441Z", | ||
location) | ||
// simple interval | ||
i := intervals.NewInterval().SetInterval(start, | ||
end) | ||
filter1 := NewSelector().SetDimension("countryName").SetValue("France") | ||
filterInterval := NewInterval().SetIntervals([]*intervals.Interval{i}).SetDimension("__time") | ||
filters := NewOr().SetFields([]builder.Filter{filter1, filterInterval}) | ||
|
||
t.Run("marshal filter with interval", | ||
func(t *testing.T) { | ||
f, err := json.Marshal(filters) | ||
assert.Nil(t, | ||
err) | ||
|
||
assert.Nil(t, | ||
err) | ||
assert.Equal(t, | ||
`{"type":"or","fields":[{"type":"selector","dimension":"countryName","value":"France"},{"type":"interval","dimension":"__time","intervals":["2022-06-16T08:28:53.33441Z/2022-06-16T15:28:53.33441Z"]}]}`, | ||
string(f), | ||
"filter with time interval") | ||
}) | ||
|
||
t.Run("marshal load marshal filter with interval", | ||
func(t *testing.T) { | ||
f, err := json.Marshal(filters) | ||
assert.Nil(t, | ||
err) | ||
|
||
filterWithIntervalObj, err := Load(f) | ||
assert.Nil(t, | ||
err) | ||
assert.NotNil(t, | ||
filterWithIntervalObj) | ||
assert.Equal(t, | ||
filters, | ||
filterWithIntervalObj) | ||
|
||
fJson, err := json.Marshal(filterWithIntervalObj) | ||
assert.Nil(t, | ||
err) | ||
|
||
assert.Nil(t, | ||
err) | ||
assert.Equal(t, | ||
`{"type":"or","fields":[{"type":"selector","dimension":"countryName","value":"France"},{"type":"interval","dimension":"__time","intervals":["2022-06-16T08:28:53.33441Z/2022-06-16T15:28:53.33441Z"]}]}`, | ||
string(fJson), | ||
"filter with time interval") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,103 @@ | ||
package intervals | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLoadUnsupportedType(t *testing.T) { | ||
func TestIntervals_Load(t *testing.T) { | ||
assert := assert.New(t) | ||
t.Run("test unsupported type", | ||
func(t *testing.T) { | ||
f, err := Load([]byte("{\"type\": \"blahblahType\"}")) | ||
|
||
f, err := Load([]byte("{\"type\": \"blahblahType\"}")) | ||
assert.Nil(f, | ||
"filter should be nil") | ||
assert.NotNil(err, | ||
"error should not be nil") | ||
assert.Error(err, | ||
"unsupported intervals type") | ||
}) | ||
|
||
assert.Nil(f, "filter should be nil") | ||
assert.NotNil(err, "error should not be nil") | ||
assert.Error(err, "unsupported intervals type") | ||
t.Run("simple interval not supported", | ||
func(t *testing.T) { | ||
f, err := Load([]byte(`"2022-06-16T08:28:53.33441Z/2022-06-16T15:28:53.33441Z"`)) | ||
|
||
assert.Nil(f, | ||
"filter should be nil") | ||
assert.NotNil(err, | ||
"error should not be nil") | ||
assert.Error(err, | ||
"unsupported intervals type") | ||
}) | ||
|
||
t.Run("complex interval supported", | ||
func(t *testing.T) { | ||
f, err := Load([]byte(`{"type":"intervals","intervals":["2022-06-16T08:28:53.33441Z/2022-06-16T15:28:53.33441Z"]}`)) | ||
location, _ := time.LoadLocation("UTC") | ||
start, _ := time.ParseInLocation(time.RFC3339Nano, | ||
"2022-06-16T08:28:53.33441Z", | ||
location) | ||
end, _ := time.ParseInLocation(time.RFC3339Nano, | ||
"2022-06-16T15:28:53.33441Z", | ||
location) | ||
// simple interval | ||
interval := NewInterval().SetInterval(start, | ||
end) | ||
// complex intervals | ||
intervals := NewIntervals().SetIntervals([]*Interval{interval}) | ||
|
||
assert.Nil(err, | ||
"error should be nil") | ||
assert.Equal(intervals, | ||
f, | ||
"loaded intervals match the built intervals") | ||
}) | ||
} | ||
|
||
func TestIntervals_MarshalJSON(t *testing.T) { | ||
location, _ := time.LoadLocation("UTC") | ||
start, _ := time.ParseInLocation(time.RFC3339Nano, | ||
"2022-06-16T08:28:53.33441Z", | ||
location) | ||
end, _ := time.ParseInLocation(time.RFC3339Nano, | ||
"2022-06-16T15:28:53.33441Z", | ||
location) | ||
// simple interval | ||
interval := NewInterval().SetInterval(start, | ||
end) | ||
// complex intervals | ||
intervals := NewIntervals().SetIntervals([]*Interval{interval}) | ||
|
||
t.Run("simple interval returns a string", | ||
func(t *testing.T) { | ||
f, err := json.Marshal(interval) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
assert.Nil(t, | ||
err) | ||
assert.Equal(t, | ||
`"2022-06-16T08:28:53.33441Z/2022-06-16T15:28:53.33441Z"`, | ||
string(f), | ||
"simple interval returns a string") | ||
}) | ||
|
||
t.Run("marshal generates complex interval type", | ||
func(t *testing.T) { | ||
f, err := json.Marshal(intervals) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
assert.Nil(t, | ||
err) | ||
assert.Equal(t, | ||
`{"type":"intervals","intervals":["2022-06-16T08:28:53.33441Z/2022-06-16T15:28:53.33441Z"]}`, | ||
string(f), | ||
"complex interval returns a struct") | ||
}) | ||
} |