mysql

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2020 License: MIT Imports: 7 Imported by: 1

README

Gnomock MySQL Build

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"
	mockmysql "github.com/orlangure/gnomock-mysql"
)

func ExampleMySQL() {
	queries := []string{
		"create table t(a int)",
		"insert into t (a) values (1)",
		"insert into t (a) values (2)",
		"insert into t (a) values (3)",
	}
	p := mockmysql.Preset(
		mockmysql.WithUser("Sherlock", "Holmes"),
		mockmysql.WithDatabase("books"),
		mockmysql.WithQueries(queries),
	)

	container, err := gnomock.StartPreset(p)

	defer func() { _ = gnomock.Stop(container) }()

	if err != nil {
		panic(err)
	}

	addr := container.Address(gnomock.DefaultPort)
	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 mysql provides a Gnomock Preset for MySQL database

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MySQL

type MySQL struct {
	// contains filtered or unexported fields
}

MySQL is a Gnomock Preset implementation for MySQL database

Example
package main

import (
	"database/sql"
	"fmt"

	"github.com/orlangure/gnomock"
	mockmysql "github.com/orlangure/gnomock-mysql"
)

func main() {
	queries := []string{
		"create table t(a int)",
		"insert into t (a) values (1)",
		"insert into t (a) values (2)",
		"insert into t (a) values (3)",
	}
	p := mockmysql.Preset(
		mockmysql.WithUser("Sherlock", "Holmes"),
		mockmysql.WithDatabase("books"),
		mockmysql.WithQueries(queries),
	)

	container, err := gnomock.StartPreset(p)

	defer func() { _ = gnomock.Stop(container) }()

	if err != nil {
		panic(err)
	}

	addr := container.Address(gnomock.DefaultPort)
	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

func Preset

func Preset(opts ...Option) *MySQL

Preset creates a new Gmomock MySQL preset. This preset includes a MySQL specific healthcheck function, default MySQL image and port, and allows to optionally set up initial state. When used without any configuration, it creates a superuser "gnomock" with password "gnomick", and "mydb" database

func (*MySQL) Image

func (p *MySQL) Image() string

Image returns an image that should be pulled to create this container

func (*MySQL) Options

func (p *MySQL) Options() []gnomock.Option

Options returns a list of options to configure this container

func (*MySQL) Ports

func (p *MySQL) Ports() gnomock.NamedPorts

Ports returns ports that should be used to access this container

type Option

type Option func(*options)

Option is an optional configuration of this Gnomock preset. Use available Options to configure the container

func WithDatabase

func WithDatabase(db string) Option

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

func WithQueries(queries []string) Option

WithQueries executes the provided queries against the database created with WithDatabase, or against default "mydb" database

func WithUser

func WithUser(user, password string) Option

WithUser creates a new superuser with the provided credentials in the container. If not used, the default credentials are gnomock:gnomick

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL