github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/sqlbuilder/addition_limit_test.go (about)

     1  package sqlbuilder_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/octohelm/storage/internal/testutil"
     7  	. "github.com/octohelm/storage/pkg/sqlbuilder"
     8  )
     9  
    10  func TestLimit(t *testing.T) {
    11  	table := T("T")
    12  
    13  	t.Run("select limit", func(t *testing.T) {
    14  		testutil.ShouldBeExpr(t,
    15  			Select(nil).
    16  				From(
    17  					table,
    18  					Where(
    19  						TypedCol[int]("F_a").V(Eq(1)),
    20  					),
    21  					Limit(1),
    22  				), `
    23  SELECT * FROM T
    24  WHERE f_a = ?
    25  LIMIT 1
    26  `, 1)
    27  	})
    28  	t.Run("select without limit", func(t *testing.T) {
    29  		testutil.ShouldBeExpr(t,
    30  			Select(nil).
    31  				From(
    32  					table,
    33  					Where(
    34  						TypedCol[int]("F_a").V(Eq(1)),
    35  					),
    36  					Limit(-1),
    37  				), `
    38  SELECT * FROM T
    39  WHERE f_a = ?
    40  `, 1,
    41  		)
    42  	})
    43  
    44  	t.Run("select limit and offset", func(t *testing.T) {
    45  		testutil.ShouldBeExpr(t,
    46  			Select(nil).
    47  				From(
    48  					table,
    49  					Where(
    50  						TypedCol[int]("F_a").V(Eq(1)),
    51  					),
    52  					Limit(10).Offset(200),
    53  				),
    54  			`
    55  SELECT * FROM T
    56  WHERE f_a = ?
    57  LIMIT 10 OFFSET 200
    58  `,
    59  			1,
    60  		)
    61  	})
    62  }