Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*config)
Option configures the MySQL test setup.
func ModifyConfig ¶
ModifyConfig applies a modification function to the underlying MySQL configuration created by mysql.NewConfig(). Use this to customize connection settings like timeouts or protocol.
Note: Some configuration fields will be overridden by SetupDatabase:
- Addr is overridden with values from HostEnv and PortEnv environment variables
- User and Passwd are overridden with RootUserEnv and RootPasswordEnv for root connections, or with randomly generated values for test user connections
- DBName is overridden with a randomly generated database name for test connections
Example ¶
package main
import (
"time"
"github.com/cybozu-go/mysqltest"
"github.com/go-sql-driver/mysql"
)
func main() {
mysqltest.ModifyConfig(func(c *mysql.Config) {
c.Net = "tcp"
c.MultiStatements = true
c.Timeout = 30 * time.Second
c.ReadTimeout = 10 * time.Second
c.WriteTimeout = 10 * time.Second
})
}
Output:
func PreserveTestDB ¶
func PreserveTestDB() Option
PreserveTestDB controls whether the test database and user are preserved after test completion. By default, the test database and user are automatically cleaned up when the test finishes. When this option is specified, the database and user will remain in MySQL for debugging or manual inspection.
func Queries ¶
Queries sets multiple SQL queries to be executed after database setup.
Note: If any of your queries contain multiple statements separated by semicolons, you must enable MultiStatements in the MySQL configuration:
db := mysqltest.SetupDatabase(t,
mysqltest.ModifyConfig(func(cfg *mysql.Config) {
cfg.MultiStatements = true
}),
mysqltest.Queries(
"CREATE TABLE t1 (id INT); INSERT INTO t1 VALUES (1);",
"CREATE TABLE t2 (name VARCHAR(50))",
))
func Query ¶
Query sets a single SQL query to be executed after database setup.
Note: If your query contains multiple statements separated by semicolons, you must enable MultiStatements in the MySQL configuration:
db := mysqltest.SetupDatabase(t,
mysqltest.ModifyConfig(func(cfg *mysql.Config) {
cfg.MultiStatements = true
}),
mysqltest.Query("CREATE TABLE t1 (id INT); INSERT INTO t1 VALUES (1);"))
func RootUserCredentials ¶
RootUserCredentials sets the root user credentials for MySQL connection. If not specified, the default credentials are "root"/"root".