github.com/kunlun-qilian/sqlx/v3@v3.0.0/builder/utils__test.go (about) 1 package builder 2 3 import ( 4 "context" 5 "strings" 6 "testing" 7 8 "github.com/onsi/gomega" 9 ) 10 11 func TestValueMap(t *testing.T) { 12 type User struct { 13 ID uint64 `db:"f_id"` 14 Name string `db:"f_name"` 15 Username string `db:"f_username"` 16 } 17 18 user := User{ 19 ID: 123123213, 20 } 21 22 t.Run("#FieldValuesFromStructBy", func(t *testing.T) { 23 gomega.NewWithT(t).Expect(FieldValuesFromStructBy(user, []string{})).To(gomega.HaveLen(0)) 24 25 values := FieldValuesFromStructBy(user, []string{"ID"}) 26 27 gomega.NewWithT(t).Expect(values).To(gomega.Equal(FieldValues{ 28 "ID": user.ID, 29 })) 30 }) 31 32 t.Run("#FieldValuesFromStructBy", func(t *testing.T) { 33 gomega.NewWithT(t).Expect(FieldValuesFromStructByNonZero(user)). 34 To(gomega.Equal(FieldValues{ 35 "ID": user.ID, 36 })) 37 38 gomega.NewWithT(t).Expect(FieldValuesFromStructByNonZero(user, "Username")). 39 To(gomega.Equal(FieldValues{ 40 "ID": user.ID, 41 "Username": user.Username, 42 })) 43 }) 44 45 t.Run("#GetColumnName", func(t *testing.T) { 46 gomega.NewWithT(t).Expect(GetColumnName("Text", "")).To(gomega.Equal("f_text")) 47 gomega.NewWithT(t).Expect(GetColumnName("Text", ",size=256")).To(gomega.Equal("f_text")) 48 gomega.NewWithT(t).Expect(GetColumnName("Text", "f_text2")).To(gomega.Equal("f_text2")) 49 gomega.NewWithT(t).Expect(GetColumnName("Text", "f_text2,default=''")).To(gomega.Equal("f_text2")) 50 }) 51 } 52 53 func TestParseDef(t *testing.T) { 54 t.Run("index with Field Names", func(t *testing.T) { 55 56 i := ParseIndexDefine("index i_xxx/BTREE Name") 57 58 gomega.NewWithT(t).Expect(i).To(gomega.Equal(&IndexDefine{ 59 Kind: "index", 60 Name: "i_xxx", 61 Method: "BTREE", 62 IndexDef: IndexDef{ 63 FieldNames: []string{"Name"}, 64 }, 65 })) 66 }) 67 68 t.Run("primary with Field Names", func(t *testing.T) { 69 70 i := ParseIndexDefine("primary ID Name") 71 72 gomega.NewWithT(t).Expect(i).To(gomega.Equal(&IndexDefine{ 73 Kind: "primary", 74 IndexDef: IndexDef{ 75 FieldNames: []string{"ID", "Name"}, 76 }, 77 })) 78 }) 79 80 t.Run("index with expr", func(t *testing.T) { 81 i := ParseIndexDefine("index i_xxx USING GIST (#TEST gist_trgm_ops)") 82 83 gomega.NewWithT(t).Expect(i).To(gomega.Equal(&IndexDefine{ 84 Kind: "index", 85 Name: "i_xxx", 86 IndexDef: IndexDef{ 87 Expr: "USING GIST (#TEST gist_trgm_ops)", 88 }, 89 })) 90 }) 91 92 t.Run("partition with Field Names", func(t *testing.T) { 93 94 i := ParseIndexDefine("partition list Name") 95 96 gomega.NewWithT(t).Expect(i).To(gomega.Equal(&IndexDefine{ 97 Kind: "partition", 98 Name: "list", 99 IndexDef: IndexDef{ 100 FieldNames: []string{"Name"}, 101 }, 102 })) 103 }) 104 } 105 106 type User struct { 107 ID uint64 `db:"f_id"` 108 Name string `db:"f_name"` 109 Username string `db:"f_username"` 110 } 111 112 func (User) TableName() string { 113 return "t_user" 114 } 115 116 type OrgUser struct { 117 OrgID uint64 `db:"f_org_id"` 118 UserID uint64 `db:"f_user_id"` 119 } 120 121 func (OrgUser) TableName() string { 122 return "t_org_user" 123 } 124 125 type Org struct { 126 ID uint64 `db:"f_id"` 127 Name string `db:"f_name"` 128 } 129 130 func (Org) TableName() string { 131 return "t_org" 132 } 133 134 type OrgUserAll struct { 135 OrgUser 136 User User `json:"user"` 137 Org Org `json:"org"` 138 } 139 140 func TestColumnsByStruct(t *testing.T) { 141 t.Run("simple", func(t *testing.T) { 142 q := ColumnsByStruct(&User{}).Ex(context.Background()).Query() 143 gomega.NewWithT(t).Expect(q).To(gomega.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")) 144 }) 145 146 t.Run("joined", func(t *testing.T) { 147 q := ColumnsByStruct(&OrgUserAll{}).Ex(context.Background()).Query() 148 149 for _, g := range strings.Split(q, ", ") { 150 t.Log(g) 151 } 152 153 gomega.NewWithT(t).Expect(q).To(gomega.Equal("t_org_user.f_org_id AS t_org_user__f_org_id, t_org_user.f_user_id AS t_org_user__f_user_id, 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, t_org.f_id AS t_org__f_id, t_org.f_name AS t_org__f_name")) 154 }) 155 }