github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_collect_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 TestCollect(t *testing.T) { 14 Convey("Should not have access to initial variables", t, func() { 15 c := compiler.New() 16 17 _, err := c.Compile(` 18 LET users = [ 19 { 20 active: true, 21 married: true, 22 age: 31, 23 gender: "m" 24 }, 25 { 26 active: true, 27 married: false, 28 age: 25, 29 gender: "f" 30 }, 31 { 32 active: true, 33 married: false, 34 age: 36, 35 gender: "m" 36 }, 37 { 38 active: false, 39 married: true, 40 age: 69, 41 gender: "m" 42 }, 43 { 44 active: true, 45 married: true, 46 age: 45, 47 gender: "f" 48 } 49 ] 50 FOR i IN users 51 COLLECT gender = i.gender 52 RETURN { 53 user: i, 54 gender: gender 55 } 56 `) 57 58 So(err, ShouldNotBeNil) 59 }) 60 61 Convey("Should not have access to variables defined before COLLECT", t, func() { 62 c := compiler.New() 63 64 _, err := c.Compile(` 65 LET users = [ 66 { 67 active: true, 68 married: true, 69 age: 31, 70 gender: "m" 71 }, 72 { 73 active: true, 74 married: false, 75 age: 25, 76 gender: "f" 77 }, 78 { 79 active: true, 80 married: false, 81 age: 36, 82 gender: "m" 83 }, 84 { 85 active: false, 86 married: true, 87 age: 69, 88 gender: "m" 89 }, 90 { 91 active: true, 92 married: true, 93 age: 45, 94 gender: "f" 95 } 96 ] 97 FOR i IN users 98 LET x = "foo" 99 COLLECT gender = i.gender 100 RETURN {x, gender} 101 `) 102 103 So(err, ShouldNotBeNil) 104 }) 105 106 Convey("Should group result by a single key", t, func() { 107 c := compiler.New() 108 109 prog, err := c.Compile(` 110 LET users = [ 111 { 112 active: true, 113 married: true, 114 age: 31, 115 gender: "m" 116 }, 117 { 118 active: true, 119 married: false, 120 age: 25, 121 gender: "f" 122 }, 123 { 124 active: true, 125 married: false, 126 age: 36, 127 gender: "m" 128 }, 129 { 130 active: false, 131 married: true, 132 age: 69, 133 gender: "m" 134 }, 135 { 136 active: true, 137 married: true, 138 age: 45, 139 gender: "f" 140 } 141 ] 142 FOR i IN users 143 COLLECT gender = i.gender 144 RETURN gender 145 `) 146 147 So(err, ShouldBeNil) 148 So(prog, ShouldHaveSameTypeAs, &runtime.Program{}) 149 150 out, err := prog.Run(context.Background()) 151 152 So(err, ShouldBeNil) 153 So(string(out), ShouldEqual, `["f","m"]`) 154 }) 155 156 Convey("Should group result by multiple keys", t, func() { 157 c := compiler.New() 158 159 prog, err := c.Compile(` 160 LET users = [ 161 { 162 active: true, 163 married: true, 164 age: 31, 165 gender: "m" 166 }, 167 { 168 active: true, 169 married: false, 170 age: 25, 171 gender: "f" 172 }, 173 { 174 active: true, 175 married: false, 176 age: 36, 177 gender: "m" 178 }, 179 { 180 active: false, 181 married: true, 182 age: 69, 183 gender: "m" 184 }, 185 { 186 active: true, 187 married: true, 188 age: 45, 189 gender: "f" 190 } 191 ] 192 FOR i IN users 193 COLLECT gender = i.gender, age = i.age 194 RETURN {age, gender} 195 `) 196 197 So(err, ShouldBeNil) 198 So(prog, ShouldHaveSameTypeAs, &runtime.Program{}) 199 200 out, err := prog.Run(context.Background()) 201 202 So(err, ShouldBeNil) 203 So(string(out), ShouldEqual, `[{"age":25,"gender":"f"},{"age":45,"gender":"f"},{"age":31,"gender":"m"},{"age":36,"gender":"m"},{"age":69,"gender":"m"}]`) 204 }) 205 } 206 207 func BenchmarkCollect(b *testing.B) { 208 p := compiler.New().MustCompile(` 209 LET users = [ 210 { 211 active: true, 212 married: true, 213 age: 31, 214 gender: "m" 215 }, 216 { 217 active: true, 218 married: false, 219 age: 25, 220 gender: "f" 221 }, 222 { 223 active: true, 224 married: false, 225 age: 36, 226 gender: "m" 227 }, 228 { 229 active: false, 230 married: true, 231 age: 69, 232 gender: "m" 233 }, 234 { 235 active: true, 236 married: true, 237 age: 45, 238 gender: "f" 239 } 240 ] 241 FOR i IN users 242 COLLECT gender = i.gender 243 RETURN gender 244 `) 245 246 for n := 0; n < b.N; n++ { 247 p.Run(context.Background()) 248 } 249 } 250 251 func BenchmarkCollect2(b *testing.B) { 252 p := compiler.New().MustCompile(` 253 LET users = [ 254 { 255 active: true, 256 married: true, 257 age: 31, 258 gender: "m" 259 }, 260 { 261 active: true, 262 married: false, 263 age: 25, 264 gender: "f" 265 }, 266 { 267 active: true, 268 married: false, 269 age: 36, 270 gender: "m" 271 }, 272 { 273 active: false, 274 married: true, 275 age: 69, 276 gender: "m" 277 }, 278 { 279 active: true, 280 married: true, 281 age: 45, 282 gender: "f" 283 } 284 ] 285 FOR i IN users 286 COLLECT gender = i.gender, age = i.age 287 RETURN {age, gender} 288 `) 289 290 for n := 0; n < b.N; n++ { 291 p.Run(context.Background()) 292 } 293 }