github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/builder/builder_z_addition_test.go (about)

     1  package builder_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	g "github.com/onsi/gomega"
     7  
     8  	. "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
     9  	. "github.com/machinefi/w3bstream/pkg/depends/testutil/buildertestutil"
    10  )
    11  
    12  func TestGroupBy(t *testing.T) {
    13  	tbl := T("T")
    14  	cola := Col("f_a")
    15  	colb := Col("F_b")
    16  
    17  	t.Run("SelectGroupBy", func(t *testing.T) {
    18  		g.NewWithT(t).Expect(
    19  			Select(nil).From(
    20  				tbl,
    21  				Where(Col("f_A").Eq(1)),
    22  				GroupBy(Col("F_a")).
    23  					Having(Col("F_A").Eq(1)),
    24  			),
    25  		).To(BeExpr(
    26  			`
    27  SELECT * FROM T
    28  WHERE f_a = ?
    29  GROUP BY f_a HAVING f_a = ?
    30  `,
    31  			1, 1,
    32  		))
    33  	})
    34  
    35  	t.Run("SelectMultiGroupByAndOrder", func(t *testing.T) {
    36  		g.NewWithT(t).Expect(
    37  			Select(nil).
    38  				From(
    39  					tbl,
    40  					Where(cola.Eq(1)),
    41  					GroupBy(AscOrder(cola), DescOrder(colb)),
    42  				),
    43  		).To(BeExpr(
    44  			`
    45  SELECT * FROM T
    46  WHERE f_a = ?
    47  GROUP BY (f_a) ASC,(f_b) DESC
    48  `,
    49  			1,
    50  		))
    51  	})
    52  }
    53  
    54  func TestJoin(t *testing.T) {
    55  	tUser := T("t_user",
    56  		Col("f_id").Type(uint64(0), ",autoincrement"),
    57  		Col("f_name").Type("", ",size=128,default=''"),
    58  		Col("f_org_id").Type("", ",size=128,default=''"),
    59  	)
    60  	tOrg := T("t_org",
    61  		Col("f_id").Type(uint64(0), ",autoincrement"),
    62  		Col("f_name").Type("", ",size=128,default=''"),
    63  	)
    64  
    65  	t.Run("JoinOn", func(t *testing.T) {
    66  		expr := Select(
    67  			MultiWith(
    68  				", ",
    69  				Alias(tUser.Col("f_id"), "f_user_id"),
    70  				Alias(tUser.Col("f_name"), "f_user_name"),
    71  				Alias(tOrg.Col("f_id"), "f_org_id"),
    72  				Alias(tOrg.Col("f_name"), "f_org_name"),
    73  			),
    74  		).From(
    75  			tUser,
    76  			Join(Alias(tOrg, "t_org")).
    77  				On(tUser.Col("f_org_id").Eq(tOrg.Col("f_id"))),
    78  		)
    79  		// t.Log(expr.Ex(context.Background()).Query())
    80  		g.NewWithT(t).Expect(expr).To(BeExpr(`
    81  SELECT t_user.f_id AS f_user_id, t_user.f_name AS f_user_name, t_org.f_id AS f_org_id, t_org.f_name AS f_org_name FROM t_user
    82  JOIN t_org AS t_org ON t_user.f_org_id = t_org.f_id
    83  `))
    84  	})
    85  
    86  	t.Run("JoinUsing", func(t *testing.T) {
    87  		g.NewWithT(t).Expect(
    88  			Select(nil).
    89  				From(
    90  					tUser,
    91  					Join(tOrg).Using(tUser.Col("f_org_id")),
    92  				),
    93  		).To(BeExpr(`
    94  SELECT * FROM t_user
    95  JOIN t_org USING (f_org_id)
    96  `,
    97  		))
    98  	})
    99  }
   100  
   101  func TestLimit(t *testing.T) {
   102  	tbl := T("T")
   103  	t.Run("SelectLimit", func(t *testing.T) {
   104  		g.NewWithT(t).Expect(
   105  			Select(nil).
   106  				From(
   107  					tbl,
   108  					Where(Col("F_a").Eq(1)),
   109  					Limit(1),
   110  				),
   111  		).To(BeExpr(`
   112  SELECT * FROM T
   113  WHERE f_a = ?
   114  LIMIT 1
   115  `, 1,
   116  		))
   117  	})
   118  	t.Run("SelectWithoutLimit", func(t *testing.T) {
   119  		g.NewWithT(t).Expect(
   120  			Select(nil).
   121  				From(
   122  					tbl,
   123  					Where(Col("F_a").Eq(1)),
   124  					Limit(-1),
   125  				),
   126  		).To(BeExpr(`
   127  SELECT * FROM T
   128  WHERE f_a = ?
   129  `, 1,
   130  		))
   131  	})
   132  	t.Run("SelectLimitAndOffset", func(t *testing.T) {
   133  		g.NewWithT(t).Expect(
   134  			Select(nil).
   135  				From(
   136  					tbl,
   137  					Where(Col("F_a").Eq(1)),
   138  					Limit(1).Offset(200),
   139  				),
   140  		).To(BeExpr(`
   141  SELECT * FROM T
   142  WHERE f_a = ?
   143  LIMIT 1 OFFSET 200
   144  `, 1,
   145  		))
   146  	})
   147  }
   148  
   149  func TestOrderBy(t *testing.T) {
   150  	tbl := T("T")
   151  	t.Run("SelectOrder", func(t *testing.T) {
   152  		g.NewWithT(t).Expect(
   153  			Select(nil).
   154  				From(
   155  					tbl,
   156  					OrderBy(
   157  						AscOrder(Col("F_a")),
   158  						DescOrder(Col("F_b")),
   159  					),
   160  					Where(Col("F_a").Eq(1)),
   161  				),
   162  		).To(BeExpr(`
   163  SELECT * FROM T
   164  WHERE f_a = ?
   165  ORDER BY (f_a) ASC,(f_b) DESC
   166  `, 1,
   167  		))
   168  	})
   169  }