github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/sqlbuilder/stmt_select_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 TestSelect(t *testing.T) {
    11  	table := T("T")
    12  
    13  	t.Run("select with modifier", func(t *testing.T) {
    14  		testutil.ShouldBeExpr(t,
    15  			Select(nil, Expr("DISTINCT")).
    16  				From(
    17  					table,
    18  					Where(
    19  						TypedCol[int]("F_a").V(Eq(1)),
    20  					),
    21  				),
    22  			`
    23  SELECT DISTINCT * FROM T
    24  WHERE f_a = ?`, 1)
    25  	})
    26  	t.Run("select simple", func(t *testing.T) {
    27  		testutil.ShouldBeExpr(t,
    28  			Select(nil).
    29  				From(
    30  					table,
    31  					Where(
    32  						TypedCol[int]("F_a").V(Eq(1)),
    33  					),
    34  					Comment("comment"),
    35  				),
    36  			`
    37  SELECT * FROM T
    38  WHERE f_a = ?
    39  /* comment */
    40  `, 1,
    41  		)
    42  	})
    43  	t.Run("select with target", func(t *testing.T) {
    44  		testutil.ShouldBeExpr(t,
    45  			Select(Col("F_a")).
    46  				From(table,
    47  					Where(
    48  						TypedCol[int]("F_a").V(Eq(1)),
    49  					),
    50  				),
    51  			`
    52  SELECT f_a FROM T
    53  WHERE f_a = ?`, 1)
    54  	})
    55  
    56  	t.Run("select for update", func(t *testing.T) {
    57  		testutil.ShouldBeExpr(t,
    58  			Select(nil).From(
    59  				table,
    60  				Where(TypedCol[int]("F_a").V(Eq(1))),
    61  				ForUpdate(),
    62  			),
    63  			`
    64  SELECT * FROM T
    65  WHERE f_a = ?
    66  FOR UPDATE
    67  `,
    68  			1,
    69  		)
    70  	})
    71  }