-
Notifications
You must be signed in to change notification settings - Fork 317
/
mysql_test.go
138 lines (135 loc) · 2.88 KB
/
mysql_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
package loader
import (
"context"
"testing"
"github.com/xo/xo/templates"
)
func TestMysqlGoType(t *testing.T) {
tests := []struct {
name string
typ string
nullable bool
goType string
zero string
prec int
}{
{
name: "bit(1) parses",
typ: "bit(1)",
goType: "bool",
zero: "false",
prec: 1,
},
{
name: "bit(2) parses",
typ: "bit(2)",
goType: "uint8",
zero: "0",
prec: 2,
},
{
name: "bit(8) parses",
typ: "bit(8)",
goType: "uint8",
zero: "0",
prec: 8,
},
{
name: "bit(9) parses",
typ: "bit(9)",
goType: "uint16",
zero: "0",
prec: 9,
},
{
name: "bit(16) parses",
typ: "bit(16)",
goType: "uint16",
zero: "0",
prec: 16,
},
{
name: "bit(17) parses",
typ: "bit(17)",
goType: "uint32",
zero: "0",
prec: 17,
},
{
name: "bit(32) parses",
typ: "bit(32)",
goType: "uint32",
zero: "0",
prec: 32,
},
{
name: "bit(33) parses",
typ: "bit(33)",
goType: "uint64",
zero: "0",
prec: 33,
},
{
name: "bit(64) parses",
typ: "bit(64)",
goType: "uint64",
zero: "0",
prec: 64,
},
{
name: "nullable bit type with precision == 1 parses",
typ: "bit(1)",
nullable: true,
goType: "sql.NullBool",
zero: "sql.NullBool{}",
prec: 1,
},
{
name: "nullable bit type with precision > 1 parses",
typ: "bit(64)",
nullable: true,
goType: "sql.NullInt64",
zero: "sql.NullInt64{}",
prec: 64,
},
{
name: "tinyint with precision one parses into bool",
typ: "tinyint(1)",
goType: "bool",
zero: "false",
prec: 1,
},
{
name: "nullable tinyint with precision one parses into bool",
typ: "tinyint(1)",
nullable: true,
goType: "sql.NullBool",
zero: "sql.NullBool{}",
prec: 1,
},
{
name: "tinyint with greater than one precision parses into int8",
typ: "tinyint(4)",
goType: "int8",
zero: "0",
prec: 4,
},
}
ctx := context.Background()
ctx = context.WithValue(ctx, templates.SchemaKey, "mysql")
for i, test := range tests {
goType, zero, prec, err := MysqlGoType(ctx, test.typ, test.nullable)
if err != nil {
t.Fatalf("test %d (%s) %q (nullable: %t) expected no error, got: %v", i, test.name, test.typ, test.nullable, err)
}
if goType != test.goType {
t.Errorf("test %d (%s) %q (nullable: %t) expected goType = %q, got: %q", i, test.name, test.typ, test.nullable, test.goType, goType)
}
if zero != test.zero {
t.Errorf("test %d (%s) %q (nullable: %t) expected zero = %q, got: %q", i, test.name, test.typ, test.nullable, test.zero, zero)
}
if prec != test.prec {
t.Errorf("test %d (%s) %q (nullable: %t) expected prec = %d, got: %d", i, test.name, test.typ, test.nullable, test.prec, prec)
}
}
}