Skip to content

[1.9] test stability improvement. #1699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
simplify TestCollation
  • Loading branch information
methane committed Apr 21, 2025
commit f19c8bbc72bc372bafb7e8c734eb3d51bb5b85e8
64 changes: 14 additions & 50 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1609,68 +1609,32 @@ func TestCollation(t *testing.T) {
t.Skipf("MySQL server not running on %s", netAddr)
}

defaultCollation := "utf8mb4_general_ci"
// MariaDB may override collation specified by handshake with `character_set_collations` variable.
// https://mariadb.com/kb/en/setting-character-sets-and-collations/#changing-default-collation
// https://mariadb.com/kb/en/server-system-variables/#character_set_collations
// utf8mb4_general_ci, utf8mb3_general_ci will be overridden by default MariaDB.
// Collations other than charasets default are not overridden. So utf8mb4_unicode_ci is safe.
testCollations := []string{
"", // do not set
defaultCollation, // driver default
"latin1_general_ci",
"binary",
"utf8mb4_unicode_ci",
"cp1257_bin",
}

for _, collation := range testCollations {
var expected, tdsn string
if collation != "" {
tdsn = dsn + "&collation=" + collation
expected = collation
} else {
tdsn = dsn
expected = defaultCollation
}

runTests(t, tdsn, func(dbt *DBTest) {
// see https://mariadb.com/kb/en/setting-character-sets-and-collations/#changing-default-collation
// when character_set_collations is set for the charset, it overrides the default collation
// so we need to check if the default collation is overridden
forceExpected := expected
var defaultCollations string
err := dbt.db.QueryRow("SELECT @@character_set_collations").Scan(&defaultCollations)
if err == nil {
// Query succeeded, need to check if we should override expected collation
collationMap := make(map[string]string)
pairs := strings.Split(defaultCollations, ",")
for _, pair := range pairs {
parts := strings.Split(pair, "=")
if len(parts) == 2 {
collationMap[parts[0]] = parts[1]
}
}
t.Run(collation, func(t *testing.T) {
tdsn := dsn + "&collation=" + collation
expected := collation

// Get charset prefix from expected collation
parts := strings.Split(expected, "_")
if len(parts) > 0 {
charset := parts[0]
if newCollation, ok := collationMap[charset]; ok {
forceExpected = newCollation
}
runTests(t, tdsn, func(dbt *DBTest) {
var got string
if err := dbt.db.QueryRow("SELECT @@collation_connection").Scan(&got); err != nil {
dbt.Fatal(err)
}
}

var got string
if err := dbt.db.QueryRow("SELECT @@collation_connection").Scan(&got); err != nil {
dbt.Fatal(err)
}

if got != expected {
if forceExpected != expected {
if got != forceExpected {
dbt.Fatalf("expected forced connection collation %s but got %s", forceExpected, got)
}
} else {
if got != expected {
dbt.Fatalf("expected connection collation %s but got %s", expected, got)
}
}
})
})
}
}
Expand Down