-
Notifications
You must be signed in to change notification settings - Fork 502
/
types_test.go
150 lines (135 loc) · 4.23 KB
/
types_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package mssql
import (
"reflect"
"testing"
"time"
)
func TestMakeGoLangScanType(t *testing.T) {
if (reflect.TypeOf(int64(0)) != makeGoLangScanType(typeInfo{TypeId: typeInt8})) {
t.Errorf("invalid type returned for typeDateTime")
}
if (reflect.TypeOf(float64(0)) != makeGoLangScanType(typeInfo{TypeId: typeFlt4})) {
t.Errorf("invalid type returned for typeDateTime")
}
if (reflect.TypeOf(float64(0)) != makeGoLangScanType(typeInfo{TypeId: typeFlt8})) {
t.Errorf("invalid type returned for typeDateTime")
}
if (reflect.TypeOf("") != makeGoLangScanType(typeInfo{TypeId: typeVarChar})) {
t.Errorf("invalid type returned for typeDateTime")
}
if (reflect.TypeOf(time.Time{}) != makeGoLangScanType(typeInfo{TypeId: typeDateTime})) {
t.Errorf("invalid type returned for typeDateTime")
}
if (reflect.TypeOf(time.Time{}) != makeGoLangScanType(typeInfo{TypeId: typeDateTim4})) {
t.Errorf("invalid type returned for typeDateTim4")
}
if (reflect.TypeOf(int64(0)) != makeGoLangScanType(typeInfo{TypeId: typeInt1})) {
t.Errorf("invalid type returned for typeInt1")
}
if (reflect.TypeOf(int64(0)) != makeGoLangScanType(typeInfo{TypeId: typeInt2})) {
t.Errorf("invalid type returned for typeInt2")
}
if (reflect.TypeOf(int64(0)) != makeGoLangScanType(typeInfo{TypeId: typeInt4})) {
t.Errorf("invalid type returned for typeInt4")
}
if (reflect.TypeOf(int64(0)) != makeGoLangScanType(typeInfo{TypeId: typeIntN, Size: 4})) {
t.Errorf("invalid type returned for typeIntN")
}
if (reflect.TypeOf([]byte{}) != makeGoLangScanType(typeInfo{TypeId: typeMoney, Size: 8})) {
t.Errorf("invalid type returned for typeIntN")
}
}
func TestMakeGoLangTypeName(t *testing.T) {
defer handlePanic(t)
tests := []struct {
typeName string
typeString string
typeID uint8
}{
{"typeDateTime", "DATETIME", typeDateTime},
{"typeDateTim4", "SMALLDATETIME", typeDateTim4},
{"typeBigBinary", "BINARY", typeBigBinary},
//TODO: Add other supported types
}
for _, tt := range tests {
if makeGoLangTypeName(typeInfo{TypeId: tt.typeID}) != tt.typeString {
t.Errorf("invalid type name returned for %s", tt.typeName)
}
}
}
func TestMakeGoLangTypeLength(t *testing.T) {
defer handlePanic(t)
tests := []struct {
typeName string
typeVarLen bool
typeLen int64
typeID uint8
}{
{"typeDateTime", false, 0, typeDateTime},
{"typeDateTim4", false, 0, typeDateTim4},
{"typeBigBinary", false, 0, typeBigBinary},
//TODO: Add other supported types
}
for _, tt := range tests {
n, v := makeGoLangTypeLength(typeInfo{TypeId: tt.typeID})
if v != tt.typeVarLen {
t.Errorf("invalid type length variability returned for %s", tt.typeName)
}
if n != tt.typeLen {
t.Errorf("invalid type length returned for %s", tt.typeName)
}
}
}
func TestMakeGoLangTypePrecisionScale(t *testing.T) {
defer handlePanic(t)
tests := []struct {
typeName string
typeID uint8
typeVarLen bool
typePrec int64
typeScale int64
}{
{"typeDateTime", typeDateTime, false, 0, 0},
{"typeDateTim4", typeDateTim4, false, 0, 0},
{"typeBigBinary", typeBigBinary, false, 0, 0},
//TODO: Add other supported types
}
for _, tt := range tests {
prec, scale, varLen := makeGoLangTypePrecisionScale(typeInfo{TypeId: tt.typeID})
if varLen != tt.typeVarLen {
t.Errorf("invalid type length variability returned for %s", tt.typeName)
}
if prec != tt.typePrec || scale != tt.typeScale {
t.Errorf("invalid type precision and/or scale returned for %s", tt.typeName)
}
}
}
func TestMakeDecl(t *testing.T) {
defer handlePanic(t)
tests := []struct {
typeName string
Size int
typeID uint8
}{
{"varchar(max)", 0xffff, typeVarChar},
{"varchar(8000)", 8000, typeVarChar},
{"varchar(4001)", 4001, typeVarChar},
{"nvarchar(max)", 0xffff, typeNVarChar},
{"nvarchar(4000)", 8000, typeNVarChar},
{"nvarchar(2001)", 4002, typeNVarChar},
{"varbinary(max)", 0xffff, typeBigVarBin},
{"varbinary(8000)", 8000, typeBigVarBin},
{"varbinary(4001)", 4001, typeBigVarBin},
}
for _, tt := range tests {
s := makeDecl(typeInfo{TypeId: tt.typeID, Size: tt.Size})
if s != tt.typeName {
t.Errorf("invalid type translation for %s", tt.typeName)
}
}
}
func handlePanic(t *testing.T) {
if r := recover(); r != nil {
t.Errorf("recovered panic")
}
}