@@ -52,6 +52,55 @@ func init() {
5252 }
5353}
5454
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+
55104func TestCharset (t * testing.T ) {
56105 mustSetCharset := func (charsetParam , expected string ) {
57106 db , err := sql .Open ("mysql" , strings .Replace (dsn , charset , charsetParam , 1 ))
@@ -111,57 +160,8 @@ func TestFailingCharset(t *testing.T) {
111160 }
112161}
113162
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-
163163func TestRawBytesResultExceedsBuffer (t * testing.T ) {
164- runTests (t , "TestRawBytesResultExceedsBuffer" , func (dbt * DBTest ) {
164+ runTests (t , "TestRawBytesResultExceedsBuffer" , dsn , func (dbt * DBTest ) {
165165 // defaultBufSize from buffer.go
166166 expected := strings .Repeat ("abc" , defaultBufSize )
167167 rows := dbt .mustQuery ("SELECT '" + expected + "'" )
@@ -178,7 +178,7 @@ func TestRawBytesResultExceedsBuffer(t *testing.T) {
178178}
179179
180180func TestCRUD (t * testing.T ) {
181- runTests (t , "TestCRUD" , func (dbt * DBTest ) {
181+ runTests (t , "TestCRUD" , dsn , func (dbt * DBTest ) {
182182 // Create Table
183183 dbt .mustExec ("CREATE TABLE test (value BOOL)" )
184184
@@ -270,7 +270,7 @@ func TestCRUD(t *testing.T) {
270270}
271271
272272func TestInt (t * testing.T ) {
273- runTests (t , "TestInt" , func (dbt * DBTest ) {
273+ runTests (t , "TestInt" , dsn , func (dbt * DBTest ) {
274274 types := [5 ]string {"TINYINT" , "SMALLINT" , "MEDIUMINT" , "INT" , "BIGINT" }
275275 in := int64 (42 )
276276 var out int64
@@ -317,7 +317,7 @@ func TestInt(t *testing.T) {
317317}
318318
319319func TestFloat (t * testing.T ) {
320- runTests (t , "TestFloat" , func (dbt * DBTest ) {
320+ runTests (t , "TestFloat" , dsn , func (dbt * DBTest ) {
321321 types := [2 ]string {"FLOAT" , "DOUBLE" }
322322 in := float32 (42.23 )
323323 var out float32
@@ -340,7 +340,7 @@ func TestFloat(t *testing.T) {
340340}
341341
342342func TestString (t * testing.T ) {
343- runTests (t , "TestString" , func (dbt * DBTest ) {
343+ runTests (t , "TestString" , dsn , func (dbt * DBTest ) {
344344 types := [6 ]string {"CHAR(255)" , "VARCHAR(255)" , "TINYTEXT" , "TEXT" , "MEDIUMTEXT" , "LONGTEXT" }
345345 in := "κόσμε üöäßñóùéàâÿœ'îë Árvíztűrő いろはにほへとちりぬるを イロハニホヘト דג סקרן чащах น่าฟังเอย"
346346 var out string
@@ -473,18 +473,15 @@ func TestDateTime(t *testing.T) {
473473 }
474474 }
475475
476- oldDsn := dsn
477- usedDsn := oldDsn + "&sql_mode=ALLOW_INVALID_DATES"
476+ timeDsn := dsn + "&sql_mode=ALLOW_INVALID_DATES"
478477 for _ , v := range setups {
479478 s = v
480- dsn = usedDsn + s .dsnSuffix
481- runTests (t , "TestDateTime" , testTime )
479+ runTests (t , "TestDateTime" , timeDsn + s .dsnSuffix , testTime )
482480 }
483- dsn = oldDsn
484481}
485482
486483func TestNULL (t * testing.T ) {
487- runTests (t , "TestNULL" , func (dbt * DBTest ) {
484+ runTests (t , "TestNULL" , dsn , func (dbt * DBTest ) {
488485 nullStmt , err := dbt .db .Prepare ("SELECT NULL" )
489486 if err != nil {
490487 dbt .Fatal (err )
@@ -600,7 +597,7 @@ func TestNULL(t *testing.T) {
600597}
601598
602599func TestLongData (t * testing.T ) {
603- runTests (t , "TestLongData" , func (dbt * DBTest ) {
600+ runTests (t , "TestLongData" , dsn , func (dbt * DBTest ) {
604601 var maxAllowedPacketSize int
605602 err := dbt .db .QueryRow ("select @@max_allowed_packet" ).Scan (& maxAllowedPacketSize )
606603 if err != nil {
@@ -657,7 +654,7 @@ func TestLongData(t *testing.T) {
657654}
658655
659656func TestLoadData (t * testing.T ) {
660- runTests (t , "TestLoadData" , func (dbt * DBTest ) {
657+ runTests (t , "TestLoadData" , dsn , func (dbt * DBTest ) {
661658 verifyLoadDataResult := func () {
662659 rows , err := dbt .db .Query ("SELECT * FROM test" )
663660 if err != nil {
@@ -744,7 +741,9 @@ func TestLoadData(t *testing.T) {
744741}
745742
746743func 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 ) {
748747 dbt .mustExec ("CREATE TABLE test (a TINYINT NOT NULL, b CHAR(4))" )
749748
750749 var queries = [... ]struct {
@@ -811,7 +810,7 @@ func TestStrict(t *testing.T) {
811810// Special cases
812811
813812func TestRowsClose (t * testing.T ) {
814- runTests (t , "TestRowsClose" , func (dbt * DBTest ) {
813+ runTests (t , "TestRowsClose" , dsn , func (dbt * DBTest ) {
815814 rows , err := dbt .db .Query ("SELECT 1" )
816815 if err != nil {
817816 dbt .Fatal (err )
@@ -836,7 +835,7 @@ func TestRowsClose(t *testing.T) {
836835// dangling statements
837836// http://code.google.com/p/go/issues/detail?id=3865
838837func TestCloseStmtBeforeRows (t * testing.T ) {
839- runTests (t , "TestCloseStmtBeforeRows" , func (dbt * DBTest ) {
838+ runTests (t , "TestCloseStmtBeforeRows" , dsn , func (dbt * DBTest ) {
840839 stmt , err := dbt .db .Prepare ("SELECT 1" )
841840 if err != nil {
842841 dbt .Fatal (err )
@@ -877,7 +876,7 @@ func TestCloseStmtBeforeRows(t *testing.T) {
877876// It is valid to have multiple Rows for the same Stmt
878877// http://code.google.com/p/go/issues/detail?id=3734
879878func TestStmtMultiRows (t * testing.T ) {
880- runTests (t , "TestStmtMultiRows" , func (dbt * DBTest ) {
879+ runTests (t , "TestStmtMultiRows" , dsn , func (dbt * DBTest ) {
881880 stmt , err := dbt .db .Prepare ("SELECT 1 UNION SELECT 0" )
882881 if err != nil {
883882 dbt .Fatal (err )
@@ -992,7 +991,7 @@ func TestConcurrent(t *testing.T) {
992991 t .Log ("CONCURRENT env var not set. Skipping TestConcurrent" )
993992 return
994993 }
995- runTests (t , "TestConcurrent" , func (dbt * DBTest ) {
994+ runTests (t , "TestConcurrent" , dsn , func (dbt * DBTest ) {
996995 var max int
997996 err := dbt .db .QueryRow ("SELECT @@max_connections" ).Scan (& max )
998997 if err != nil {
0 commit comments