github.com/wawandco/oxplugins@v0.7.11/tools/buffalo/model/attributes_test.go (about)

     1  package model
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/gobuffalo/flect/name"
     8  )
     9  
    10  func Test_BuildAttrs(t *testing.T) {
    11  	defaults := []attr{{Name: name.New("id"), goType: "uuid"}, {Name: name.New("created_at"), goType: "timestamp"}, {Name: name.New("updated_at"), goType: "timestamp"}}
    12  
    13  	cases := []struct {
    14  		args     []string
    15  		expected []attr
    16  		testName string
    17  	}{
    18  		{
    19  			testName: "Empty Args",
    20  			args:     []string{},
    21  			expected: defaults,
    22  		},
    23  		{
    24  			testName: "Some Args Without Type",
    25  			args:     []string{"description:text", "title"},
    26  			expected: []attr{{Name: name.New("id"), goType: "uuid"}, {Name: name.New("created_at"), goType: "timestamp"}, {Name: name.New("updated_at"), goType: "timestamp"}, {Name: name.New("description"), goType: "text"}, {Name: name.New("title"), goType: "string"}},
    27  		},
    28  		{
    29  			testName: "Replacing Defaults",
    30  			args:     []string{"description:text", "id:int"},
    31  			expected: []attr{{Name: name.New("created_at"), goType: "timestamp"}, {Name: name.New("updated_at"), goType: "timestamp"}, {Name: name.New("description"), goType: "text"}, {Name: name.New("id"), goType: "int"}},
    32  		},
    33  		{
    34  			testName: "Replacing Defaults 2",
    35  			args:     []string{"created_at:int", "description:text", "updated_at:int", "id:int"},
    36  			expected: []attr{{Name: name.New("created_at"), goType: "int"}, {Name: name.New("description"), goType: "text"}, {Name: name.New("updated_at"), goType: "int"}, {Name: name.New("id"), goType: "int"}},
    37  		},
    38  	}
    39  
    40  	for _, c := range cases {
    41  		t.Run(c.testName, func(t *testing.T) {
    42  			attrs := buildAttrs(c.args)
    43  			if !reflect.DeepEqual(c.expected, attrs) {
    44  				t.Errorf("unexpected result, it should be %v but got %v", c.expected, attrs)
    45  			}
    46  		})
    47  	}
    48  }