github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/sqlbuilder/addition_group_by_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 TestGroupBy(t *testing.T) {
    11  	table := T("T")
    12  
    13  	t.Run("select group by", func(t *testing.T) {
    14  		testutil.ShouldBeExpr(t,
    15  			Select(nil).
    16  				From(
    17  					table,
    18  					Where(TypedCol[int]("F_a").V(Eq(1))),
    19  					GroupBy(Col("F_a")).
    20  						Having(TypedCol[int]("F_a").V(Eq(1))),
    21  				),
    22  			`SELECT * FROM T
    23  WHERE f_a = ?
    24  GROUP BY f_a HAVING f_a = ?
    25  `,
    26  			1, 1,
    27  		)
    28  	})
    29  
    30  	t.Run("select desc group by", func(t *testing.T) {
    31  		testutil.ShouldBeExpr(t,
    32  			Select(nil).
    33  				From(
    34  					table,
    35  					Where(TypedCol[int]("F_a").V(Eq(1))),
    36  					GroupBy(AscOrder(Col("F_a")), DescOrder(Col("F_b"))),
    37  				),
    38  			`
    39  SELECT * FROM T
    40  WHERE f_a = ?
    41  GROUP BY (f_a) ASC,(f_b) DESC
    42  `,
    43  			1,
    44  		)
    45  	})
    46  	t.Run("select multi group by", func(t *testing.T) {
    47  		testutil.ShouldBeExpr(t,
    48  			Select(nil).
    49  				From(
    50  					table,
    51  					Where(TypedCol[int]("F_a").V(Eq(1))),
    52  					GroupBy(AscOrder(Col("F_a")), DescOrder(Col("F_b"))),
    53  				),
    54  
    55  			`
    56  SELECT * FROM T
    57  WHERE f_a = ?
    58  GROUP BY (f_a) ASC,(f_b) DESC
    59  `,
    60  			1,
    61  		)
    62  	})
    63  }