github.com/eden-framework/sqlx@v0.0.2/builder/addition_group_by_test.go (about)

     1  package builder_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/eden-framework/sqlx/builder"
     7  	. "github.com/eden-framework/sqlx/builder/buidertestingutils"
     8  	"github.com/onsi/gomega"
     9  )
    10  
    11  func TestGroupBy(t *testing.T) {
    12  	table := T("T")
    13  
    14  	t.Run("select group by", func(t *testing.T) {
    15  		gomega.NewWithT(t).Expect(
    16  			Select(nil).
    17  				From(
    18  					table,
    19  					Where(Col("F_a").Eq(1)),
    20  					GroupBy(Col("F_a")).
    21  						Having(Col("F_a").Eq(1)),
    22  				),
    23  		).To(BeExpr(
    24  			`
    25  SELECT * FROM T
    26  WHERE f_a = ?
    27  GROUP BY f_a HAVING f_a = ?
    28  `,
    29  			1, 1,
    30  		))
    31  	})
    32  
    33  	t.Run("select desc group by", func(t *testing.T) {
    34  		gomega.NewWithT(t).Expect(
    35  			Select(nil).
    36  				From(
    37  					table,
    38  					Where(Col("F_a").Eq(1)),
    39  					GroupBy(AscOrder(Col("F_a")), DescOrder(Col("F_b"))),
    40  				),
    41  		).To(BeExpr(`
    42  SELECT * FROM T
    43  WHERE f_a = ?
    44  GROUP BY (f_a) ASC,(f_b) DESC
    45  `,
    46  			1,
    47  		))
    48  	})
    49  	t.Run("select multi group by", func(t *testing.T) {
    50  		gomega.NewWithT(t).Expect(
    51  			Select(nil).
    52  				From(
    53  					table,
    54  					Where(Col("F_a").Eq(1)),
    55  					GroupBy(AscOrder(Col("F_a")), DescOrder(Col("F_b"))),
    56  				),
    57  		).To(BeExpr(
    58  			`
    59  SELECT * FROM T
    60  WHERE f_a = ?
    61  GROUP BY (f_a) ASC,(f_b) DESC
    62  `,
    63  			1,
    64  		))
    65  	})
    66  }