github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_collect_into_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 TestCollectInto(t *testing.T) {
    14  	Convey("Should create default projection", 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 INTO genders
    52  				RETURN {
    53  					gender,
    54  					values: genders
    55  				}
    56  		`)
    57  
    58  		So(err, ShouldBeNil)
    59  		So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
    60  
    61  		out, err := prog.Run(context.Background())
    62  
    63  		So(err, ShouldBeNil)
    64  		So(string(out), ShouldEqual, `[{"gender":"f","values":[{"i":{"active":true,"age":25,"gender":"f","married":false}},{"i":{"active":true,"age":45,"gender":"f","married":true}}]},{"gender":"m","values":[{"i":{"active":true,"age":31,"gender":"m","married":true}},{"i":{"active":true,"age":36,"gender":"m","married":false}},{"i":{"active":false,"age":69,"gender":"m","married":true}}]}]`)
    65  	})
    66  
    67  	Convey("Should create custom projection", t, func() {
    68  		c := compiler.New()
    69  
    70  		prog, err := c.Compile(`
    71  			LET users = [
    72  				{
    73  					active: true,
    74  					age: 31,
    75  					gender: "m",
    76  					married: true
    77  				},
    78  				{
    79  					active: true,
    80  					age: 25,
    81  					gender: "f",
    82  					married: false
    83  				},
    84  				{
    85  					active: true,
    86  					age: 36,
    87  					gender: "m",
    88  					married: false
    89  				},
    90  				{
    91  					active: false,
    92  					age: 69,
    93  					gender: "m",
    94  					married: true
    95  				},
    96  				{
    97  					active: true,
    98  					age: 45,
    99  					gender: "f",
   100  					married: true
   101  				}
   102  			]
   103  			FOR i IN users
   104  				COLLECT gender = i.gender INTO genders = { active: i.active }
   105  				RETURN {
   106  					gender,
   107  					values: genders
   108  				}
   109  		`)
   110  
   111  		So(err, ShouldBeNil)
   112  		So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
   113  
   114  		out, err := prog.Run(context.Background())
   115  
   116  		So(err, ShouldBeNil)
   117  		So(string(out), ShouldEqual, `[{"gender":"f","values":[{"active":true},{"active":true}]},{"gender":"m","values":[{"active":true},{"active":true},{"active":false}]}]`)
   118  	})
   119  
   120  	Convey("Should create custom projection grouped by multiple keys", t, func() {
   121  		c := compiler.New()
   122  
   123  		prog, err := c.Compile(`
   124  			LET users = [
   125  				{
   126  					active: true,
   127  					age: 31,
   128  					gender: "m",
   129  					married: true
   130  				},
   131  				{
   132  					active: true,
   133  					age: 25,
   134  					gender: "f",
   135  					married: false
   136  				},
   137  				{
   138  					active: true,
   139  					age: 36,
   140  					gender: "m",
   141  					married: false
   142  				},
   143  				{
   144  					active: false,
   145  					age: 69,
   146  					gender: "m",
   147  					married: true
   148  				},
   149  				{
   150  					active: true,
   151  					age: 45,
   152  					gender: "f",
   153  					married: true
   154  				}
   155  			]
   156  			FOR i IN users
   157  				COLLECT gender = i.gender, age = i.age INTO genders = { active: i.active }
   158  				RETURN {
   159  					age,
   160  					gender,
   161  					values: genders
   162  				}
   163  		`)
   164  
   165  		So(err, ShouldBeNil)
   166  		So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
   167  
   168  		out, err := prog.Run(context.Background())
   169  
   170  		So(err, ShouldBeNil)
   171  		So(string(out), ShouldEqual, `[{"age":25,"gender":"f","values":[{"active":true}]},{"age":45,"gender":"f","values":[{"active":true}]},{"age":31,"gender":"m","values":[{"active":true}]},{"age":36,"gender":"m","values":[{"active":true}]},{"age":69,"gender":"m","values":[{"active":false}]}]`)
   172  	})
   173  }
   174  
   175  func BenchmarkCollectInto(b *testing.B) {
   176  	p := compiler.New().MustCompile(`
   177  			LET users = [
   178  				{
   179  					active: true,
   180  					age: 31,
   181  					gender: "m",
   182  					married: true
   183  				},
   184  				{
   185  					active: true,
   186  					age: 25,
   187  					gender: "f",
   188  					married: false
   189  				},
   190  				{
   191  					active: true,
   192  					age: 36,
   193  					gender: "m",
   194  					married: false
   195  				},
   196  				{
   197  					active: false,
   198  					age: 69,
   199  					gender: "m",
   200  					married: true
   201  				},
   202  				{
   203  					active: true,
   204  					age: 45,
   205  					gender: "f",
   206  					married: true
   207  				}
   208  			]
   209  			FOR i IN users
   210  				COLLECT gender = i.gender INTO genders
   211  				RETURN {
   212  					gender,
   213  					values: genders
   214  				}
   215  		`)
   216  
   217  	for n := 0; n < b.N; n++ {
   218  		p.Run(context.Background())
   219  	}
   220  }
   221  
   222  func BenchmarkCollectInto2(b *testing.B) {
   223  	p := compiler.New().MustCompile(`
   224  			LET users = [
   225  				{
   226  					active: true,
   227  					age: 31,
   228  					gender: "m",
   229  					married: true
   230  				},
   231  				{
   232  					active: true,
   233  					age: 25,
   234  					gender: "f",
   235  					married: false
   236  				},
   237  				{
   238  					active: true,
   239  					age: 36,
   240  					gender: "m",
   241  					married: false
   242  				},
   243  				{
   244  					active: false,
   245  					age: 69,
   246  					gender: "m",
   247  					married: true
   248  				},
   249  				{
   250  					active: true,
   251  					age: 45,
   252  					gender: "f",
   253  					married: true
   254  				}
   255  			]
   256  			FOR i IN users
   257  				COLLECT gender = i.gender INTO genders = { active: i.active }
   258  				RETURN {
   259  					gender,
   260  					values: genders
   261  				}
   262  		`)
   263  
   264  	for n := 0; n < b.N; n++ {
   265  		p.Run(context.Background())
   266  	}
   267  }