github.com/reggieriser/pop@v4.13.1+incompatible/columns/columns_test.go (about)

     1  package columns_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/gobuffalo/pop/columns"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  type foo struct {
    12  	FirstName string `db:"first_name" select:"first_name as f"`
    13  	LastName  string
    14  	Unwanted  string `db:"-"`
    15  	ReadOnly  string `db:"read" rw:"r"`
    16  	WriteOnly string `db:"write" rw:"w"`
    17  }
    18  
    19  type foos []foo
    20  
    21  func Test_Column_MapsSlice(t *testing.T) {
    22  	r := require.New(t)
    23  
    24  	c1 := columns.ForStruct(&foo{}, "foo")
    25  	c2 := columns.ForStruct(&foos{}, "foo")
    26  	r.Equal(c1.String(), c2.String())
    27  }
    28  
    29  func Test_Columns_Basics(t *testing.T) {
    30  	r := require.New(t)
    31  
    32  	for _, f := range []interface{}{foo{}, &foo{}} {
    33  		c := columns.ForStruct(f, "foo")
    34  		r.Equal(len(c.Cols), 4)
    35  		r.Equal(c.Cols["first_name"], &columns.Column{Name: "first_name", Writeable: false, Readable: true, SelectSQL: "first_name as f"})
    36  		r.Equal(c.Cols["LastName"], &columns.Column{Name: "LastName", Writeable: true, Readable: true, SelectSQL: "foo.LastName"})
    37  		r.Equal(c.Cols["read"], &columns.Column{Name: "read", Writeable: false, Readable: true, SelectSQL: "foo.read"})
    38  		r.Equal(c.Cols["write"], &columns.Column{Name: "write", Writeable: true, Readable: false, SelectSQL: "foo.write"})
    39  	}
    40  }
    41  
    42  func Test_Columns_Add(t *testing.T) {
    43  	r := require.New(t)
    44  
    45  	for _, f := range []interface{}{foo{}, &foo{}} {
    46  		c := columns.ForStruct(f, "foo")
    47  		r.Equal(len(c.Cols), 4)
    48  		c.Add("foo", "first_name")
    49  		r.Equal(len(c.Cols), 5)
    50  		r.Equal(c.Cols["foo"], &columns.Column{Name: "foo", Writeable: true, Readable: true, SelectSQL: "foo.foo"})
    51  	}
    52  }
    53  
    54  func Test_Columns_Remove(t *testing.T) {
    55  	r := require.New(t)
    56  
    57  	for _, f := range []interface{}{foo{}, &foo{}} {
    58  		c := columns.ForStruct(f, "foo")
    59  		r.Equal(len(c.Cols), 4)
    60  		c.Remove("foo", "first_name")
    61  		r.Equal(len(c.Cols), 3)
    62  	}
    63  }
    64  
    65  type fooWithSuffix struct {
    66  	Amount      float64 `db:"amount"`
    67  	AmountUnits string  `db:"amount_units"`
    68  }
    69  type fooQuoter struct{}
    70  
    71  func (fooQuoter) Quote(key string) string {
    72  	return fmt.Sprintf("`%v`", key)
    73  }
    74  
    75  func Test_Columns_Sorted(t *testing.T) {
    76  	r := require.New(t)
    77  
    78  	c := columns.ForStruct(fooWithSuffix{}, "fooWithSuffix")
    79  	r.Equal(len(c.Cols), 2)
    80  	r.Equal(c.SymbolizedString(), ":amount, :amount_units")
    81  	r.Equal(c.String(), "amount, amount_units")
    82  	r.Equal(c.QuotedString(fooQuoter{}), "`amount`, `amount_units`")
    83  }