package
0.31.0
Repository: https://github.com/orlangure/gnomock.git
Documentation: pkg.go.dev

# README

Gnomock MySQL

Gnomock MySQL is a Gnomock preset for running tests against a real MySQL database, without mocks.

package mysql_test

import (
	"database/sql"
	"fmt"

	"github.com/orlangure/gnomock"
	"github.com/orlangure/gnomock/preset/mysql"
)

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 := mysql.Preset(
		mysql.WithUser("Sherlock", "Holmes"),
		mysql.WithDatabase("books"),
		mysql.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
}

# Functions

Preset creates a new Gmomock MySQL preset.
WithDatabase creates a database with the provided name in the container.
WithQueries executes the provided queries against the database created with WithDatabase, or against default "mydb" database.
WithQueriesFile sets a file name to read initial queries from.
WithUser creates a new superuser with the provided credentials in the container.
WithVersion sets image version.

# Structs

P is a Gnomock Preset implementation of MySQL database.

# Type aliases

Option is an optional configuration of this Gnomock preset.