github.com/movsb/taorm@v0.0.0-20201209183410-91bafb0b22a6/taorm/utils_test.go (about) 1 package taorm 2 3 import ( 4 "database/sql" 5 "database/sql/driver" 6 "fmt" 7 "reflect" 8 "testing" 9 "time" 10 "unsafe" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 type ColumnFieldStruct struct { 16 unexported int "false" 17 B bool "true" 18 I int "true" 19 I8 int8 "true" 20 I16 int16 "true" 21 I32 int32 "true" 22 I64 int64 "true" 23 U uint "true" 24 U8 uint8 "true" 25 U16 uint16 "true" 26 U32 uint32 "true" 27 U64 uint64 "true" 28 UPtr uintptr "false" 29 F32 float32 "true" 30 F64 float64 "true" 31 C64 complex64 "false" 32 C128 complex128 "false" 33 A [1]int "false" 34 C chan string "false" 35 F func() "false" 36 If interface{} "false" 37 M map[string]int "false" 38 P *int "false" 39 Slice []int "false" 40 S string "true" 41 Struct struct{} "false" 42 UP unsafe.Pointer "false" 43 44 NullBool sql.NullBool "true" 45 NullFloat64 sql.NullFloat64 "true" 46 NullInt64 sql.NullInt64 "true" 47 NullString sql.NullString "true" 48 49 Time time.Time "true" 50 Bytes []byte "true" 51 52 TypeWithScannerAndValuer _TypeWithScannerAndValuer "true" 53 TypeWithValueScannerAndValuer _TypeWithValueScannerAndValuer "false" 54 } 55 56 type _TypeWithScannerAndValuer struct{} 57 58 func (t _TypeWithScannerAndValuer) Value() (driver.Value, error) { 59 return "", nil 60 } 61 62 func (t *_TypeWithScannerAndValuer) Scan(value interface{}) error { 63 return nil 64 } 65 66 type _TypeWithValueScannerAndValuer struct{} 67 68 func (t _TypeWithValueScannerAndValuer) Value() (driver.Value, error) { 69 return "", nil 70 } 71 72 func (t _TypeWithValueScannerAndValuer) Scan(value interface{}) error { 73 return nil 74 } 75 76 func TestIsColumnField(t *testing.T) { 77 typ := reflect.TypeOf(ColumnFieldStruct{}) 78 for i := 0; i < typ.NumField(); i++ { 79 f := typ.Field(i) 80 b := isColumnField(f) 81 want := string(f.Tag) 82 got := fmt.Sprint(b) 83 if got != want { 84 t.Errorf("%-16s want=%-8s got=%-8s\n", f.Name, want, got) 85 } 86 } 87 } 88 89 func TestToSnakeCase(t *testing.T) { 90 tests := []struct { 91 a, b string 92 }{ 93 {`a`, `a`}, 94 {`A`, `a`}, 95 {`AB`, `ab`}, 96 {`Ab`, `ab`}, 97 {`ABc`, `a_bc`}, 98 {`AbcDef`, `abc_def`}, 99 {`URL`, `url`}, 100 {`URLString`, `url_string`}, 101 {`HTTPAPI`, `httpapi`}, 102 {`GRPCEndpoint`, `grpc_endpoint`}, 103 {`SQLInMarks`, `sql_in_marks`}, 104 } 105 for _, x := range tests { 106 assert.Equal(t, x.b, toSnakeCase(x.a)) 107 } 108 } 109 110 func TestGetColumnName(t *testing.T) { 111 s := struct { 112 A int `taorm:"-"` 113 B int `taorm:"xxx"` 114 C int `taorm:"name:c"` 115 D int `taorm:"name"` 116 }{} 117 n := []string{ 118 ``, 119 `b`, 120 `c`, 121 ``, 122 } 123 r := reflect.TypeOf(s) 124 for i := 0; i < r.NumField(); i++ { 125 assert.Equal(t, n[i], getColumnName(r.Field(i))) 126 } 127 } 128 129 func TestCreateSQLInMarks(t *testing.T) { 130 ss := []struct { 131 n int 132 m string 133 }{ 134 {0, `?`}, 135 {1, `?`}, 136 {2, `?,?`}, 137 {3, `?,?,?`}, 138 {10, `?,?,?,?,?,?,?,?,?,?`}, 139 } 140 for _, s := range ss { 141 assert.Equal(t, s.m, createSQLInMarks(s.n)) 142 } 143 }