github.com/duskeagle/pop@v4.10.1-0.20190417200916-92f2b794aab5+incompatible/soda/cmd/generate/attribute_test.go (about) 1 package generate 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 ) 9 10 func Test_Attribute_String(t *testing.T) { 11 r := require.New(t) 12 13 cases := []struct { 14 exp string 15 name string 16 }{ 17 { 18 name: "id", 19 exp: "\tID string `json:\"id\" db:\"id\"`", 20 }, 21 { 22 name: "user_id", 23 exp: "\tUserID string `json:\"user_id\" db:\"user_id\"`", 24 }, 25 { 26 name: "UserID", 27 exp: "\tUserID string `json:\"user_id\" db:\"user_id\"`", 28 }, 29 { 30 name: "userid", 31 exp: "\tUserid string `json:\"userid\" db:\"userid\"`", 32 }, 33 { 34 name: "userId", 35 exp: "\tUserID string `json:\"user_id\" db:\"user_id\"`", 36 }, 37 { 38 name: "expires", 39 exp: "\tExpires string `json:\"expires\" db:\"expires\"`", 40 }, 41 { 42 name: "message_headers", 43 exp: "\tMessageHeaders string `json:\"message_headers\" db:\"message_headers\"`", 44 }, 45 } 46 47 for _, c := range cases { 48 model, err := newModel("car", "json", "models") 49 r.NoError(err) 50 a, err := newAttribute(c.name, &model) 51 r.NoError(err) 52 r.Equal(c.exp, a.String()) 53 } 54 } 55 56 func Test_newAttribute(t *testing.T) { 57 cases := []struct { 58 AttributeInput string 59 ResultType string 60 Nullable bool 61 62 ModelHasUUID bool 63 ModelHasNulls bool 64 ModelHasSlices bool 65 Invalid bool 66 }{ 67 { 68 AttributeInput: "name", 69 ResultType: "string", 70 }, 71 72 { 73 AttributeInput: "name:text", 74 ResultType: "string", 75 }, 76 { 77 AttributeInput: "id:uuid.UUID", 78 ResultType: "uuid.UUID", 79 }, 80 { 81 AttributeInput: "other:uuid", 82 ResultType: "uuid.UUID", 83 ModelHasUUID: true, 84 }, 85 { 86 AttributeInput: "optional:nulls.String", 87 ResultType: "nulls.String", 88 ModelHasNulls: true, 89 Nullable: true, 90 }, 91 { 92 AttributeInput: "optional:slices.float", 93 ResultType: "slices.Float", 94 ModelHasSlices: true, 95 }, 96 { 97 AttributeInput: "raw:blob", 98 ResultType: "[]byte", 99 }, 100 { 101 AttributeInput: "raw:[]byte", 102 ResultType: "[]byte", 103 }, 104 { 105 AttributeInput: "age:int", 106 ResultType: "int", 107 }, 108 { 109 AttributeInput: "age:int:int64", 110 ResultType: "int64", 111 }, 112 { 113 AttributeInput: "111:int", 114 Invalid: true, 115 }, 116 { 117 AttributeInput: "admin/user", 118 Invalid: true, 119 }, 120 { 121 AttributeInput: "admin;user", 122 Invalid: true, 123 }, 124 { 125 AttributeInput: "_bread", 126 Invalid: true, 127 }, 128 } 129 130 for index, tcase := range cases { 131 t.Run(fmt.Sprintf("%d-%s", index, tcase.AttributeInput), func(tt *testing.T) { 132 r := require.New(tt) 133 model, err := newModel("car", "json", "models") 134 r.NoError(err) 135 a, err := newAttribute(tcase.AttributeInput, &model) 136 if tcase.Invalid { 137 r.Errorf(err, "%s should be an invalid attribute", tcase.AttributeInput) 138 return 139 } 140 r.NoError(err) 141 142 r.Equal(a.GoType, tcase.ResultType) 143 r.Equal(a.Nullable, tcase.Nullable) 144 145 r.Equal(model.HasUUID, tcase.ModelHasUUID) 146 r.Equal(model.HasNulls, tcase.ModelHasNulls) 147 r.Equal(model.HasSlices, tcase.ModelHasSlices) 148 }) 149 } 150 151 }