github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/builder/builder_z_util_test.go (about) 1 package builder_test 2 3 import ( 4 "context" 5 "testing" 6 7 g "github.com/onsi/gomega" 8 9 . "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder" 10 ) 11 12 func TestFieldValueFromStruct(t *testing.T) { 13 type User struct { 14 ID uint64 `db:"f_id"` 15 Name string `db:"f_name"` 16 Username string `db:"f_username"` 17 } 18 19 user := User{ID: 12345} 20 t.Run("FieldValueFromStruct", func(t *testing.T) { 21 g.NewWithT(t).Expect(FieldValueFromStruct(user, []string{})).To(g.HaveLen(0)) 22 values := FieldValueFromStruct(user, []string{"ID"}) 23 g.NewWithT(t).Expect(values).To(g.Equal(FieldValues{"ID": user.ID})) 24 }) 25 26 t.Run("FieldValueFromStructByNoneZero", func(t *testing.T) { 27 g.NewWithT(t).Expect(FieldValueFromStructByNoneZero(user)). 28 To(g.Equal(FieldValues{"ID": user.ID})) 29 30 g.NewWithT(t).Expect(FieldValueFromStructByNoneZero(user, "Username")). 31 To(g.Equal(FieldValues{"ID": user.ID, "Username": user.Username})) 32 }) 33 } 34 35 func TestGetColumnName(t *testing.T) { 36 g.NewWithT(t).Expect(GetColumnName("Text", "")).To(g.Equal("f_text")) 37 g.NewWithT(t).Expect(GetColumnName("Text", ",size=256")).To(g.Equal("f_text")) 38 g.NewWithT(t).Expect(GetColumnName("Text", "f_xxx")).To(g.Equal("f_xxx")) 39 g.NewWithT(t).Expect(GetColumnName("Text", "f_xxx,default=''")).To(g.Equal("f_xxx")) 40 } 41 42 func TestParseIndexDefine(t *testing.T) { 43 t.Run("IdxWithFieldNames", func(t *testing.T) { 44 i := ParseIndexDefine("index i_xxx/BTREE Name") 45 g.NewWithT(t).Expect(i).To(g.Equal(&IndexDefine{ 46 Kind: "index", 47 Name: "i_xxx", 48 Method: "BTREE", 49 IndexDef: IndexDef{FieldNames: []string{"Name"}}, 50 })) 51 }) 52 t.Run("PrimaryWithFieldNames", func(t *testing.T) { 53 i := ParseIndexDefine("primary ID Name") 54 g.NewWithT(t).Expect(i).To(g.Equal(&IndexDefine{ 55 Kind: "primary", 56 Name: "", 57 Method: "", 58 IndexDef: IndexDef{FieldNames: []string{"ID", "Name"}}, 59 })) 60 }) 61 t.Run("IndexWithExpr", func(t *testing.T) { 62 i := ParseIndexDefine("index i_xxx USING GIST (#TEST gist_trgm_ops)") 63 g.NewWithT(t).Expect(i).To(g.Equal(&IndexDefine{ 64 Kind: "index", 65 Name: "i_xxx", 66 Method: "", 67 IndexDef: IndexDef{Expr: "USING GIST (#TEST gist_trgm_ops)"}, 68 })) 69 }) 70 } 71 72 type User struct { 73 ID uint64 `db:"f_id"` 74 Name string `db:"f_name"` 75 Username string `db:"f_username"` 76 } 77 78 func (User) TableName() string { return "t_user" } 79 80 type OrgUser struct { 81 OrgID uint64 `db:"f_org_id"` 82 UserID uint64 `db:"f_user_id"` 83 } 84 85 func (OrgUser) TableName() string { return "t_org_user" } 86 87 type Org struct { 88 ID uint64 `db:"f_id"` 89 Name string `db:"f_name"` 90 } 91 92 func (Org) TableName() string { return "t_org" } 93 94 type OrgUserAll struct { 95 OrgUser 96 User User `json:"user"` 97 Org Org `json:"org"` 98 } 99 100 func TestColumnsByStruct(t *testing.T) { 101 t.Run("Simple", func(t *testing.T) { 102 q := ColumnsByStruct(&User{}).Ex(context.Background()).Query() 103 g.NewWithT(t).Expect(q). 104 To(g.Equal("t_user.f_id AS t_user__f_id, t_user.f_name AS t_user__f_name, t_user.f_username AS t_user__f_username")) 105 }) 106 t.Run("Joined", func(t *testing.T) { 107 q := ColumnsByStruct(&OrgUserAll{}).Ex(context.Background()).Query() 108 // for _, sub := range strings.Split(q, ", ") { 109 // t.Log(sub) 110 // } 111 // t.Log(q) 112 g.NewWithT(t).Expect(q).To( 113 g.Equal("t_org_user.f_org_id AS t_org_user__f_org_id, " + 114 "t_org_user.f_user_id AS t_org_user__f_user_id, " + 115 "t_user.f_id AS t_user__f_id, " + 116 "t_user.f_name AS t_user__f_name, " + 117 "t_user.f_username AS t_user__f_username, " + 118 "t_org.f_id AS t_org__f_id, " + 119 "t_org.f_name AS t_org__f_name", 120 ), 121 ) 122 }) 123 }