github.com/kunlun-qilian/sqlx/v3@v3.0.0/builder/def_columns_test.go (about) 1 package builder 2 3 import ( 4 "testing" 5 6 "github.com/onsi/gomega" 7 ) 8 9 func BenchmarkCols(b *testing.B) { 10 columns := Columns{} 11 12 columns.Add( 13 Col("f_id").Field("ID").Type(1, `,autoincrement`), 14 Col("f_name").Field("Name").Type(1, ``), 15 Col("f_f1").Field("F1").Type(1, ``), 16 Col("f_f2").Field("F2").Type(1, ``), 17 Col("f_f3").Field("F3").Type(1, ``), 18 Col("f_f4").Field("F4").Type(1, ``), 19 Col("f_f5").Field("F5").Type(1, ``), 20 Col("f_f6").Field("F6").Type(1, ``), 21 Col("f_f7").Field("F7").Type(1, ``), 22 Col("f_f8").Field("F8").Type(1, ``), 23 Col("f_f9").Field("F9").Type(1, ``), 24 ) 25 26 b.Run("pick", func(b *testing.B) { 27 for i := 0; i < b.N; i++ { 28 _ = columns.F("F3") 29 } 30 }) 31 32 b.Run("multi pick", func(b *testing.B) { 33 for i := 0; i < b.N; i++ { 34 _, _ = columns.Fields("ID", "Name") 35 } 36 }) 37 38 b.Run("multi pick all", func(b *testing.B) { 39 for i := 0; i < b.N; i++ { 40 _, _ = columns.Fields() 41 } 42 }) 43 44 } 45 46 func TestColumns(t *testing.T) { 47 columns := Columns{} 48 49 t.Run("empty columns", func(t *testing.T) { 50 gomega.NewWithT(t).Expect(columns.Len()).To(gomega.Equal(0)) 51 gomega.NewWithT(t).Expect(columns.AutoIncrement()).To(gomega.BeNil()) 52 }) 53 54 t.Run("added cols", func(t *testing.T) { 55 columns.Add( 56 Col("F_id").Field("ID").Type(1, `,autoincrement`), 57 ) 58 59 autoIncrementCol := columns.AutoIncrement() 60 61 gomega.NewWithT(t).Expect(autoIncrementCol).NotTo(gomega.BeNil()) 62 gomega.NewWithT(t).Expect(autoIncrementCol.Name).To(gomega.Equal("f_id")) 63 64 t.Run("get col by FieldName", func(t *testing.T) { 65 66 gomega.NewWithT(t).Expect(columns.F("ID2")).To(gomega.BeNil()) 67 68 gomega.NewWithT(t).Expect(MustCols(columns.Fields("ID2")).Len()).To(gomega.Equal(0)) 69 gomega.NewWithT(t).Expect(MustCols(columns.Fields()).Len()).To(gomega.Equal(1)) 70 71 gomega.NewWithT(t).Expect(MustCols(columns.Fields("ID2")).List()).To(gomega.HaveLen(0)) 72 gomega.NewWithT(t).Expect(MustCols(columns.Fields()).Len()).To(gomega.Equal(1)) 73 }) 74 t.Run("get col by ColName", func(t *testing.T) { 75 gomega.NewWithT(t).Expect(MustCols(columns.Cols("F_id")).Len()).To(gomega.Equal(1)) 76 gomega.NewWithT(t).Expect(MustCols(columns.Cols()).Len()).To(gomega.Equal(1)) 77 gomega.NewWithT(t).Expect(MustCols(columns.Cols()).List()).To(gomega.HaveLen(1)) 78 79 gomega.NewWithT(t).Expect(MustCols(columns.Cols()).FieldNames()).To(gomega.Equal([]string{"ID"})) 80 }) 81 }) 82 } 83 84 func MustCols(cols *Columns, err error) *Columns { 85 return cols 86 }