Skip to content

Commit 3840c6c

Browse files
committed
add test for invalid charset
1 parent 33793ab commit 3840c6c

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

driver_test.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ func mustQuery(t *testing.T, db *sql.DB, query string, args ...interface{}) (row
8080
return
8181
}
8282

83-
func mustSetCharset(t *testing.T, charsetParam, expected string) error {
83+
func mustSetCharset(t *testing.T, charsetParam, expected string) {
8484
db, err := sql.Open("mysql", strings.Replace(dsn, charset, charsetParam, 1))
8585
if err != nil {
86-
return err
86+
t.Fatalf("Error on Open: %v", err)
8787
}
8888

8989
rows := mustQuery(t, db, ("SELECT @@character_set_connection"))
@@ -97,7 +97,6 @@ func mustSetCharset(t *testing.T, charsetParam, expected string) error {
9797
if got != expected {
9898
t.Fatalf("Expected connection charset %s but got %s", expected, got)
9999
}
100-
db.Close()
101100
}
102101

103102
func TestCharset(t *testing.T) {
@@ -107,8 +106,16 @@ func TestCharset(t *testing.T) {
107106
}
108107

109108
// 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")
112119
}
113120
}
114121

@@ -119,22 +126,14 @@ func TestFallbackCharset(t *testing.T) {
119126
}
120127

121128
// 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")
125130

126131
// when the first charset is valid, use it
127132
charsets := []string{"ascii", "utf8"}
128133
for i := range charsets {
129134
expected := charsets[i]
130135
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)
138137
}
139138
}
140139

0 commit comments

Comments
 (0)