forked from ilibs/gosql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflect_test.go
59 lines (47 loc) · 1.2 KB
/
reflect_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package gosql
import (
"database/sql"
"reflect"
"testing"
"time"
"github.com/guoanfamily/gosql/v2/internal/example/models"
)
func TestReflectMapper_FieldMap(t *testing.T) {
mapper := NewReflectMapper("db")
{
user := &models.Users{
Id: 1,
Name: "test",
SuccessTime: sql.NullString{
String: "2018-09-03 00:00:00",
Valid: false,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
fields := mapper.FieldMap(reflect.ValueOf(user))
if len(fields) != 6 {
t.Error("FieldMap length error")
}
if v := fields["name"].Interface().(string); v != user.Name {
t.Errorf("Expecting %s, got %s", user.Name, v)
}
if v := fields["success_time"].Interface().(sql.NullString).String; v != user.SuccessTime.String {
t.Errorf("Expecting %s, got %s", user.Name, v)
}
}
{
photos := &models.Photos{}
fields := mapper.FieldMap(reflect.ValueOf(photos))
if len(fields) != 5 {
t.Error("FieldMap length error")
}
if v := fields["url"].Interface().(string); v != photos.Url {
t.Errorf("Expecting %s, got %s", photos.Url, v)
}
if _, ok := fields["created_at"].Interface().(time.Time); !ok {
t.Error("Expecting true, got false")
}
//fmt.Println(fields)
}
}