@@ -52,6 +52,55 @@ func init() {
52
52
}
53
53
}
54
54
55
+ type DBTest struct {
56
+ * testing.T
57
+ db * sql.DB
58
+ }
59
+
60
+ func runTests (t * testing.T , name , dsn string , tests ... func (dbt * DBTest )) {
61
+ if ! available {
62
+ t .Logf ("MySQL-Server not running on %s. Skipping %s" , netAddr , name )
63
+ return
64
+ }
65
+
66
+ db , err := sql .Open ("mysql" , dsn )
67
+ if err != nil {
68
+ t .Fatalf ("Error connecting: %v" , err )
69
+ }
70
+ defer db .Close ()
71
+
72
+ db .Exec ("DROP TABLE IF EXISTS test" )
73
+
74
+ dbt := & DBTest {t , db }
75
+ for _ , test := range tests {
76
+ test (dbt )
77
+ dbt .db .Exec ("DROP TABLE IF EXISTS test" )
78
+ }
79
+ }
80
+
81
+ func (dbt * DBTest ) fail (method , query string , err error ) {
82
+ if len (query ) > 300 {
83
+ query = "[query too large to print]"
84
+ }
85
+ dbt .Fatalf ("Error on %s %s: %v" , method , query , err )
86
+ }
87
+
88
+ func (dbt * DBTest ) mustExec (query string , args ... interface {}) (res sql.Result ) {
89
+ res , err := dbt .db .Exec (query , args ... )
90
+ if err != nil {
91
+ dbt .fail ("Exec" , query , err )
92
+ }
93
+ return res
94
+ }
95
+
96
+ func (dbt * DBTest ) mustQuery (query string , args ... interface {}) (rows * sql.Rows ) {
97
+ rows , err := dbt .db .Query (query , args ... )
98
+ if err != nil {
99
+ dbt .fail ("Query" , query , err )
100
+ }
101
+ return rows
102
+ }
103
+
55
104
func TestCharset (t * testing.T ) {
56
105
mustSetCharset := func (charsetParam , expected string ) {
57
106
db , err := sql .Open ("mysql" , strings .Replace (dsn , charset , charsetParam , 1 ))
@@ -111,57 +160,8 @@ func TestFailingCharset(t *testing.T) {
111
160
}
112
161
}
113
162
114
- type DBTest struct {
115
- * testing.T
116
- db * sql.DB
117
- }
118
-
119
- func runTests (t * testing.T , name string , tests ... func (dbt * DBTest )) {
120
- if ! available {
121
- t .Logf ("MySQL-Server not running on %s. Skipping %s" , netAddr , name )
122
- return
123
- }
124
-
125
- db , err := sql .Open ("mysql" , dsn )
126
- if err != nil {
127
- t .Fatalf ("Error connecting: %v" , err )
128
- }
129
- defer db .Close ()
130
-
131
- db .Exec ("DROP TABLE IF EXISTS test" )
132
-
133
- dbt := & DBTest {t , db }
134
- for _ , test := range tests {
135
- test (dbt )
136
- dbt .db .Exec ("DROP TABLE IF EXISTS test" )
137
- }
138
- }
139
-
140
- func (dbt * DBTest ) fail (method , query string , err error ) {
141
- if len (query ) > 300 {
142
- query = "[query too large to print]"
143
- }
144
- dbt .Fatalf ("Error on %s %s: %v" , method , query , err )
145
- }
146
-
147
- func (dbt * DBTest ) mustExec (query string , args ... interface {}) (res sql.Result ) {
148
- res , err := dbt .db .Exec (query , args ... )
149
- if err != nil {
150
- dbt .fail ("Exec" , query , err )
151
- }
152
- return res
153
- }
154
-
155
- func (dbt * DBTest ) mustQuery (query string , args ... interface {}) (rows * sql.Rows ) {
156
- rows , err := dbt .db .Query (query , args ... )
157
- if err != nil {
158
- dbt .fail ("Query" , query , err )
159
- }
160
- return rows
161
- }
162
-
163
163
func TestRawBytesResultExceedsBuffer (t * testing.T ) {
164
- runTests (t , "TestRawBytesResultExceedsBuffer" , func (dbt * DBTest ) {
164
+ runTests (t , "TestRawBytesResultExceedsBuffer" , dsn , func (dbt * DBTest ) {
165
165
// defaultBufSize from buffer.go
166
166
expected := strings .Repeat ("abc" , defaultBufSize )
167
167
rows := dbt .mustQuery ("SELECT '" + expected + "'" )
@@ -178,7 +178,7 @@ func TestRawBytesResultExceedsBuffer(t *testing.T) {
178
178
}
179
179
180
180
func TestCRUD (t * testing.T ) {
181
- runTests (t , "TestCRUD" , func (dbt * DBTest ) {
181
+ runTests (t , "TestCRUD" , dsn , func (dbt * DBTest ) {
182
182
// Create Table
183
183
dbt .mustExec ("CREATE TABLE test (value BOOL)" )
184
184
@@ -270,7 +270,7 @@ func TestCRUD(t *testing.T) {
270
270
}
271
271
272
272
func TestInt (t * testing.T ) {
273
- runTests (t , "TestInt" , func (dbt * DBTest ) {
273
+ runTests (t , "TestInt" , dsn , func (dbt * DBTest ) {
274
274
types := [5 ]string {"TINYINT" , "SMALLINT" , "MEDIUMINT" , "INT" , "BIGINT" }
275
275
in := int64 (42 )
276
276
var out int64
@@ -317,7 +317,7 @@ func TestInt(t *testing.T) {
317
317
}
318
318
319
319
func TestFloat (t * testing.T ) {
320
- runTests (t , "TestFloat" , func (dbt * DBTest ) {
320
+ runTests (t , "TestFloat" , dsn , func (dbt * DBTest ) {
321
321
types := [2 ]string {"FLOAT" , "DOUBLE" }
322
322
in := float32 (42.23 )
323
323
var out float32
@@ -340,7 +340,7 @@ func TestFloat(t *testing.T) {
340
340
}
341
341
342
342
func TestString (t * testing.T ) {
343
- runTests (t , "TestString" , func (dbt * DBTest ) {
343
+ runTests (t , "TestString" , dsn , func (dbt * DBTest ) {
344
344
types := [6 ]string {"CHAR(255)" , "VARCHAR(255)" , "TINYTEXT" , "TEXT" , "MEDIUMTEXT" , "LONGTEXT" }
345
345
in := "κόσμε üöäßñóùéàâÿœ'îë Árvíztűrő いろはにほへとちりぬるを イロハニホヘト דג סקרן чащах น่าฟังเอย"
346
346
var out string
@@ -473,18 +473,15 @@ func TestDateTime(t *testing.T) {
473
473
}
474
474
}
475
475
476
- oldDsn := dsn
477
- usedDsn := oldDsn + "&sql_mode=ALLOW_INVALID_DATES"
476
+ timeDsn := dsn + "&sql_mode=ALLOW_INVALID_DATES"
478
477
for _ , v := range setups {
479
478
s = v
480
- dsn = usedDsn + s .dsnSuffix
481
- runTests (t , "TestDateTime" , testTime )
479
+ runTests (t , "TestDateTime" , timeDsn + s .dsnSuffix , testTime )
482
480
}
483
- dsn = oldDsn
484
481
}
485
482
486
483
func TestNULL (t * testing.T ) {
487
- runTests (t , "TestNULL" , func (dbt * DBTest ) {
484
+ runTests (t , "TestNULL" , dsn , func (dbt * DBTest ) {
488
485
nullStmt , err := dbt .db .Prepare ("SELECT NULL" )
489
486
if err != nil {
490
487
dbt .Fatal (err )
@@ -600,7 +597,7 @@ func TestNULL(t *testing.T) {
600
597
}
601
598
602
599
func TestLongData (t * testing.T ) {
603
- runTests (t , "TestLongData" , func (dbt * DBTest ) {
600
+ runTests (t , "TestLongData" , dsn , func (dbt * DBTest ) {
604
601
var maxAllowedPacketSize int
605
602
err := dbt .db .QueryRow ("select @@max_allowed_packet" ).Scan (& maxAllowedPacketSize )
606
603
if err != nil {
@@ -657,7 +654,7 @@ func TestLongData(t *testing.T) {
657
654
}
658
655
659
656
func TestLoadData (t * testing.T ) {
660
- runTests (t , "TestLoadData" , func (dbt * DBTest ) {
657
+ runTests (t , "TestLoadData" , dsn , func (dbt * DBTest ) {
661
658
verifyLoadDataResult := func () {
662
659
rows , err := dbt .db .Query ("SELECT * FROM test" )
663
660
if err != nil {
@@ -744,7 +741,9 @@ func TestLoadData(t *testing.T) {
744
741
}
745
742
746
743
func TestStrict (t * testing.T ) {
747
- runTests (t , "TestStrict" , func (dbt * DBTest ) {
744
+ // ALLOW_INVALID_DATES to get rid of stricter modes - we want to test for warnings, not errors
745
+ relaxedDsn := dsn + "&sql_mode=ALLOW_INVALID_DATES"
746
+ runTests (t , "TestStrict" , relaxedDsn , func (dbt * DBTest ) {
748
747
dbt .mustExec ("CREATE TABLE test (a TINYINT NOT NULL, b CHAR(4))" )
749
748
750
749
var queries = [... ]struct {
@@ -811,7 +810,7 @@ func TestStrict(t *testing.T) {
811
810
// Special cases
812
811
813
812
func TestRowsClose (t * testing.T ) {
814
- runTests (t , "TestRowsClose" , func (dbt * DBTest ) {
813
+ runTests (t , "TestRowsClose" , dsn , func (dbt * DBTest ) {
815
814
rows , err := dbt .db .Query ("SELECT 1" )
816
815
if err != nil {
817
816
dbt .Fatal (err )
@@ -836,7 +835,7 @@ func TestRowsClose(t *testing.T) {
836
835
// dangling statements
837
836
// http://code.google.com/p/go/issues/detail?id=3865
838
837
func TestCloseStmtBeforeRows (t * testing.T ) {
839
- runTests (t , "TestCloseStmtBeforeRows" , func (dbt * DBTest ) {
838
+ runTests (t , "TestCloseStmtBeforeRows" , dsn , func (dbt * DBTest ) {
840
839
stmt , err := dbt .db .Prepare ("SELECT 1" )
841
840
if err != nil {
842
841
dbt .Fatal (err )
@@ -877,7 +876,7 @@ func TestCloseStmtBeforeRows(t *testing.T) {
877
876
// It is valid to have multiple Rows for the same Stmt
878
877
// http://code.google.com/p/go/issues/detail?id=3734
879
878
func TestStmtMultiRows (t * testing.T ) {
880
- runTests (t , "TestStmtMultiRows" , func (dbt * DBTest ) {
879
+ runTests (t , "TestStmtMultiRows" , dsn , func (dbt * DBTest ) {
881
880
stmt , err := dbt .db .Prepare ("SELECT 1 UNION SELECT 0" )
882
881
if err != nil {
883
882
dbt .Fatal (err )
@@ -992,7 +991,7 @@ func TestConcurrent(t *testing.T) {
992
991
t .Log ("CONCURRENT env var not set. Skipping TestConcurrent" )
993
992
return
994
993
}
995
- runTests (t , "TestConcurrent" , func (dbt * DBTest ) {
994
+ runTests (t , "TestConcurrent" , dsn , func (dbt * DBTest ) {
996
995
var max int
997
996
err := dbt .db .QueryRow ("SELECT @@max_connections" ).Scan (& max )
998
997
if err != nil {
0 commit comments