forked from jmoiron/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
core_test.go
78 lines (62 loc) · 1.65 KB
/
core_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package sqlx
import (
"database/sql"
"reflect"
"testing"
)
func TestQueryable(t *testing.T) {
sqlDBType := reflect.TypeOf(&sql.DB{})
dbType := reflect.TypeOf(&DB{})
sqlTxType := reflect.TypeOf(&sql.Tx{})
txType := reflect.TypeOf(&Tx{})
dbMethods := exportableMethods(sqlDBType)
for k, v := range exportableMethods(dbType) {
dbMethods[k] = v
}
txMethods := exportableMethods(sqlTxType)
for k, v := range exportableMethods(txType) {
txMethods[k] = v
}
sharedMethods := make([]string, 0)
for name, dbMethod := range dbMethods {
if txMethod, ok := txMethods[name]; ok {
if methodsEqual(dbMethod.Type, txMethod.Type) {
sharedMethods = append(sharedMethods, name)
}
}
}
queryableType := reflect.TypeOf((*Queryable)(nil)).Elem()
queryableMethods := exportableMethods(queryableType)
for _, sharedMethodName := range sharedMethods {
if _, ok := queryableMethods[sharedMethodName]; !ok {
t.Errorf("Queryable does not include shared DB/Tx method: %s", sharedMethodName)
}
}
}
func exportableMethods(t reflect.Type) map[string]reflect.Method {
methods := make(map[string]reflect.Method)
for i := 0; i < t.NumMethod(); i++ {
method := t.Method(i)
if method.IsExported() {
methods[method.Name] = method
}
}
return methods
}
func methodsEqual(t reflect.Type, ot reflect.Type) bool {
if t.NumIn() != ot.NumIn() || t.NumOut() != ot.NumOut() || t.IsVariadic() != ot.IsVariadic() {
return false
}
// Start at 1 to avoid comparing receiver argument
for i := 1; i < t.NumIn(); i++ {
if t.In(i) != ot.In(i) {
return false
}
}
for i := 0; i < t.NumOut(); i++ {
if t.Out(i) != ot.Out(i) {
return false
}
}
return true
}