mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-16 11:37:36 +02:00
200 lines
3.5 KiB
Go
200 lines
3.5 KiB
Go
|
package compiler_test
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/MontFerret/ferret/pkg/compiler"
|
||
|
"github.com/MontFerret/ferret/pkg/runtime"
|
||
|
. "github.com/smartystreets/goconvey/convey"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestCollectWithCount(t *testing.T) {
|
||
|
Convey("Should count grouped values", t, func() {
|
||
|
c := compiler.New()
|
||
|
|
||
|
prog, err := c.Compile(`
|
||
|
LET users = [
|
||
|
{
|
||
|
active: true,
|
||
|
age: 31,
|
||
|
gender: "m",
|
||
|
married: true
|
||
|
},
|
||
|
{
|
||
|
active: true,
|
||
|
age: 25,
|
||
|
gender: "f",
|
||
|
married: false
|
||
|
},
|
||
|
{
|
||
|
active: true,
|
||
|
age: 36,
|
||
|
gender: "m",
|
||
|
married: false
|
||
|
},
|
||
|
{
|
||
|
active: false,
|
||
|
age: 69,
|
||
|
gender: "m",
|
||
|
married: true
|
||
|
},
|
||
|
{
|
||
|
active: true,
|
||
|
age: 45,
|
||
|
gender: "f",
|
||
|
married: true
|
||
|
}
|
||
|
]
|
||
|
FOR i IN users
|
||
|
COLLECT gender = i.gender WITH COUNT INTO genders
|
||
|
RETURN {gender, genders}
|
||
|
`)
|
||
|
|
||
|
So(err, ShouldBeNil)
|
||
|
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
|
||
|
|
||
|
out, err := prog.Run(context.Background())
|
||
|
|
||
|
So(err, ShouldBeNil)
|
||
|
So(string(out), ShouldEqual, `[{"gender":"f","genders":2},{"gender":"m","genders":3}]`)
|
||
|
})
|
||
|
|
||
|
Convey("Should count grouped values with multiple keys", t, func() {
|
||
|
c := compiler.New()
|
||
|
|
||
|
prog, err := c.Compile(`
|
||
|
LET users = [
|
||
|
{
|
||
|
active: true,
|
||
|
age: 31,
|
||
|
gender: "m",
|
||
|
married: true
|
||
|
},
|
||
|
{
|
||
|
active: true,
|
||
|
age: 25,
|
||
|
gender: "f",
|
||
|
married: false
|
||
|
},
|
||
|
{
|
||
|
active: true,
|
||
|
age: 36,
|
||
|
gender: "m",
|
||
|
married: false
|
||
|
},
|
||
|
{
|
||
|
active: false,
|
||
|
age: 69,
|
||
|
gender: "m",
|
||
|
married: true
|
||
|
},
|
||
|
{
|
||
|
active: true,
|
||
|
age: 45,
|
||
|
gender: "f",
|
||
|
married: true
|
||
|
}
|
||
|
]
|
||
|
FOR i IN users
|
||
|
COLLECT gender = i.gender, married = i.married WITH COUNT INTO genders
|
||
|
RETURN {gender, married, genders}
|
||
|
`)
|
||
|
|
||
|
So(err, ShouldBeNil)
|
||
|
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
|
||
|
|
||
|
out, err := prog.Run(context.Background())
|
||
|
|
||
|
So(err, ShouldBeNil)
|
||
|
So(string(out), ShouldEqual, `[{"gender":"f","genders":1,"married":false},{"gender":"f","genders":1,"married":true},{"gender":"m","genders":1,"married":false},{"gender":"m","genders":2,"married":true}]`)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func BenchmarkWithCount(b *testing.B) {
|
||
|
p := compiler.New().MustCompile(`
|
||
|
LET users = [
|
||
|
{
|
||
|
active: true,
|
||
|
age: 31,
|
||
|
gender: "m",
|
||
|
married: true
|
||
|
},
|
||
|
{
|
||
|
active: true,
|
||
|
age: 25,
|
||
|
gender: "f",
|
||
|
married: false
|
||
|
},
|
||
|
{
|
||
|
active: true,
|
||
|
age: 36,
|
||
|
gender: "m",
|
||
|
married: false
|
||
|
},
|
||
|
{
|
||
|
active: false,
|
||
|
age: 69,
|
||
|
gender: "m",
|
||
|
married: true
|
||
|
},
|
||
|
{
|
||
|
active: true,
|
||
|
age: 45,
|
||
|
gender: "f",
|
||
|
married: true
|
||
|
}
|
||
|
]
|
||
|
FOR i IN users
|
||
|
COLLECT gender = i.gender WITH COUNT INTO genders
|
||
|
RETURN {gender, genders}
|
||
|
`)
|
||
|
|
||
|
for n := 0; n < b.N; n++ {
|
||
|
p.Run(context.Background())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func BenchmarkWithCount2(b *testing.B) {
|
||
|
p := compiler.New().MustCompile(`
|
||
|
LET users = [
|
||
|
{
|
||
|
active: true,
|
||
|
age: 31,
|
||
|
gender: "m",
|
||
|
married: true
|
||
|
},
|
||
|
{
|
||
|
active: true,
|
||
|
age: 25,
|
||
|
gender: "f",
|
||
|
married: false
|
||
|
},
|
||
|
{
|
||
|
active: true,
|
||
|
age: 36,
|
||
|
gender: "m",
|
||
|
married: false
|
||
|
},
|
||
|
{
|
||
|
active: false,
|
||
|
age: 69,
|
||
|
gender: "m",
|
||
|
married: true
|
||
|
},
|
||
|
{
|
||
|
active: true,
|
||
|
age: 45,
|
||
|
gender: "f",
|
||
|
married: true
|
||
|
}
|
||
|
]
|
||
|
FOR i IN users
|
||
|
COLLECT gender = i.gender, married = i.married WITH COUNT INTO genders
|
||
|
RETURN {gender, married, genders}
|
||
|
`)
|
||
|
|
||
|
for n := 0; n < b.N; n++ {
|
||
|
p.Run(context.Background())
|
||
|
}
|
||
|
}
|