github.com/solongordon/pop@v4.10.0+incompatible/columns/columns_test.go (about)

     1  package columns_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gobuffalo/pop/columns"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  type foo struct {
    11  	FirstName string `db:"first_name" select:"first_name as f"`
    12  	LastName  string
    13  	Unwanted  string `db:"-"`
    14  	ReadOnly  string `db:"read" rw:"r"`
    15  	WriteOnly string `db:"write" rw:"w"`
    16  }
    17  
    18  type foos []foo
    19  
    20  func Test_Column_MapsSlice(t *testing.T) {
    21  	r := require.New(t)
    22  
    23  	c1 := columns.ForStruct(&foo{}, "foo")
    24  	c2 := columns.ForStruct(&foos{}, "foo")
    25  	r.Equal(c1.String(), c2.String())
    26  }
    27  
    28  func Test_Columns_Basics(t *testing.T) {
    29  	r := require.New(t)
    30  
    31  	for _, f := range []interface{}{foo{}, &foo{}} {
    32  		c := columns.ForStruct(f, "foo")
    33  		r.Equal(len(c.Cols), 4)
    34  		r.Equal(c.Cols["first_name"], &columns.Column{Name: "first_name", Writeable: false, Readable: true, SelectSQL: "first_name as f"})
    35  		r.Equal(c.Cols["LastName"], &columns.Column{Name: "LastName", Writeable: true, Readable: true, SelectSQL: "foo.LastName"})
    36  		r.Equal(c.Cols["read"], &columns.Column{Name: "read", Writeable: false, Readable: true, SelectSQL: "foo.read"})
    37  		r.Equal(c.Cols["write"], &columns.Column{Name: "write", Writeable: true, Readable: false, SelectSQL: "foo.write"})
    38  	}
    39  }
    40  
    41  func Test_Columns_Add(t *testing.T) {
    42  	r := require.New(t)
    43  
    44  	for _, f := range []interface{}{foo{}, &foo{}} {
    45  		c := columns.ForStruct(f, "foo")
    46  		r.Equal(len(c.Cols), 4)
    47  		c.Add("foo", "first_name")
    48  		r.Equal(len(c.Cols), 5)
    49  		r.Equal(c.Cols["foo"], &columns.Column{Name: "foo", Writeable: true, Readable: true, SelectSQL: "foo.foo"})
    50  	}
    51  }
    52  
    53  func Test_Columns_Remove(t *testing.T) {
    54  	r := require.New(t)
    55  
    56  	for _, f := range []interface{}{foo{}, &foo{}} {
    57  		c := columns.ForStruct(f, "foo")
    58  		r.Equal(len(c.Cols), 4)
    59  		c.Remove("foo", "first_name")
    60  		r.Equal(len(c.Cols), 3)
    61  	}
    62  }