gitee.com/eden-framework/sqlx@v0.0.3/builder/addition_join_test.go (about)

     1  package builder_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "gitee.com/eden-framework/sqlx/builder"
     7  	. "gitee.com/eden-framework/sqlx/builder/buidertestingutils"
     8  	"github.com/onsi/gomega"
     9  )
    10  
    11  func TestJoin(t *testing.T) {
    12  	tUser := T("t_user",
    13  		Col("f_id").Type(uint64(0), ",autoincrement"),
    14  		Col("f_name").Type("", ",size=128,default=''"),
    15  		Col("f_org_id").Type("", ",size=128,default=''"),
    16  	)
    17  
    18  	tOrg := T("t_org",
    19  		Col("f_org_id").Type(uint64(0), ",autoincrement"),
    20  		Col("f_org_name").Type("", ",size=128,default=''"),
    21  	)
    22  
    23  	t.Run("JOIN ON", func(t *testing.T) {
    24  		gomega.NewWithT(t).Expect(
    25  			Select(MultiWith(", ",
    26  				Alias(tUser.Col("f_id"), "f_id"),
    27  				Alias(tUser.Col("f_name"), "f_name"),
    28  				Alias(tUser.Col("f_org_id"), "f_org_id"),
    29  				Alias(tOrg.Col("f_org_name"), "f_org_name"),
    30  			)).
    31  				From(
    32  					tUser,
    33  					Join(tOrg).On(tUser.Col("f_org_id").Eq(tOrg.Col("f_org_id"))),
    34  				)).
    35  			To(BeExpr(
    36  				`
    37  SELECT (t_user.f_id) AS f_id, (t_user.f_name) AS f_name, (t_user.f_org_id) AS f_org_id, (t_org.f_org_name) AS f_org_name FROM t_user
    38  JOIN t_org ON t_user.f_org_id = t_org.f_org_id
    39  `,
    40  			))
    41  	})
    42  	t.Run("JOIN USING", func(t *testing.T) {
    43  		gomega.NewWithT(t).Expect(
    44  			Select(nil).
    45  				From(
    46  					tUser,
    47  					Join(tOrg).Using(tUser.Col("f_org_id")),
    48  				),
    49  		).To(BeExpr(
    50  			`
    51  SELECT * FROM t_user
    52  JOIN t_org USING (f_org_id)
    53  `,
    54  		))
    55  	})
    56  }