Debian Codegraph @

utils/tests/utils.go

raw
1 package tests
2
3 import (
4 "database/sql/driver"
5 "fmt"
6 "go/ast"
7 "reflect"
8 "testing"
9 "time"
10
11 "gorm.io/gorm/utils"
12 )
13
14 func AssertObjEqual(t *testing.T, r, e interface{}, names ...string) {
15 for _, name := range names {
16 rv := reflect.Indirect(reflect.ValueOf(r))
17 ev := reflect.Indirect(reflect.ValueOf(e))
18 if rv.IsValid() != ev.IsValid() {
19 t.Errorf("%v: expect: %+v, got %+v", utils.FileWithLineNum(), r, e)
20 return
21 }
22 got := rv.FieldByName(name).Interface()
23 expect := ev.FieldByName(name).Interface()
24 t.Run(name, func(t *testing.T) {
25 AssertEqual(t, got, expect)
26 })
27 }
28 }
29
30 func AssertEqual(t *testing.T, got, expect interface{}) {
31 if !reflect.DeepEqual(got, expect) {
32 isEqual := func() {
33 if curTime, ok := got.(time.Time); ok {
34 format := "2006-01-02T15:04:05Z07:00"
35
36 if curTime.Round(time.Second).UTC().Format(format) != expect.(time.Time).Round(time.Second).UTC().Format(format) && curTime.Truncate(time.Second).UTC().Format(format) != expect.(time.Time).Truncate(time.Second).UTC().Format(format) {
37 t.Errorf("%v: expect: %v, got %v after time round", utils.FileWithLineNum(), expect.(time.Time), curTime)
38 }
39 } else if fmt.Sprint(got) != fmt.Sprint(expect) {
40 t.Errorf("%v: expect: %#v, got %#v", utils.FileWithLineNum(), expect, got)
41 }
42 }
43
44 if fmt.Sprint(got) == fmt.Sprint(expect) {
45 return
46 }
47
48 if reflect.Indirect(reflect.ValueOf(got)).IsValid() != reflect.Indirect(reflect.ValueOf(expect)).IsValid() {
49 t.Errorf("%v: expect: %+v, got %+v", utils.FileWithLineNum(), expect, got)
50 return
51 }
52
53 if valuer, ok := got.(driver.Valuer); ok {
54 got, _ = valuer.Value()
55 }
56
57 if valuer, ok := expect.(driver.Valuer); ok {
58 expect, _ = valuer.Value()
59 }
60
61 if got != nil {
62 got = reflect.Indirect(reflect.ValueOf(got)).Interface()
63 }
64
65 if expect != nil {
66 expect = reflect.Indirect(reflect.ValueOf(expect)).Interface()
67 }
68
69 if reflect.ValueOf(got).IsValid() != reflect.ValueOf(expect).IsValid() {
70 t.Errorf("%v: expect: %+v, got %+v", utils.FileWithLineNum(), expect, got)
71 return
72 }
73
74 if reflect.ValueOf(got).Kind() == reflect.Slice {
75 if reflect.ValueOf(expect).Kind() == reflect.Slice {
76 if reflect.ValueOf(got).Len() == reflect.ValueOf(expect).Len() {
77 for i := 0; i < reflect.ValueOf(got).Len(); i++ {
78 name := fmt.Sprintf(reflect.ValueOf(got).Type().Name()+" #%v", i)
79 t.Run(name, func(t *testing.T) {
80 AssertEqual(t, reflect.ValueOf(got).Index(i).Interface(), reflect.ValueOf(expect).Index(i).Interface())
81 })
82 }
83 } else {
84 name := reflect.ValueOf(got).Type().Elem().Name()
85 t.Errorf("%v expects length: %v, got %v (expects: %+v, got %+v)", name, reflect.ValueOf(expect).Len(), reflect.ValueOf(got).Len(), expect, got)
86 }
87 return
88 }
89 }
90
91 if reflect.ValueOf(got).Kind() == reflect.Struct {
92 if reflect.ValueOf(expect).Kind() == reflect.Struct {
93 if reflect.ValueOf(got).NumField() == reflect.ValueOf(expect).NumField() {
94 exported := false
95 for i := 0; i < reflect.ValueOf(got).NumField(); i++ {
96 if fieldStruct := reflect.ValueOf(got).Type().Field(i); ast.IsExported(fieldStruct.Name) {
97 exported = true
98 field := reflect.ValueOf(got).Field(i)
99 t.Run(fieldStruct.Name, func(t *testing.T) {
100 AssertEqual(t, field.Interface(), reflect.ValueOf(expect).Field(i).Interface())
101 })
102 }
103 }
104
105 if exported {
106 return
107 }
108 }
109 }
110 }
111
112 if reflect.ValueOf(got).Type().ConvertibleTo(reflect.ValueOf(expect).Type()) {
113 got = reflect.ValueOf(got).Convert(reflect.ValueOf(expect).Type()).Interface()
114 isEqual()
115 } else if reflect.ValueOf(expect).Type().ConvertibleTo(reflect.ValueOf(got).Type()) {
116 expect = reflect.ValueOf(got).Convert(reflect.ValueOf(got).Type()).Interface()
117 isEqual()
118 } else {
119 t.Errorf("%v: expect: %+v, got %+v", utils.FileWithLineNum(), expect, got)
120 return
121 }
122 }
123 }
124
125 func Now() *time.Time {
126 now := time.Now()
127 return &now
128 }