github.com/goravel/framework@v1.13.9/support/database/database_test.go (about) 1 package database 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/google/uuid" 8 "github.com/stretchr/testify/assert" 9 10 "github.com/goravel/framework/database/orm" 11 ) 12 13 type TestStruct struct { 14 ID int `gorm:"primaryKey"` 15 name string 16 } 17 18 type TestStructString struct { 19 ID string `gorm:"primaryKey"` 20 name string 21 } 22 23 type TestStructUUID struct { 24 ID uuid.UUID `gorm:"primaryKey"` 25 name string 26 } 27 28 type TestStructNoPK struct { 29 ID int 30 name string 31 } 32 33 func TestGetID(t *testing.T) { 34 tests := []struct { 35 description string 36 setup func(description string) 37 }{ 38 { 39 description: "return value", 40 setup: func(description string) { 41 type User struct { 42 ID uint `gorm:"primaryKey"` 43 Name string 44 Avatar string 45 } 46 user := User{} 47 user.ID = 1 48 assert.Equal(t, uint(1), GetID(&user), description) 49 }, 50 }, 51 { 52 description: "return value with orm.Model", 53 setup: func(description string) { 54 type User struct { 55 orm.Model 56 Name string 57 Avatar string 58 } 59 user := User{} 60 user.ID = 1 61 assert.Equal(t, uint(1), GetID(&user), description) 62 }, 63 }, 64 { 65 description: "return nil", 66 setup: func(description string) { 67 type User struct { 68 Name string 69 Avatar string 70 } 71 user := User{} 72 assert.Nil(t, GetID(&user), description) 73 }, 74 }, 75 { 76 description: "return value(struct)", 77 setup: func(description string) { 78 type User struct { 79 ID uint `gorm:"primaryKey"` 80 Name string 81 Avatar string 82 } 83 user := User{} 84 user.ID = 1 85 assert.Equal(t, uint(1), GetID(user), description) 86 }, 87 }, 88 { 89 description: "return value with orm.Model", 90 setup: func(description string) { 91 type User struct { 92 orm.Model 93 Name string 94 Avatar string 95 } 96 user := User{} 97 user.ID = 1 98 assert.Equal(t, uint(1), GetID(user), description) 99 }, 100 }, 101 { 102 description: "return nil", 103 setup: func(description string) { 104 type User struct { 105 Name string 106 Avatar string 107 } 108 user := User{} 109 assert.Nil(t, GetID(user), description) 110 }, 111 }, 112 { 113 description: "return nil when model is nil", 114 setup: func(description string) { 115 type User struct { 116 Name string 117 Avatar string 118 } 119 assert.Nil(t, GetID(&User{}), description) 120 assert.Nil(t, GetID(nil), description) 121 }, 122 }, 123 } 124 for _, test := range tests { 125 test.setup(test.description) 126 } 127 } 128 129 func TestGetIDByReflect(t *testing.T) { 130 tests := []struct { 131 description string 132 setup func(description string) 133 }{ 134 { 135 description: "TestStruct.ID type int", 136 setup: func(description string) { 137 ts := TestStruct{ID: 1, name: "name"} 138 v := reflect.ValueOf(ts) 139 tpe := reflect.TypeOf(ts) 140 141 result := GetIDByReflect(tpe, v) 142 143 assert.Equal(t, 1, result) 144 }, 145 }, 146 { 147 description: "TestStruct.ID type string", 148 setup: func(description string) { 149 ts := TestStructString{ID: "goravel", name: "name"} 150 v := reflect.ValueOf(ts) 151 tpe := reflect.TypeOf(ts) 152 153 result := GetIDByReflect(tpe, v) 154 155 assert.Equal(t, "goravel", result) 156 }, 157 }, 158 { 159 description: "TestStruct.ID type UUID", 160 setup: func(description string) { 161 id := uuid.New() 162 ts := TestStructUUID{ID: id, name: "name"} 163 v := reflect.ValueOf(ts) 164 tpe := reflect.TypeOf(ts) 165 166 result := GetIDByReflect(tpe, v) 167 168 assert.Equal(t, id, result) 169 }, 170 }, 171 { 172 description: "TestStruct without primaryKey", 173 setup: func(description string) { 174 ts := TestStructNoPK{ID: 1, name: "name"} 175 v := reflect.ValueOf(ts) 176 tpe := reflect.TypeOf(ts) 177 178 result := GetIDByReflect(tpe, v) 179 180 assert.Nil(t, result) 181 }, 182 }, 183 } 184 for _, test := range tests { 185 test.setup(test.description) 186 } 187 }