github.com/eden-framework/sqlx@v0.0.2/builder/def_table_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 TestTable_Expr(t *testing.T) {
    12  	tUser := T("t_user",
    13  		Col("f_id").Field("ID").Type(uint64(0), ",autoincrement"),
    14  		Col("f_name").Field("Name").Type("", ",size=128,default=''"),
    15  	)
    16  
    17  	tUserRole := T("t_user_role",
    18  		Col("f_id").Field("ID").Type(uint64(0), ",autoincrement"),
    19  		Col("f_user_id").Field("UserID").Type(uint64(0), ""),
    20  	)
    21  
    22  	t.Run("replace table", func(t *testing.T) {
    23  		gomega.NewWithT(t).Expect(tUser.Expr("#.*")).To(buidertestingutils.BeExpr("t_user.*"))
    24  	})
    25  	t.Run("replace table col by field", func(t *testing.T) {
    26  		gomega.NewWithT(t).Expect(tUser.Expr("#ID = #ID + 1")).To(buidertestingutils.BeExpr("f_id = f_id + 1"))
    27  	})
    28  	t.Run("replace table col by field for function", func(t *testing.T) {
    29  		gomega.NewWithT(t).Expect(tUser.Expr("COUNT(#ID)")).To(buidertestingutils.BeExpr("COUNT(f_id)"))
    30  	})
    31  	t.Run("could handle context", func(t *testing.T) {
    32  		gomega.NewWithT(t).Expect(
    33  			Select(nil).
    34  				From(
    35  					tUser,
    36  					Where(
    37  						AsCond(tUser.Expr("#ID > 1")),
    38  					),
    39  					Join(tUserRole).On(AsCond(tUser.Expr("#ID = ?", tUserRole.Expr("#UserID")))),
    40  				),
    41  		).To(buidertestingutils.BeExpr(`
    42  SELECT * FROM t_user
    43  JOIN t_user_role ON t_user.f_id = t_user_role.f_user_id
    44  WHERE t_user.f_id > 1
    45  `))
    46  	})
    47  }