Browse Source

Merge pull request #43 from arnehormann/master

add tests for charset parameter on live connection
Julien Schmidt 12 years ago
parent
commit
1be09eb993
2 changed files with 61 additions and 3 deletions
  1. 10 2
      connection.go
  2. 51 1
      driver_test.go

+ 10 - 2
connection.go

@@ -48,11 +48,19 @@ func (mc *mysqlConn) handleParams() (err error) {
 		case "charset":
 			charsets := strings.Split(val, ",")
 			for _, charset := range charsets {
+				// ignore errors here - a charset may not exist
 				err = mc.exec("SET NAMES " + charset)
-				if err != nil {
-					return
+				if err == nil {
+					var value []byte
+					value, err = mc.getSystemVar("character_set_connection")
+					if string(value) == charset {
+						break
+					}
 				}
 			}
+			if err != nil {
+				return
+			}
 
 		// handled elsewhere
 		case "timeout", "allowAllFiles":

+ 51 - 1
driver_test.go

@@ -13,6 +13,7 @@ import (
 )
 
 var (
+	charset string
 	dsn     string
 	netAddr string
 	run     bool
@@ -44,8 +45,9 @@ func getEnv() bool {
 			dbname = "gotest"
 		}
 
+		charset = "charset=utf8"
 		netAddr = fmt.Sprintf("%s(%s)", prot, addr)
-		dsn = fmt.Sprintf("%s:%s@%s/%s?timeout=30s&charset=utf8", user, pass, netAddr, dbname)
+		dsn = fmt.Sprintf("%s:%s@%s/%s?timeout=30s&"+charset, user, pass, netAddr, dbname)
 
 		c, err := net.Dial(prot, addr)
 		if err == nil {
@@ -79,6 +81,54 @@ func mustQuery(t *testing.T, db *sql.DB, query string, args ...interface{}) (row
 	return
 }
 
+func mustSetCharset(t *testing.T, charsetParam, expected string) {
+	db, err := sql.Open("mysql", strings.Replace(dsn, charset, charsetParam, 1))
+	if err != nil {
+		t.Fatalf("Error connecting: %v", err)
+	}
+
+	rows := mustQuery(t, db, ("SELECT @@character_set_connection"))
+	if !rows.Next() {
+		t.Fatalf("Error getting connection charset: %v", err)
+	}
+
+	var got string
+	rows.Scan(&got)
+
+	if got != expected {
+		t.Fatalf("Expected connection charset %s but got %s", expected, got)
+	}
+	db.Close()
+}
+
+func TestCharset(t *testing.T) {
+	if !getEnv() {
+		t.Logf("MySQL-Server not running on %s. Skipping TestCharset", netAddr)
+		return
+	}
+
+	// non utf8 test
+	mustSetCharset(t, "charset=ascii", "ascii")
+}
+
+func TestFallbackCharset(t *testing.T) {
+	if !getEnv() {
+		t.Logf("MySQL-Server not running on %s. Skipping TestFallbackCharset", netAddr)
+		return
+	}
+
+	// when the first charset is invalid, use the second
+	mustSetCharset(t, "charset=none,utf8", "utf8")
+
+	// when the first charset is valid, use it
+	charsets := []string{"ascii", "utf8"}
+	for i := range charsets {
+		expected := charsets[i]
+		other := charsets[1-i]
+		mustSetCharset(t, "charset="+expected+","+other, expected)
+	}
+}
+
 func TestCRUD(t *testing.T) {
 	if !getEnv() {
 		t.Logf("MySQL-Server not running on %s. Skipping TestCRUD", netAddr)