@@ -42,6 +42,55 @@ func init() {
42
42
}
43
43
}
44
44
45
+ type DBTest struct {
46
+ * testing.T
47
+ db * sql.DB
48
+ }
49
+
50
+ func runTests (t * testing.T , name , dsn string , tests ... func (dbt * DBTest )) {
51
+ if ! available {
52
+ t .Logf ("MySQL-Server not running on %s. Skipping %s" , netAddr , name )
53
+ return
54
+ }
55
+
56
+ db , err := sql .Open ("mysql" , dsn )
57
+ if err != nil {
58
+ t .Fatalf ("Error connecting: %v" , err )
59
+ }
60
+ defer db .Close ()
61
+
62
+ db .Exec ("DROP TABLE IF EXISTS test" )
63
+
64
+ dbt := & DBTest {t , db }
65
+ for _ , test := range tests {
66
+ test (dbt )
67
+ dbt .db .Exec ("DROP TABLE IF EXISTS test" )
68
+ }
69
+ }
70
+
71
+ func (dbt * DBTest ) fail (method , query string , err error ) {
72
+ if len (query ) > 300 {
73
+ query = "[query too large to print]"
74
+ }
75
+ dbt .Fatalf ("Error on %s %s: %v" , method , query , err )
76
+ }
77
+
78
+ func (dbt * DBTest ) mustExec (query string , args ... interface {}) (res sql.Result ) {
79
+ res , err := dbt .db .Exec (query , args ... )
80
+ if err != nil {
81
+ dbt .fail ("Exec" , query , err )
82
+ }
83
+ return res
84
+ }
85
+
86
+ func (dbt * DBTest ) mustQuery (query string , args ... interface {}) (rows * sql.Rows ) {
87
+ rows , err := dbt .db .Query (query , args ... )
88
+ if err != nil {
89
+ dbt .fail ("Query" , query , err )
90
+ }
91
+ return rows
92
+ }
93
+
45
94
func TestCharset (t * testing.T ) {
46
95
mustSetCharset := func (charsetParam , expected string ) {
47
96
db , err := sql .Open ("mysql" , strings .Replace (dsn , charset , charsetParam , 1 ))
@@ -101,57 +150,8 @@ func TestFailingCharset(t *testing.T) {
101
150
}
102
151
}
103
152
104
- type DBTest struct {
105
- * testing.T
106
- db * sql.DB
107
- }
108
-
109
- func runTests (t * testing.T , name string , tests ... func (dbt * DBTest )) {
110
- if ! available {
111
- t .Logf ("MySQL-Server not running on %s. Skipping %s" , netAddr , name )
112
- return
113
- }
114
-
115
- db , err := sql .Open ("mysql" , dsn )
116
- if err != nil {
117
- t .Fatalf ("Error connecting: %v" , err )
118
- }
119
- defer db .Close ()
120
-
121
- db .Exec ("DROP TABLE IF EXISTS test" )
122
-
123
- dbt := & DBTest {t , db }
124
- for _ , test := range tests {
125
- test (dbt )
126
- dbt .db .Exec ("DROP TABLE IF EXISTS test" )
127
- }
128
- }
129
-
130
- func (dbt * DBTest ) fail (method , query string , err error ) {
131
- if len (query ) > 300 {
132
- query = "[query too large to print]"
133
- }
134
- dbt .Fatalf ("Error on %s %s: %v" , method , query , err )
135
- }
136
-
137
- func (dbt * DBTest ) mustExec (query string , args ... interface {}) (res sql.Result ) {
138
- res , err := dbt .db .Exec (query , args ... )
139
- if err != nil {
140
- dbt .fail ("Exec" , query , err )
141
- }
142
- return res
143
- }
144
-
145
- func (dbt * DBTest ) mustQuery (query string , args ... interface {}) (rows * sql.Rows ) {
146
- rows , err := dbt .db .Query (query , args ... )
147
- if err != nil {
148
- dbt .fail ("Query" , query , err )
149
- }
150
- return rows
151
- }
152
-
153
153
func TestRawBytesResultExceedsBuffer (t * testing.T ) {
154
- runTests (t , "TestRawBytesResultExceedsBuffer" , func (dbt * DBTest ) {
154
+ runTests (t , "TestRawBytesResultExceedsBuffer" , dsn , func (dbt * DBTest ) {
155
155
// defaultBufSize from buffer.go
156
156
expected := strings .Repeat ("abc" , defaultBufSize )
157
157
rows := dbt .mustQuery ("SELECT '" + expected + "'" )
@@ -168,7 +168,7 @@ func TestRawBytesResultExceedsBuffer(t *testing.T) {
168
168
}
169
169
170
170
func TestCRUD (t * testing.T ) {
171
- runTests (t , "TestCRUD" , func (dbt * DBTest ) {
171
+ runTests (t , "TestCRUD" , dsn , func (dbt * DBTest ) {
172
172
// Create Table
173
173
dbt .mustExec ("CREATE TABLE test (value BOOL)" )
174
174
@@ -260,7 +260,7 @@ func TestCRUD(t *testing.T) {
260
260
}
261
261
262
262
func TestInt (t * testing.T ) {
263
- runTests (t , "TestInt" , func (dbt * DBTest ) {
263
+ runTests (t , "TestInt" , dsn , func (dbt * DBTest ) {
264
264
types := [5 ]string {"TINYINT" , "SMALLINT" , "MEDIUMINT" , "INT" , "BIGINT" }
265
265
in := int64 (42 )
266
266
var out int64
@@ -307,7 +307,7 @@ func TestInt(t *testing.T) {
307
307
}
308
308
309
309
func TestFloat (t * testing.T ) {
310
- runTests (t , "TestFloat" , func (dbt * DBTest ) {
310
+ runTests (t , "TestFloat" , dsn , func (dbt * DBTest ) {
311
311
types := [2 ]string {"FLOAT" , "DOUBLE" }
312
312
in := float32 (42.23 )
313
313
var out float32
@@ -330,7 +330,7 @@ func TestFloat(t *testing.T) {
330
330
}
331
331
332
332
func TestString (t * testing.T ) {
333
- runTests (t , "TestString" , func (dbt * DBTest ) {
333
+ runTests (t , "TestString" , dsn , func (dbt * DBTest ) {
334
334
types := [6 ]string {"CHAR(255)" , "VARCHAR(255)" , "TINYTEXT" , "TEXT" , "MEDIUMTEXT" , "LONGTEXT" }
335
335
in := "κόσμε üöäßñóùéàâÿœ'îë Árvíztűrő いろはにほへとちりぬるを イロハニホヘト דג סקרן чащах น่าฟังเอย"
336
336
var out string
@@ -470,18 +470,15 @@ func TestDateTime(t *testing.T) {
470
470
}
471
471
}
472
472
473
- oldDsn := dsn
474
- usedDsn := oldDsn + "&sql_mode=ALLOW_INVALID_DATES"
473
+ timeDsn := dsn + "&sql_mode=ALLOW_INVALID_DATES"
475
474
for _ , v := range setups {
476
475
s = v
477
- dsn = usedDsn + s .dsnSuffix
478
- runTests (t , "TestDateTime" , testTime )
476
+ runTests (t , "TestDateTime" , timeDsn + s .dsnSuffix , testTime )
479
477
}
480
- dsn = oldDsn
481
478
}
482
479
483
480
func TestNULL (t * testing.T ) {
484
- runTests (t , "TestNULL" , func (dbt * DBTest ) {
481
+ runTests (t , "TestNULL" , dsn , func (dbt * DBTest ) {
485
482
nullStmt , err := dbt .db .Prepare ("SELECT NULL" )
486
483
if err != nil {
487
484
dbt .Fatal (err )
@@ -597,7 +594,7 @@ func TestNULL(t *testing.T) {
597
594
}
598
595
599
596
func TestLongData (t * testing.T ) {
600
- runTests (t , "TestLongData" , func (dbt * DBTest ) {
597
+ runTests (t , "TestLongData" , dsn , func (dbt * DBTest ) {
601
598
var maxAllowedPacketSize int
602
599
err := dbt .db .QueryRow ("select @@max_allowed_packet" ).Scan (& maxAllowedPacketSize )
603
600
if err != nil {
@@ -654,7 +651,7 @@ func TestLongData(t *testing.T) {
654
651
}
655
652
656
653
func TestLoadData (t * testing.T ) {
657
- runTests (t , "TestLoadData" , func (dbt * DBTest ) {
654
+ runTests (t , "TestLoadData" , dsn , func (dbt * DBTest ) {
658
655
verifyLoadDataResult := func () {
659
656
rows , err := dbt .db .Query ("SELECT * FROM test" )
660
657
if err != nil {
@@ -741,7 +738,9 @@ func TestLoadData(t *testing.T) {
741
738
}
742
739
743
740
func TestStrict (t * testing.T ) {
744
- runTests (t , "TestStrict" , func (dbt * DBTest ) {
741
+ // ALLOW_INVALID_DATES to get rid of stricter modes - we want to test for warnings, not errors
742
+ relaxedDsn := dsn + "&sql_mode=ALLOW_INVALID_DATES"
743
+ runTests (t , "TestStrict" , relaxedDsn , func (dbt * DBTest ) {
745
744
dbt .mustExec ("CREATE TABLE test (a TINYINT NOT NULL, b CHAR(4))" )
746
745
747
746
var queries = [... ]struct {
@@ -808,7 +807,7 @@ func TestStrict(t *testing.T) {
808
807
// Special cases
809
808
810
809
func TestRowsClose (t * testing.T ) {
811
- runTests (t , "TestRowsClose" , func (dbt * DBTest ) {
810
+ runTests (t , "TestRowsClose" , dsn , func (dbt * DBTest ) {
812
811
rows , err := dbt .db .Query ("SELECT 1" )
813
812
if err != nil {
814
813
dbt .Fatal (err )
@@ -833,7 +832,7 @@ func TestRowsClose(t *testing.T) {
833
832
// dangling statements
834
833
// http://code.google.com/p/go/issues/detail?id=3865
835
834
func TestCloseStmtBeforeRows (t * testing.T ) {
836
- runTests (t , "TestCloseStmtBeforeRows" , func (dbt * DBTest ) {
835
+ runTests (t , "TestCloseStmtBeforeRows" , dsn , func (dbt * DBTest ) {
837
836
stmt , err := dbt .db .Prepare ("SELECT 1" )
838
837
if err != nil {
839
838
dbt .Fatal (err )
@@ -874,7 +873,7 @@ func TestCloseStmtBeforeRows(t *testing.T) {
874
873
// It is valid to have multiple Rows for the same Stmt
875
874
// http://code.google.com/p/go/issues/detail?id=3734
876
875
func TestStmtMultiRows (t * testing.T ) {
877
- runTests (t , "TestStmtMultiRows" , func (dbt * DBTest ) {
876
+ runTests (t , "TestStmtMultiRows" , dsn , func (dbt * DBTest ) {
878
877
stmt , err := dbt .db .Prepare ("SELECT 1 UNION SELECT 0" )
879
878
if err != nil {
880
879
dbt .Fatal (err )
@@ -989,7 +988,7 @@ func TestConcurrent(t *testing.T) {
989
988
t .Log ("CONCURRENT env var not set. Skipping TestConcurrent" )
990
989
return
991
990
}
992
- runTests (t , "TestConcurrent" , func (dbt * DBTest ) {
991
+ runTests (t , "TestConcurrent" , dsn , func (dbt * DBTest ) {
993
992
var max int
994
993
err := dbt .db .QueryRow ("SELECT @@max_connections" ).Scan (& max )
995
994
if err != nil {
0 commit comments