README
¶
Gnomock MariaDB
Gnomock MariaDB is a Gnomock preset for running tests against a real MariaDB database, without mocks.
package mariadb_test
import (
"database/sql"
"fmt"
"github.com/orlangure/gnomock"
"github.com/orlangure/gnomock/preset/mariadb"
)
func ExamplePreset() {
queries := `
create table t(a int);
insert into t (a) values (1);
insert into t (a) values (2);
`
query := `insert into t (a) values (3);`
p := mariadb.Preset(
mariadb.WithUser("Sherlock", "Holmes"),
mariadb.WithDatabase("books"),
mariadb.WithQueries(queries, query),
)
container, err := gnomock.Start(p)
defer func() { _ = gnomock.Stop(container) }()
if err != nil {
panic(err)
}
addr := container.DefaultAddress()
connStr := fmt.Sprintf(
"%s:%s@tcp(%s)/%s",
"Sherlock", "Holmes", addr, "books",
)
db, err := sql.Open("mysql", connStr)
if err != nil {
panic(err)
}
var max, avg, min, count float64
rows := db.QueryRow("select max(a), avg(a), min(a), count(a) from t")
err = rows.Scan(&max, &avg, &min, &count)
if err != nil {
panic("can't query the database: " + err.Error())
}
fmt.Println("max", 3)
fmt.Println("avg", 2)
fmt.Println("min", 1)
fmt.Println("count", 3)
// Output:
// max 3
// avg 2
// min 1
// count 3
}
Documentation
¶
Overview ¶
Package mariadb provides a Gnomock Preset for MariaDB database
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Preset ¶
Preset creates a new Gmomock MariaDB preset. This preset includes a MariaDB specific healthcheck function, default MariaDB image and port, and allows to optionally set up initial state.
When used without any configuration, it creates a superuser `gnomock` with password `gnoria`, and `mydb` database. Default MariaDB version is 10.5.8.
Types ¶
type Option ¶
type Option func(*P)
Option is an optional configuration of this Gnomock preset. Use available Options to configure the container.
func WithDatabase ¶
WithDatabase creates a database with the provided name in the container. If not provided, "mydb" is used by default. WithQueries, if provided, runs against the new database.
func WithQueries ¶
WithQueries executes the provided queries against the database created with WithDatabase, or against default "mydb" database.
func WithQueriesFile ¶
WithQueriesFile sets a file name to read initial queries from. Queries from this file are executed before any other queries provided in WithQueries.
type P ¶
type P struct { DB string `json:"db"` User string `json:"user"` Password string `json:"password"` Queries []string `json:"queries"` QueriesFiles []string `json:"queries_files"` Version string `json:"version"` }
P is a Gnomock Preset implementation of MariaDB database.
func (*P) Ports ¶
func (p *P) Ports() gnomock.NamedPorts
Ports returns ports that should be used to access this container.