github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_array_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 ) 11 12 func TestArrayOperator(t *testing.T) { 13 Convey("ALL", t, func() { 14 Convey("[1,2,3] ALL IN [1,2,3] should return true", func() { 15 c := compiler.New() 16 17 prog, err := c.Compile(` 18 RETURN [1,2,3] ALL IN [1,2,3] 19 `) 20 21 So(err, ShouldBeNil) 22 23 out, err := prog.Run(context.Background()) 24 25 So(err, ShouldBeNil) 26 So(string(out), ShouldEqual, `true`) 27 }) 28 29 Convey("[1,2,4] ALL IN [1,2,3] should return false", func() { 30 c := compiler.New() 31 32 prog, err := c.Compile(` 33 RETURN [1,2,4] ALL IN [1,2,3] 34 `) 35 36 So(err, ShouldBeNil) 37 38 out, err := prog.Run(context.Background()) 39 40 So(err, ShouldBeNil) 41 So(string(out), ShouldEqual, `false`) 42 }) 43 44 Convey("[4,5,6] ALL NOT IN [1,2,3] should return true", func() { 45 c := compiler.New() 46 47 prog, err := c.Compile(` 48 RETURN [4,5,6] ALL NOT IN [1,2,3] 49 `) 50 51 So(err, ShouldBeNil) 52 53 out, err := prog.Run(context.Background()) 54 55 So(err, ShouldBeNil) 56 So(string(out), ShouldEqual, `true`) 57 }) 58 59 Convey("[1,2,3] ALL > 0 should return true", func() { 60 c := compiler.New() 61 62 prog, err := c.Compile(` 63 RETURN [1,2,3] ALL > 0 64 `) 65 66 So(err, ShouldBeNil) 67 68 out, err := prog.Run(context.Background()) 69 70 So(err, ShouldBeNil) 71 So(string(out), ShouldEqual, `true`) 72 }) 73 74 Convey("[1,2,3] ALL > 2 should return false", func() { 75 c := compiler.New() 76 77 prog, err := c.Compile(` 78 RETURN [1,2,3] ALL > 2 79 `) 80 81 So(err, ShouldBeNil) 82 83 out, err := prog.Run(context.Background()) 84 85 So(err, ShouldBeNil) 86 So(string(out), ShouldEqual, `false`) 87 }) 88 89 Convey("[1,2,3] ALL >= 3 should return false", func() { 90 c := compiler.New() 91 92 prog, err := c.Compile(` 93 RETURN [1,2,3] ALL >= 3 94 `) 95 96 So(err, ShouldBeNil) 97 98 out, err := prog.Run(context.Background()) 99 100 So(err, ShouldBeNil) 101 So(string(out), ShouldEqual, `false`) 102 }) 103 104 Convey("['foo','bar'] ALL != 'moo' should return true", func() { 105 c := compiler.New() 106 107 prog, err := c.Compile(` 108 RETURN ['foo', 'bar'] ALL != 'moo' 109 `) 110 111 So(err, ShouldBeNil) 112 113 out, err := prog.Run(context.Background()) 114 115 So(err, ShouldBeNil) 116 So(string(out), ShouldEqual, `true`) 117 }) 118 }) 119 120 Convey("ANY", t, func() { 121 Convey("[1,2,3] ANY IN [1,2,3] should return true", func() { 122 c := compiler.New() 123 124 prog, err := c.Compile(` 125 RETURN [1,2,3] ANY IN [1,2,3] 126 `) 127 128 So(err, ShouldBeNil) 129 130 out, err := prog.Run(context.Background()) 131 132 So(err, ShouldBeNil) 133 So(string(out), ShouldEqual, `true`) 134 }) 135 136 Convey("[4,2,5] ANY IN [1,2,3] should return true", func() { 137 c := compiler.New() 138 139 prog, err := c.Compile(` 140 RETURN [4,2,5] ANY IN [1,2,3] 141 `) 142 143 So(err, ShouldBeNil) 144 145 out, err := prog.Run(context.Background()) 146 147 So(err, ShouldBeNil) 148 So(string(out), ShouldEqual, `true`) 149 }) 150 151 Convey("[4,5,6] ANY IN [1,2,3] should return false", func() { 152 c := compiler.New() 153 154 prog, err := c.Compile(` 155 RETURN [4,5,6] ANY IN [1,2,3] 156 `) 157 158 So(err, ShouldBeNil) 159 160 out, err := prog.Run(context.Background()) 161 162 So(err, ShouldBeNil) 163 So(string(out), ShouldEqual, `false`) 164 }) 165 166 Convey("[4,5,6] ANY NOT IN [1,2,3] should return true", func() { 167 c := compiler.New() 168 169 prog, err := c.Compile(` 170 RETURN [4,5,6] ANY NOT IN [1,2,3] 171 `) 172 173 So(err, ShouldBeNil) 174 175 out, err := prog.Run(context.Background()) 176 177 So(err, ShouldBeNil) 178 So(string(out), ShouldEqual, `true`) 179 }) 180 181 Convey("[1,2,3 ] ANY == 2 should return true", func() { 182 c := compiler.New() 183 184 prog, err := c.Compile(` 185 RETURN [1,2,3 ] ANY == 2 186 `) 187 188 So(err, ShouldBeNil) 189 190 out, err := prog.Run(context.Background()) 191 192 So(err, ShouldBeNil) 193 So(string(out), ShouldEqual, `true`) 194 }) 195 196 Convey("[1,2,3 ] ANY == 4 should return false", func() { 197 c := compiler.New() 198 199 prog, err := c.Compile(` 200 RETURN [1,2,3 ] ANY == 4 201 `) 202 203 So(err, ShouldBeNil) 204 205 out, err := prog.Run(context.Background()) 206 207 So(err, ShouldBeNil) 208 So(string(out), ShouldEqual, `false`) 209 }) 210 211 Convey("['foo','bar'] ANY == 'foo' should return true", func() { 212 c := compiler.New() 213 214 prog, err := c.Compile(` 215 RETURN ['foo', 'bar'] ANY == 'foo' 216 `) 217 218 So(err, ShouldBeNil) 219 220 out, err := prog.Run(context.Background()) 221 222 So(err, ShouldBeNil) 223 So(string(out), ShouldEqual, `true`) 224 }) 225 }) 226 227 Convey("NONE", t, func() { 228 Convey("[1,2,3] NONE IN [1,2,3] should return false", func() { 229 c := compiler.New() 230 231 prog, err := c.Compile(` 232 RETURN [1,2,3] NONE IN [1,2,3] 233 `) 234 235 So(err, ShouldBeNil) 236 237 out, err := prog.Run(context.Background()) 238 239 So(err, ShouldBeNil) 240 So(string(out), ShouldEqual, `false`) 241 }) 242 243 Convey("[4,2,5] NONE IN [1,2,3] should return false", func() { 244 c := compiler.New() 245 246 prog, err := c.Compile(` 247 RETURN [4,2,5] NONE IN [1,2,3] 248 `) 249 250 So(err, ShouldBeNil) 251 252 out, err := prog.Run(context.Background()) 253 254 So(err, ShouldBeNil) 255 So(string(out), ShouldEqual, `false`) 256 }) 257 258 Convey("[4,5,6] NONE IN [1,2,3] should return true", func() { 259 c := compiler.New() 260 261 prog, err := c.Compile(` 262 RETURN [4,5,6] NONE IN [1,2,3] 263 `) 264 265 So(err, ShouldBeNil) 266 267 out, err := prog.Run(context.Background()) 268 269 So(err, ShouldBeNil) 270 So(string(out), ShouldEqual, `true`) 271 }) 272 273 Convey("[4,5,6] NONE NOT IN [1,2,3] should return false", func() { 274 c := compiler.New() 275 276 prog, err := c.Compile(` 277 RETURN [4,5,6] NONE NOT IN [1,2,3] 278 `) 279 280 So(err, ShouldBeNil) 281 282 out, err := prog.Run(context.Background()) 283 284 So(err, ShouldBeNil) 285 So(string(out), ShouldEqual, `false`) 286 }) 287 288 Convey("[1,2,3] NONE > 99 should return false", func() { 289 c := compiler.New() 290 291 prog, err := c.Compile(` 292 RETURN [1,2,3] NONE > 99 293 `) 294 295 So(err, ShouldBeNil) 296 297 out, err := prog.Run(context.Background()) 298 299 So(err, ShouldBeNil) 300 So(string(out), ShouldEqual, `true`) 301 }) 302 303 Convey("[1,2,3] NONE < 99 should return false", func() { 304 c := compiler.New() 305 306 prog, err := c.Compile(` 307 RETURN [1,2,3] NONE < 99 308 `) 309 310 So(err, ShouldBeNil) 311 312 out, err := prog.Run(context.Background()) 313 314 So(err, ShouldBeNil) 315 So(string(out), ShouldEqual, `false`) 316 }) 317 318 Convey("['foo','bar'] NONE == 'foo' should return false", func() { 319 c := compiler.New() 320 321 prog, err := c.Compile(` 322 RETURN ['foo','bar'] NONE == 'foo' 323 `) 324 325 So(err, ShouldBeNil) 326 327 out, err := prog.Run(context.Background()) 328 329 So(err, ShouldBeNil) 330 So(string(out), ShouldEqual, `false`) 331 }) 332 }) 333 } 334 335 func BenchmarkArrayOperatorALL(b *testing.B) { 336 p := compiler.New().MustCompile(` 337 RETURN [1,2,3] ALL IN [1,2,3] 338 `) 339 340 for n := 0; n < b.N; n++ { 341 p.Run(context.Background()) 342 } 343 } 344 345 func BenchmarkArrayOperatorALL2(b *testing.B) { 346 p := compiler.New().MustCompile(` 347 RETURN [1,2,4] ALL IN [1,2,3] 348 `) 349 350 for n := 0; n < b.N; n++ { 351 p.Run(context.Background()) 352 } 353 } 354 355 func BenchmarkArrayOperatorANY(b *testing.B) { 356 p := compiler.New().MustCompile(` 357 RETURN [1,2,3] ANY IN [1,2,3] 358 `) 359 360 for n := 0; n < b.N; n++ { 361 p.Run(context.Background()) 362 } 363 } 364 365 func BenchmarkArrayOperatorANY2(b *testing.B) { 366 p := compiler.New().MustCompile(` 367 RETURN [4,5,6] ANY IN [1,2,3] 368 `) 369 370 for n := 0; n < b.N; n++ { 371 p.Run(context.Background()) 372 } 373 } 374 375 func BenchmarkArrayOperatorANY3(b *testing.B) { 376 p := compiler.New().MustCompile(` 377 RETURN [4,5,6] ANY NOT IN [1,2,3] 378 `) 379 380 for n := 0; n < b.N; n++ { 381 p.Run(context.Background()) 382 } 383 } 384 385 func BenchmarkArrayOperatorANY4(b *testing.B) { 386 p := compiler.New().MustCompile(` 387 RETURN [1,2,3 ] ANY == 2 388 `) 389 390 for n := 0; n < b.N; n++ { 391 p.Run(context.Background()) 392 } 393 } 394 395 func BenchmarkArrayOperatorNONE(b *testing.B) { 396 p := compiler.New().MustCompile(` 397 RETURN [1,2,3] NONE IN [1,2,3] 398 `) 399 400 for n := 0; n < b.N; n++ { 401 p.Run(context.Background()) 402 } 403 } 404 405 func BenchmarkArrayOperatorNONE2(b *testing.B) { 406 p := compiler.New().MustCompile(` 407 RETURN [4,5,6] NONE IN [1,2,3] 408 `) 409 410 for n := 0; n < b.N; n++ { 411 p.Run(context.Background()) 412 } 413 } 414 415 func BenchmarkArrayOperatorNONE3(b *testing.B) { 416 p := compiler.New().MustCompile(` 417 RETURN [4,5,6] NONE NOT IN [1,2,3] 418 `) 419 420 for n := 0; n < b.N; n++ { 421 p.Run(context.Background()) 422 } 423 } 424 425 func BenchmarkArrayOperatorNONE4(b *testing.B) { 426 p := compiler.New().MustCompile(` 427 RETURN [1,2,3] NONE < 99 428 `) 429 430 for n := 0; n < b.N; n++ { 431 p.Run(context.Background()) 432 } 433 }