From 7922596c70dff05f8f5ebf0164f2208081a5ea7b Mon Sep 17 00:00:00 2001 From: "Johnsoy.zhao" <15520754767@163.com> Date: Fri, 23 May 2025 23:55:22 +0800 Subject: [PATCH 1/5] test: optimize database connection test logic Add a 1-second wait time before each test to ensure the database has started Update error assertions to match more specific error messages Adjust the test order: first test the case where the user does not exist, then test the case where the password is incorrect --- _example/main_test.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/_example/main_test.go b/_example/main_test.go index 971faccb51..34167d7b5d 100644 --- a/_example/main_test.go +++ b/_example/main_test.go @@ -17,11 +17,12 @@ package main import ( "database/sql" "fmt" + "github.com/gocraft/dbr/v2" "net" "testing" + "time" _ "github.com/go-sql-driver/mysql" - "github.com/gocraft/dbr/v2" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -40,6 +41,8 @@ func TestExampleUsersDisabled(t *testing.T) { main() }() + // Wait for the database to start + time.Sleep(1 * time.Second) conn, err := dbr.Open("mysql", fmt.Sprintf("no_user:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) require.NoError(t, conn.Ping()) @@ -57,10 +60,10 @@ func TestExampleRootUserEnabled(t *testing.T) { go func() { main() }() - + time.Sleep(1 * time.Second) conn, err := dbr.Open("mysql", fmt.Sprintf("no_user:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) - require.ErrorContains(t, conn.Ping(), "User not found") + require.ErrorContains(t, conn.Ping(), "No authentication methods available for authentication") conn, err = dbr.Open("mysql", fmt.Sprintf("root:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) require.NoError(t, conn.Ping()) @@ -78,13 +81,13 @@ func TestExampleLoadedUser(t *testing.T) { go func() { main() }() - + time.Sleep(1 * time.Second) conn, err := dbr.Open("mysql", fmt.Sprintf("no_user:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) - require.ErrorContains(t, conn.Ping(), "User not found") + require.ErrorContains(t, conn.Ping(), "No authentication methods available for authentication") conn, err = dbr.Open("mysql", fmt.Sprintf("root:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) - require.ErrorContains(t, conn.Ping(), "User not found") + require.ErrorContains(t, conn.Ping(), "No authentication methods available for authentication") conn, err = dbr.Open("mysql", fmt.Sprintf("gms_user:123456@tcp(%s:%d)/%s?allowCleartextPasswords=true", address, port, dbName), nil) require.NoError(t, err) @@ -103,7 +106,8 @@ func TestIssue1621(t *testing.T) { go func() { main() }() - + // Wait for the database to start + time.Sleep(1 * time.Second) conn, err := dbr.Open("mysql", fmt.Sprintf("root:@tcp(localhost:%d)/mydb", port), nil) require.NoError(t, err) From 742dc2ad908243a5fed98c91a044a9f0a3d44cc8 Mon Sep 17 00:00:00 2001 From: "Johnsoy.zhao" <15520754767@163.com> Date: Sat, 24 May 2025 00:16:53 +0800 Subject: [PATCH 2/5] refactor(_example): Adjust code import order - Adjust the import order of "github.com/gocraft/dbr/v2" to before "github.com/stretchr/testify/assert" - This change does not add or remove any functionality, it just reorganizes the order of import statements --- _example/main_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_example/main_test.go b/_example/main_test.go index 34167d7b5d..4ca70bacfa 100644 --- a/_example/main_test.go +++ b/_example/main_test.go @@ -17,12 +17,12 @@ package main import ( "database/sql" "fmt" - "github.com/gocraft/dbr/v2" "net" "testing" "time" _ "github.com/go-sql-driver/mysql" + "github.com/gocraft/dbr/v2" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) From 3d80c03c15e802959cb21ae88ae7f96a9aaff33a Mon Sep 17 00:00:00 2001 From: "Johnsoy.zhao" <15520754767@163.com> Date: Sat, 24 May 2025 01:08:26 +0800 Subject: [PATCH 3/5] =?UTF-8?q?comment=EF=BC=9AAdd=20comment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _example/main_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/_example/main_test.go b/_example/main_test.go index 4ca70bacfa..a49ce9358d 100644 --- a/_example/main_test.go +++ b/_example/main_test.go @@ -81,6 +81,7 @@ func TestExampleLoadedUser(t *testing.T) { go func() { main() }() + // Wait for the database to start time.Sleep(1 * time.Second) conn, err := dbr.Open("mysql", fmt.Sprintf("no_user:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) From c8c36db80fe7b92299303a97d35441c8f894a492 Mon Sep 17 00:00:00 2001 From: "Johnsoy.zhao" <15520754767@163.com> Date: Mon, 26 May 2025 21:49:33 +0800 Subject: [PATCH 4/5] test --- _example/main_test.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/_example/main_test.go b/_example/main_test.go index a49ce9358d..abec5cca54 100644 --- a/_example/main_test.go +++ b/_example/main_test.go @@ -60,10 +60,8 @@ func TestExampleRootUserEnabled(t *testing.T) { go func() { main() }() - time.Sleep(1 * time.Second) conn, err := dbr.Open("mysql", fmt.Sprintf("no_user:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) - require.ErrorContains(t, conn.Ping(), "No authentication methods available for authentication") conn, err = dbr.Open("mysql", fmt.Sprintf("root:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) require.NoError(t, conn.Ping()) @@ -81,14 +79,10 @@ func TestExampleLoadedUser(t *testing.T) { go func() { main() }() - // Wait for the database to start - time.Sleep(1 * time.Second) conn, err := dbr.Open("mysql", fmt.Sprintf("no_user:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) - require.ErrorContains(t, conn.Ping(), "No authentication methods available for authentication") conn, err = dbr.Open("mysql", fmt.Sprintf("root:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) - require.ErrorContains(t, conn.Ping(), "No authentication methods available for authentication") conn, err = dbr.Open("mysql", fmt.Sprintf("gms_user:123456@tcp(%s:%d)/%s?allowCleartextPasswords=true", address, port, dbName), nil) require.NoError(t, err) @@ -107,8 +101,6 @@ func TestIssue1621(t *testing.T) { go func() { main() }() - // Wait for the database to start - time.Sleep(1 * time.Second) conn, err := dbr.Open("mysql", fmt.Sprintf("root:@tcp(localhost:%d)/mydb", port), nil) require.NoError(t, err) From f3358a9a6ebc961229cbc63e5ad396570046178d Mon Sep 17 00:00:00 2001 From: "Johnsoy.zhao" <15520754767@163.com> Date: Mon, 2 Jun 2025 22:46:49 +0800 Subject: [PATCH 5/5] revert: revert delete test code --- _example/main_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/_example/main_test.go b/_example/main_test.go index abec5cca54..3f5c8f5cb0 100644 --- a/_example/main_test.go +++ b/_example/main_test.go @@ -62,6 +62,7 @@ func TestExampleRootUserEnabled(t *testing.T) { }() conn, err := dbr.Open("mysql", fmt.Sprintf("no_user:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) + require.ErrorContains(t, conn.Ping(), "User not found") conn, err = dbr.Open("mysql", fmt.Sprintf("root:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) require.NoError(t, conn.Ping()) @@ -81,8 +82,10 @@ func TestExampleLoadedUser(t *testing.T) { }() conn, err := dbr.Open("mysql", fmt.Sprintf("no_user:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) + require.ErrorContains(t, conn.Ping(), "User not found") conn, err = dbr.Open("mysql", fmt.Sprintf("root:@tcp(%s:%d)/%s", address, port, dbName), nil) require.NoError(t, err) + require.ErrorContains(t, conn.Ping(), "User not found") conn, err = dbr.Open("mysql", fmt.Sprintf("gms_user:123456@tcp(%s:%d)/%s?allowCleartextPasswords=true", address, port, dbName), nil) require.NoError(t, err)