github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_collect_count_test.go (about) 1 package compiler_test 2 3 import ( 4 "context" 5 "testing" 6 7 . "github.com/smartystreets/goconvey/convey" 8 9 "github.com/MontFerret/ferret/pkg/compiler" 10 "github.com/MontFerret/ferret/pkg/runtime" 11 ) 12 13 func TestCollectCount(t *testing.T) { 14 Convey("Should count grouped values", t, func() { 15 c := compiler.New() 16 17 prog, err := c.Compile(` 18 LET users = [ 19 { 20 active: true, 21 age: 31, 22 gender: "m", 23 married: true 24 }, 25 { 26 active: true, 27 age: 25, 28 gender: "f", 29 married: false 30 }, 31 { 32 active: true, 33 age: 36, 34 gender: "m", 35 married: false 36 }, 37 { 38 active: false, 39 age: 69, 40 gender: "m", 41 married: true 42 }, 43 { 44 active: true, 45 age: 45, 46 gender: "f", 47 married: true 48 } 49 ] 50 FOR i IN users 51 COLLECT WITH COUNT INTO c 52 RETURN c 53 `) 54 55 So(err, ShouldBeNil) 56 So(prog, ShouldHaveSameTypeAs, &runtime.Program{}) 57 58 out, err := prog.Run(context.Background()) 59 60 So(err, ShouldBeNil) 61 So(string(out), ShouldEqual, `[5]`) 62 }) 63 } 64 65 func BenchmarkCollectCount(b *testing.B) { 66 p := compiler.New().MustCompile(` 67 LET users = [ 68 { 69 active: true, 70 age: 31, 71 gender: "m", 72 married: true 73 }, 74 { 75 active: true, 76 age: 25, 77 gender: "f", 78 married: false 79 }, 80 { 81 active: true, 82 age: 36, 83 gender: "m", 84 married: false 85 }, 86 { 87 active: false, 88 age: 69, 89 gender: "m", 90 married: true 91 }, 92 { 93 active: true, 94 age: 45, 95 gender: "f", 96 married: true 97 } 98 ] 99 FOR i IN users 100 COLLECT WITH COUNT INTO c 101 RETURN c 102 `) 103 104 for n := 0; n < b.N; n++ { 105 p.Run(context.Background()) 106 } 107 }