github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_collect_with_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 TestCollectWithCount(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 gender = i.gender WITH COUNT INTO genders 52 RETURN {gender, genders} 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, `[{"gender":"f","genders":2},{"gender":"m","genders":3}]`) 62 }) 63 64 Convey("Should count grouped values with multiple keys", t, func() { 65 c := compiler.New() 66 67 prog, err := c.Compile(` 68 LET users = [ 69 { 70 active: true, 71 age: 31, 72 gender: "m", 73 married: true 74 }, 75 { 76 active: true, 77 age: 25, 78 gender: "f", 79 married: false 80 }, 81 { 82 active: true, 83 age: 36, 84 gender: "m", 85 married: false 86 }, 87 { 88 active: false, 89 age: 69, 90 gender: "m", 91 married: true 92 }, 93 { 94 active: true, 95 age: 45, 96 gender: "f", 97 married: true 98 } 99 ] 100 FOR i IN users 101 COLLECT gender = i.gender, married = i.married WITH COUNT INTO genders 102 RETURN {gender, married, genders} 103 `) 104 105 So(err, ShouldBeNil) 106 So(prog, ShouldHaveSameTypeAs, &runtime.Program{}) 107 108 out, err := prog.Run(context.Background()) 109 110 So(err, ShouldBeNil) 111 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}]`) 112 }) 113 } 114 115 func BenchmarkWithCount(b *testing.B) { 116 p := compiler.New().MustCompile(` 117 LET users = [ 118 { 119 active: true, 120 age: 31, 121 gender: "m", 122 married: true 123 }, 124 { 125 active: true, 126 age: 25, 127 gender: "f", 128 married: false 129 }, 130 { 131 active: true, 132 age: 36, 133 gender: "m", 134 married: false 135 }, 136 { 137 active: false, 138 age: 69, 139 gender: "m", 140 married: true 141 }, 142 { 143 active: true, 144 age: 45, 145 gender: "f", 146 married: true 147 } 148 ] 149 FOR i IN users 150 COLLECT gender = i.gender WITH COUNT INTO genders 151 RETURN {gender, genders} 152 `) 153 154 for n := 0; n < b.N; n++ { 155 p.Run(context.Background()) 156 } 157 } 158 159 func BenchmarkWithCount2(b *testing.B) { 160 p := compiler.New().MustCompile(` 161 LET users = [ 162 { 163 active: true, 164 age: 31, 165 gender: "m", 166 married: true 167 }, 168 { 169 active: true, 170 age: 25, 171 gender: "f", 172 married: false 173 }, 174 { 175 active: true, 176 age: 36, 177 gender: "m", 178 married: false 179 }, 180 { 181 active: false, 182 age: 69, 183 gender: "m", 184 married: true 185 }, 186 { 187 active: true, 188 age: 45, 189 gender: "f", 190 married: true 191 } 192 ] 193 FOR i IN users 194 COLLECT gender = i.gender, married = i.married WITH COUNT INTO genders 195 RETURN {gender, married, genders} 196 `) 197 198 for n := 0; n < b.N; n++ { 199 p.Run(context.Background()) 200 } 201 }