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

     1  package builder
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/onsi/gomega"
     7  )
     8  
     9  func TestColumns(t *testing.T) {
    10  	columns := Columns{}
    11  
    12  	t.Run("empty columns", func(t *testing.T) {
    13  		gomega.NewWithT(t).Expect(columns.Len()).To(gomega.Equal(0))
    14  		gomega.NewWithT(t).Expect(columns.AutoIncrement()).To(gomega.BeNil())
    15  	})
    16  	t.Run("added cols", func(t *testing.T) {
    17  		columns.Add(
    18  			Col("F_id").Field("ID").Type(1, `,autoincrement`),
    19  		)
    20  
    21  		autoIncrementCol := columns.AutoIncrement()
    22  
    23  		gomega.NewWithT(t).Expect(autoIncrementCol).NotTo(gomega.BeNil())
    24  		gomega.NewWithT(t).Expect(autoIncrementCol.Name).To(gomega.Equal("f_id"))
    25  
    26  		t.Run("get col by FieldName", func(t *testing.T) {
    27  
    28  			gomega.NewWithT(t).Expect(columns.F("ID2")).To(gomega.BeNil())
    29  
    30  			gomega.NewWithT(t).Expect(MustCols(columns.Fields("ID2")).Len()).To(gomega.Equal(0))
    31  			gomega.NewWithT(t).Expect(MustCols(columns.Fields()).Len()).To(gomega.Equal(1))
    32  
    33  			gomega.NewWithT(t).Expect(MustCols(columns.Fields("ID2")).List()).To(gomega.HaveLen(0))
    34  			gomega.NewWithT(t).Expect(MustCols(columns.Fields()).Len()).To(gomega.Equal(1))
    35  		})
    36  		t.Run("get col by ColName", func(t *testing.T) {
    37  			gomega.NewWithT(t).Expect(MustCols(columns.Cols("F_id")).Len()).To(gomega.Equal(1))
    38  			gomega.NewWithT(t).Expect(MustCols(columns.Cols()).Len()).To(gomega.Equal(1))
    39  			gomega.NewWithT(t).Expect(MustCols(columns.Cols()).List()).To(gomega.HaveLen(1))
    40  
    41  			gomega.NewWithT(t).Expect(MustCols(columns.Cols()).FieldNames()).To(gomega.Equal([]string{"ID"}))
    42  		})
    43  	})
    44  }
    45  
    46  func MustCols(cols *Columns, err error) *Columns {
    47  	return cols
    48  }