@@ -42,6 +42,55 @@ func init() {
4242 }
4343}
4444
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+
4594func TestCharset (t * testing.T ) {
4695 mustSetCharset := func (charsetParam , expected string ) {
4796 db , err := sql .Open ("mysql" , strings .Replace (dsn , charset , charsetParam , 1 ))
@@ -101,57 +150,8 @@ func TestFailingCharset(t *testing.T) {
101150 }
102151}
103152
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-
153153func TestRawBytesResultExceedsBuffer (t * testing.T ) {
154- runTests (t , "TestRawBytesResultExceedsBuffer" , func (dbt * DBTest ) {
154+ runTests (t , "TestRawBytesResultExceedsBuffer" , dsn , func (dbt * DBTest ) {
155155 // defaultBufSize from buffer.go
156156 expected := strings .Repeat ("abc" , defaultBufSize )
157157 rows := dbt .mustQuery ("SELECT '" + expected + "'" )
@@ -168,7 +168,7 @@ func TestRawBytesResultExceedsBuffer(t *testing.T) {
168168}
169169
170170func TestCRUD (t * testing.T ) {
171- runTests (t , "TestCRUD" , func (dbt * DBTest ) {
171+ runTests (t , "TestCRUD" , dsn , func (dbt * DBTest ) {
172172 // Create Table
173173 dbt .mustExec ("CREATE TABLE test (value BOOL)" )
174174
@@ -260,7 +260,7 @@ func TestCRUD(t *testing.T) {
260260}
261261
262262func TestInt (t * testing.T ) {
263- runTests (t , "TestInt" , func (dbt * DBTest ) {
263+ runTests (t , "TestInt" , dsn , func (dbt * DBTest ) {
264264 types := [5 ]string {"TINYINT" , "SMALLINT" , "MEDIUMINT" , "INT" , "BIGINT" }
265265 in := int64 (42 )
266266 var out int64
@@ -307,7 +307,7 @@ func TestInt(t *testing.T) {
307307}
308308
309309func TestFloat (t * testing.T ) {
310- runTests (t , "TestFloat" , func (dbt * DBTest ) {
310+ runTests (t , "TestFloat" , dsn , func (dbt * DBTest ) {
311311 types := [2 ]string {"FLOAT" , "DOUBLE" }
312312 in := float32 (42.23 )
313313 var out float32
@@ -330,7 +330,7 @@ func TestFloat(t *testing.T) {
330330}
331331
332332func TestString (t * testing.T ) {
333- runTests (t , "TestString" , func (dbt * DBTest ) {
333+ runTests (t , "TestString" , dsn , func (dbt * DBTest ) {
334334 types := [6 ]string {"CHAR(255)" , "VARCHAR(255)" , "TINYTEXT" , "TEXT" , "MEDIUMTEXT" , "LONGTEXT" }
335335 in := "κόσμε üöäßñóùéàâÿœ'îë Árvíztűrő いろはにほへとちりぬるを イロハニホヘト דג סקרן чащах น่าฟังเอย"
336336 var out string
@@ -470,18 +470,15 @@ func TestDateTime(t *testing.T) {
470470 }
471471 }
472472
473- oldDsn := dsn
474- usedDsn := oldDsn + "&sql_mode=ALLOW_INVALID_DATES"
473+ timeDsn := dsn + "&sql_mode=ALLOW_INVALID_DATES"
475474 for _ , v := range setups {
476475 s = v
477- dsn = usedDsn + s .dsnSuffix
478- runTests (t , "TestDateTime" , testTime )
476+ runTests (t , "TestDateTime" , timeDsn + s .dsnSuffix , testTime )
479477 }
480- dsn = oldDsn
481478}
482479
483480func TestNULL (t * testing.T ) {
484- runTests (t , "TestNULL" , func (dbt * DBTest ) {
481+ runTests (t , "TestNULL" , dsn , func (dbt * DBTest ) {
485482 nullStmt , err := dbt .db .Prepare ("SELECT NULL" )
486483 if err != nil {
487484 dbt .Fatal (err )
@@ -597,7 +594,7 @@ func TestNULL(t *testing.T) {
597594}
598595
599596func TestLongData (t * testing.T ) {
600- runTests (t , "TestLongData" , func (dbt * DBTest ) {
597+ runTests (t , "TestLongData" , dsn , func (dbt * DBTest ) {
601598 var maxAllowedPacketSize int
602599 err := dbt .db .QueryRow ("select @@max_allowed_packet" ).Scan (& maxAllowedPacketSize )
603600 if err != nil {
@@ -654,7 +651,7 @@ func TestLongData(t *testing.T) {
654651}
655652
656653func TestLoadData (t * testing.T ) {
657- runTests (t , "TestLoadData" , func (dbt * DBTest ) {
654+ runTests (t , "TestLoadData" , dsn , func (dbt * DBTest ) {
658655 verifyLoadDataResult := func () {
659656 rows , err := dbt .db .Query ("SELECT * FROM test" )
660657 if err != nil {
@@ -741,7 +738,9 @@ func TestLoadData(t *testing.T) {
741738}
742739
743740func 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 ) {
745744 dbt .mustExec ("CREATE TABLE test (a TINYINT NOT NULL, b CHAR(4))" )
746745
747746 var queries = [... ]struct {
@@ -808,7 +807,7 @@ func TestStrict(t *testing.T) {
808807// Special cases
809808
810809func TestRowsClose (t * testing.T ) {
811- runTests (t , "TestRowsClose" , func (dbt * DBTest ) {
810+ runTests (t , "TestRowsClose" , dsn , func (dbt * DBTest ) {
812811 rows , err := dbt .db .Query ("SELECT 1" )
813812 if err != nil {
814813 dbt .Fatal (err )
@@ -833,7 +832,7 @@ func TestRowsClose(t *testing.T) {
833832// dangling statements
834833// http://code.google.com/p/go/issues/detail?id=3865
835834func TestCloseStmtBeforeRows (t * testing.T ) {
836- runTests (t , "TestCloseStmtBeforeRows" , func (dbt * DBTest ) {
835+ runTests (t , "TestCloseStmtBeforeRows" , dsn , func (dbt * DBTest ) {
837836 stmt , err := dbt .db .Prepare ("SELECT 1" )
838837 if err != nil {
839838 dbt .Fatal (err )
@@ -874,7 +873,7 @@ func TestCloseStmtBeforeRows(t *testing.T) {
874873// It is valid to have multiple Rows for the same Stmt
875874// http://code.google.com/p/go/issues/detail?id=3734
876875func TestStmtMultiRows (t * testing.T ) {
877- runTests (t , "TestStmtMultiRows" , func (dbt * DBTest ) {
876+ runTests (t , "TestStmtMultiRows" , dsn , func (dbt * DBTest ) {
878877 stmt , err := dbt .db .Prepare ("SELECT 1 UNION SELECT 0" )
879878 if err != nil {
880879 dbt .Fatal (err )
@@ -989,7 +988,7 @@ func TestConcurrent(t *testing.T) {
989988 t .Log ("CONCURRENT env var not set. Skipping TestConcurrent" )
990989 return
991990 }
992- runTests (t , "TestConcurrent" , func (dbt * DBTest ) {
991+ runTests (t , "TestConcurrent" , dsn , func (dbt * DBTest ) {
993992 var max int
994993 err := dbt .db .QueryRow ("SELECT @@max_connections" ).Scan (& max )
995994 if err != nil {
0 commit comments