github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/builder/builder_z_def_tab_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 TestTable_Expr(t *testing.T) {
    13  	tUser := T("t_user",
    14  		Col("f_id").Field("ID").Type(uint64(0), ",autoincrement"),
    15  		Col("f_name").Field("Name").Type("", ",size=128,default=''"),
    16  	)
    17  
    18  	tUserRole := T("t_user_role",
    19  		Col("f_id").Field("ID").Type(uint64(0), ",autoincrement"),
    20  		Col("f_user_id").Field("UserID").Type(uint64(0), ""),
    21  	)
    22  
    23  	t.Run("ReplaceTable", func(t *testing.T) {
    24  		g.NewWithT(t).Expect(tUser.Expr("#.*")).
    25  			To(BeExpr("t_user.*"))
    26  	})
    27  	t.Run("ReplaceTableColByField", func(t *testing.T) {
    28  		g.NewWithT(t).Expect(tUser.Expr("#ID = #ID + 1")).
    29  			To(BeExpr("f_id = f_id + 1"))
    30  	})
    31  	t.Run("ReplaceTableColByFieldForFn", func(t *testing.T) {
    32  		g.NewWithT(t).Expect(tUser.Expr("COUNT(#ID)")).
    33  			To(BeExpr("COUNT(f_id)"))
    34  	})
    35  	t.Run("CouldHandleContext", func(t *testing.T) {
    36  		g.NewWithT(t).Expect(
    37  			Select(nil).
    38  				From(
    39  					tUser,
    40  					Where(AsCond(tUser.Expr("#ID > 1"))),
    41  					Join(tUserRole).
    42  						On(AsCond(
    43  							tUser.Expr("#ID = ?", tUserRole.Expr("#UserID")),
    44  						)),
    45  				),
    46  		).To(BeExpr(`
    47  SELECT * FROM t_user
    48  JOIN t_user_role ON t_user.f_id = t_user_role.f_user_id
    49  WHERE t_user.f_id > 1
    50  `,
    51  		))
    52  	})
    53  
    54  	// TODO table diff
    55  }