github.com/MontFerret/ferret@v0.18.0/pkg/compiler/compiler_limit_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/core"
    11  	"github.com/MontFerret/ferret/pkg/runtime/values"
    12  )
    13  
    14  func TestForLimit(t *testing.T) {
    15  	Convey("Should compile query with LIMIT 2", t, func() {
    16  		c := compiler.New()
    17  
    18  		p, err := c.Compile(`
    19  			FOR i IN [ 1, 2, 3, 4, 1, 3 ]
    20  				LIMIT 2
    21  				RETURN i
    22  		`)
    23  
    24  		So(err, ShouldBeNil)
    25  
    26  		out, err := p.Run(context.Background())
    27  
    28  		So(err, ShouldBeNil)
    29  
    30  		So(string(out), ShouldEqual, `[1,2]`)
    31  	})
    32  
    33  	Convey("Should compile query with LIMIT 2, 2", t, func() {
    34  		c := compiler.New()
    35  
    36  		// 4 is offset
    37  		// 2 is count
    38  		p, err := c.Compile(`
    39  			FOR i IN [ 1,2,3,4,5,6,7,8 ]
    40  				LIMIT 4, 2
    41  				RETURN i
    42  		`)
    43  
    44  		So(err, ShouldBeNil)
    45  
    46  		out, err := p.Run(context.Background())
    47  
    48  		So(err, ShouldBeNil)
    49  
    50  		So(string(out), ShouldEqual, `[5,6]`)
    51  	})
    52  
    53  	Convey("Should define variables and call functions", t, func() {
    54  		c := compiler.New()
    55  		counter := 0
    56  		c.RegisterFunction("TEST", func(ctx context.Context, args ...core.Value) (core.Value, error) {
    57  			counter++
    58  
    59  			So(args[0], ShouldEqual, "foo")
    60  
    61  			return values.None, nil
    62  		})
    63  
    64  		p, err := c.Compile(`
    65  			FOR i IN [ 1,2,3,4,5,6,7,8 ]
    66  				LET x = "foo"
    67  				TEST(x)
    68  				LIMIT 2
    69  				RETURN i
    70  		`)
    71  
    72  		So(err, ShouldBeNil)
    73  
    74  		out, err := p.Run(context.Background())
    75  
    76  		So(err, ShouldBeNil)
    77  		So(counter, ShouldEqual, 2)
    78  		So(string(out), ShouldEqual, `[1,2]`)
    79  	})
    80  
    81  	Convey("Should be able to reuse values from a source", t, func() {
    82  		c := compiler.New()
    83  
    84  		p, err := c.Compile(`
    85  			FOR i IN [ 1,2,3,4,5,6,7,8 ]
    86  				LET x = i
    87  				LIMIT 2
    88  				RETURN i*x
    89  		`)
    90  
    91  		So(err, ShouldBeNil)
    92  
    93  		out, err := p.Run(context.Background())
    94  
    95  		So(err, ShouldBeNil)
    96  		So(string(out), ShouldEqual, `[1,4]`)
    97  	})
    98  
    99  	Convey("Should be able to use variable", t, func() {
   100  		c := compiler.New()
   101  
   102  		p, err := c.Compile(`
   103  			LET li = 2
   104  			FOR i IN [ 1,2,3,4,5,6,7,8 ]
   105  				LIMIT li
   106  				RETURN i
   107  		`)
   108  
   109  		So(err, ShouldBeNil)
   110  
   111  		out, err := p.Run(context.Background())
   112  
   113  		So(err, ShouldBeNil)
   114  		So(string(out), ShouldEqual, `[1,2]`)
   115  	})
   116  
   117  	Convey("Should be able to use function call", t, func() {
   118  		c := compiler.New()
   119  		c.RegisterFunction("TEST", func(ctx context.Context, args ...core.Value) (core.Value, error) {
   120  			return values.NewInt(2), nil
   121  		})
   122  
   123  		p, err := c.Compile(`
   124  			FOR i IN [ 1,2,3,4,5,6,7,8 ]
   125  				LIMIT TEST()
   126  				RETURN i
   127  		`)
   128  
   129  		So(err, ShouldBeNil)
   130  
   131  		out, err := p.Run(context.Background())
   132  
   133  		So(err, ShouldBeNil)
   134  		So(string(out), ShouldEqual, `[1,2]`)
   135  	})
   136  
   137  	Convey("Should be able to use member expression (object)", t, func() {
   138  		c := compiler.New()
   139  
   140  		p, err := c.Compile(`
   141  			LET o = {
   142  				limit: 2
   143  			}
   144  			FOR i IN [ 1,2,3,4,5,6,7,8 ]
   145  				LIMIT o.limit
   146  				RETURN i
   147  		`)
   148  
   149  		So(err, ShouldBeNil)
   150  
   151  		out, err := p.Run(context.Background())
   152  
   153  		So(err, ShouldBeNil)
   154  		So(string(out), ShouldEqual, `[1,2]`)
   155  	})
   156  
   157  	Convey("Should be able to use member expression (array)", t, func() {
   158  		c := compiler.New()
   159  
   160  		p, err := c.Compile(`
   161  			LET o = [1,2]
   162  
   163  			FOR i IN [ 1,2,3,4,5,6,7,8 ]
   164  				LIMIT o[1]
   165  				RETURN i
   166  		`)
   167  
   168  		So(err, ShouldBeNil)
   169  
   170  		out, err := p.Run(context.Background())
   171  
   172  		So(err, ShouldBeNil)
   173  		So(string(out), ShouldEqual, `[1,2]`)
   174  	})
   175  }