github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/builder/builder_z_columns_test.go (about) 1 package builder_test 2 3 import ( 4 "testing" 5 6 g "github.com/onsi/gomega" 7 8 . "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder" 9 . "github.com/machinefi/w3bstream/pkg/depends/testutil/buildertestutil" 10 ) 11 12 func BenchmarkCols(b *testing.B) { 13 columns := Columns{} 14 15 columns.Add( 16 Col("f_id").Field("ID").Type(1, `,autoincrement`), 17 Col("f_name").Field("Name").Type(1, ``), 18 Col("f_f1").Field("F1").Type(1, ``), 19 Col("f_f2").Field("F2").Type(1, ``), 20 Col("f_f3").Field("F3").Type(1, ``), 21 Col("f_f4").Field("F4").Type(1, ``), 22 Col("f_f5").Field("F5").Type(1, ``), 23 Col("f_f6").Field("F6").Type(1, ``), 24 Col("f_f7").Field("F7").Type(1, ``), 25 Col("f_f8").Field("F8").Type(1, ``), 26 Col("f_f9").Field("F9").Type(1, ``), 27 ) 28 29 b.Run("Pick", func(b *testing.B) { 30 for i := 0; i < b.N; i++ { 31 _ = columns.ColByFieldName("F3") 32 } 33 }) 34 b.Run("MultiPick", func(b *testing.B) { 35 for i := 0; i < b.N; i++ { 36 _, _ = columns.ColsByFieldNames("ID", "Name") 37 } 38 }) 39 b.Run("PickAll", func(b *testing.B) { 40 for i := 0; i < b.N; i++ { 41 _, _ = columns.ColsByFieldNames() 42 } 43 }) 44 } 45 46 func TestColumns(t *testing.T) { 47 columns := Columns{} 48 49 t.Run("EmptyColumns", func(t *testing.T) { 50 g.NewWithT(t).Expect(columns.Len()).To(g.Equal(0)) 51 g.NewWithT(t).Expect(columns.AutoIncrement()).To(g.BeNil()) 52 }) 53 54 t.Run("AddColumns", func(t *testing.T) { 55 columns.Add( 56 Col("F_id").Field("ID").Type(1, `,autoincrement`), 57 ) 58 59 autoIncrementCol := columns.AutoIncrement() 60 g.NewWithT(t).Expect(autoIncrementCol).NotTo(g.BeNil()) 61 g.NewWithT(t).Expect(autoIncrementCol.Name).To(g.Equal("f_id")) 62 63 t.Run("GetByFieldName", func(t *testing.T) { 64 g.NewWithT(t).Expect(columns.ColByFieldName("ID2")).To(g.BeNil()) 65 g.NewWithT(t).Expect(MustCols(columns.ColsByFieldNames("ID2")).Len()).To(g.Equal(0)) 66 g.NewWithT(t).Expect(MustCols(columns.ColsByFieldNames()).Len()).To(g.Equal(1)) 67 g.NewWithT(t).Expect(MustCols(columns.ColsByFieldNames("ID2")).List()).To(g.HaveLen(0)) 68 g.NewWithT(t).Expect(MustCols(columns.ColsByFieldNames()).Len()).To(g.Equal(1)) 69 }) 70 t.Run("GetByColName", func(t *testing.T) { 71 g.NewWithT(t).Expect(MustCols(columns.Cols("F_id")).Len()).To(g.Equal(1)) 72 g.NewWithT(t).Expect(MustCols(columns.Cols()).Len()).To(g.Equal(1)) 73 g.NewWithT(t).Expect(MustCols(columns.Cols()).List()).To(g.HaveLen(1)) 74 g.NewWithT(t).Expect(MustCols(columns.Cols()).FieldNames()).To(g.Equal([]string{"ID"})) 75 }) 76 }) 77 }