Skip to content

Commit 7922596

Browse files
committed
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
1 parent 3d19fa2 commit 7922596

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

_example/main_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ package main
1717
import (
1818
"database/sql"
1919
"fmt"
20+
"github.com/gocraft/dbr/v2"
2021
"net"
2122
"testing"
23+
"time"
2224

2325
_ "github.com/go-sql-driver/mysql"
24-
"github.com/gocraft/dbr/v2"
2526
"github.com/stretchr/testify/assert"
2627
"github.com/stretchr/testify/require"
2728
)
@@ -40,6 +41,8 @@ func TestExampleUsersDisabled(t *testing.T) {
4041
main()
4142
}()
4243

44+
// Wait for the database to start
45+
time.Sleep(1 * time.Second)
4346
conn, err := dbr.Open("mysql", fmt.Sprintf("no_user:@tcp(%s:%d)/%s", address, port, dbName), nil)
4447
require.NoError(t, err)
4548
require.NoError(t, conn.Ping())
@@ -57,10 +60,10 @@ func TestExampleRootUserEnabled(t *testing.T) {
5760
go func() {
5861
main()
5962
}()
60-
63+
time.Sleep(1 * time.Second)
6164
conn, err := dbr.Open("mysql", fmt.Sprintf("no_user:@tcp(%s:%d)/%s", address, port, dbName), nil)
6265
require.NoError(t, err)
63-
require.ErrorContains(t, conn.Ping(), "User not found")
66+
require.ErrorContains(t, conn.Ping(), "No authentication methods available for authentication")
6467
conn, err = dbr.Open("mysql", fmt.Sprintf("root:@tcp(%s:%d)/%s", address, port, dbName), nil)
6568
require.NoError(t, err)
6669
require.NoError(t, conn.Ping())
@@ -78,13 +81,13 @@ func TestExampleLoadedUser(t *testing.T) {
7881
go func() {
7982
main()
8083
}()
81-
84+
time.Sleep(1 * time.Second)
8285
conn, err := dbr.Open("mysql", fmt.Sprintf("no_user:@tcp(%s:%d)/%s", address, port, dbName), nil)
8386
require.NoError(t, err)
84-
require.ErrorContains(t, conn.Ping(), "User not found")
87+
require.ErrorContains(t, conn.Ping(), "No authentication methods available for authentication")
8588
conn, err = dbr.Open("mysql", fmt.Sprintf("root:@tcp(%s:%d)/%s", address, port, dbName), nil)
8689
require.NoError(t, err)
87-
require.ErrorContains(t, conn.Ping(), "User not found")
90+
require.ErrorContains(t, conn.Ping(), "No authentication methods available for authentication")
8891
conn, err = dbr.Open("mysql",
8992
fmt.Sprintf("gms_user:123456@tcp(%s:%d)/%s?allowCleartextPasswords=true", address, port, dbName), nil)
9093
require.NoError(t, err)
@@ -103,7 +106,8 @@ func TestIssue1621(t *testing.T) {
103106
go func() {
104107
main()
105108
}()
106-
109+
// Wait for the database to start
110+
time.Sleep(1 * time.Second)
107111
conn, err := dbr.Open("mysql",
108112
fmt.Sprintf("root:@tcp(localhost:%d)/mydb", port), nil)
109113
require.NoError(t, err)

0 commit comments

Comments
 (0)