@@ -80,10 +80,10 @@ func mustQuery(t *testing.T, db *sql.DB, query string, args ...interface{}) (row
80
80
return
81
81
}
82
82
83
- func mustSetCharset (t * testing.T , charsetParam , expected string ) error {
83
+ func mustSetCharset (t * testing.T , charsetParam , expected string ) {
84
84
db , err := sql .Open ("mysql" , strings .Replace (dsn , charset , charsetParam , 1 ))
85
85
if err != nil {
86
- return err
86
+ t . Fatalf ( "Error on Open: %v" , err )
87
87
}
88
88
89
89
rows := mustQuery (t , db , ("SELECT @@character_set_connection" ))
@@ -97,7 +97,6 @@ func mustSetCharset(t *testing.T, charsetParam, expected string) error {
97
97
if got != expected {
98
98
t .Fatalf ("Expected connection charset %s but got %s" , expected , got )
99
99
}
100
- db .Close ()
101
100
}
102
101
103
102
func TestCharset (t * testing.T ) {
@@ -107,8 +106,16 @@ func TestCharset(t *testing.T) {
107
106
}
108
107
109
108
// non utf8 test
110
- if err := mustSetCharset (t , "charset=ascii" , "ascii" ); err != nil {
111
- t .Fatalf ("Error connecting: %v" , err )
109
+ mustSetCharset (t , "charset=ascii" , "ascii" )
110
+ }
111
+
112
+ func TestFailingCharset (t * testing.T ) {
113
+ db , err := sql .Open ("mysql" , strings .Replace (dsn , charset , "charset=none" , 1 ))
114
+ // run query to really establish connection...
115
+ _ , err = db .Exec ("SELECT 1" )
116
+ if err == nil {
117
+ db .Close ()
118
+ t .Fatalf ("Connection must not succeed without a valid charset" )
112
119
}
113
120
}
114
121
@@ -119,22 +126,14 @@ func TestFallbackCharset(t *testing.T) {
119
126
}
120
127
121
128
// when the first charset is invalid, use the second
122
- if err := mustSetCharset (t , "charset=none,utf8" , "utf8" ); err != nil {
123
- t .Fatalf ("Error connecting: %v" , err )
124
- }
129
+ mustSetCharset (t , "charset=none,utf8" , "utf8" )
125
130
126
131
// when the first charset is valid, use it
127
132
charsets := []string {"ascii" , "utf8" }
128
133
for i := range charsets {
129
134
expected := charsets [i ]
130
135
other := charsets [1 - i ]
131
- if err := mustSetCharset (t , "charset=" + expected + "," + other , expected ); err != nil {
132
- t .Fatalf ("Error connecting: %v" , err )
133
- }
134
- }
135
-
136
- if err := mustSetCharset (t , "charset=none1,none2" , "utf8" ); err == nil {
137
- t .Fatalf ("Must throw an error if no charsets are supported" )
136
+ mustSetCharset (t , "charset=" + expected + "," + other , expected )
138
137
}
139
138
}
140
139
0 commit comments