Version: v0.13.2 Latest Latest

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

Go to latest
Published: Apr 4, 2021 License: MIT Imports: 7 Imported by: 0


Gnomock MSSQL

Gnomock MSSQL is a Gnomock preset for running tests against a real Microsoft SQL Server database, without mocks.

package mssql_test

import (


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 := mssql.Preset(
		mssql.WithQueries(queries, query),

	container, err := gnomock.Start(p)

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

	if err != nil {

	addr := container.DefaultAddress()
	connStr := fmt.Sprintf("sqlserver://sa:Passw0rd-@%s?database=foobar", addr)

	db, err := sql.Open("sqlserver", connStr)
	if err != nil {

	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 {

	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



Package mssql provides a Gnomock Preset for Microsoft SQL Server database



This section is empty.


This section is empty.


func Preset

func Preset(opts ...Option) gnomock.Preset

Preset creates a new Gmomock Microsoft SQL Server preset. This preset includes a mssql specific healthcheck function, default mssql image and port, and allows to optionally set up initial state.

When used without any configuration, it uses `mydb` database, and `Gn0m!ck~` administrator password (user: `sa`). You must accept EULA to use this image (`WithLicense` option). By default, version `2019-latest` is used.


type Option

type Option func(*P)

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

func WithAdminPassword

func WithAdminPassword(password string) Option

WithAdminPassword sets administrator password that can be used to connect (default: Gn0m!ck~)

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 WithLicense

func WithLicense(accept bool) Option

WithLicense sets EULA acceptance state. To accept the license, use true. See for more information

func WithQueries

func WithQueries(queries ...string) Option

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

func WithQueriesFile

func WithQueriesFile(file string) Option

WithQueriesFile sets a file name to read initial queries from. Queries from this file are executed before any other queries provided in WithQueries

func WithVersion

func WithVersion(version string) Option

WithVersion sets image version.

type P

type P struct {
	DB           string   `json:"db"`
	Password     string   `json:"password"`
	Queries      []string `json:"queries"`
	QueriesFiles []string `json:"queries_files"`
	License      bool     `json:"license"`
	Version      string   `json:"version"`

P is a Gnomock Preset implementation of Microsoft SQL Server database

func (*P) Image

func (p *P) Image() string

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

func (*P) Options

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

Options returns a list of options to configure this container

func (*P) Ports

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

Ports returns ports that should be used to access this container

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL