cockroachdb

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2024 License: MIT Imports: 7 Imported by: 1

README

Gnomock CockroachDB

Gnomock CockroachDB is a Gnomock preset for running tests against a real CockroachDB container, without mocks.

package cockroachdb_test

import (
	"database/sql"
	"fmt"
	"testing"

	"github.com/orlangure/gnomock"
	"github.com/orlangure/gnomock/preset/cockroachdb"
	"github.com/stretchr/testify/require"
)

func TestPreset(t *testing.T) {
	t.Parallel()

	for _, version := range []string{"v19.2.11", "v20.1.10"} {
		t.Run(version, testPreset(version))
	}
}

func testPreset(version string) func(t *testing.T) {
	return func(t *testing.T) {
		queries := `
			insert into t (a) values (1);
			insert into t (a) values (2);
		`
		query := `insert into t (a) values (3);`
		p := cockroachdb.Preset(
			cockroachdb.WithDatabase("gnomock"),
			cockroachdb.WithQueries(queries, query),
			cockroachdb.WithQueriesFile("./testdata/queries.sql"),
			cockroachdb.WithVersion(version),
		)

		container, err := gnomock.Start(p)
		require.NoError(t, err)

		defer func() { require.NoError(t, gnomock.Stop(container)) }()

		connStr := fmt.Sprintf(
			"host=%s port=%d user=root dbname=%s sslmode=disable",
			container.Host, container.DefaultPort(), "gnomock",
		)

		db, err := sql.Open("postgres", connStr)
		require.NoError(t, err)

		var max, avg, min, count float64

		rows := db.QueryRow("select max(a), avg(a), min(a), count(a) from t")
		require.NoError(t, rows.Scan(&max, &avg, &min, &count))

		require.Equal(t, float64(3), max)
		require.Equal(t, float64(2), avg)
		require.Equal(t, float64(1), min)
		require.Equal(t, float64(3), count)
	}
}

func TestPreset_withDefaults(t *testing.T) {
	t.Parallel()

	container, err := gnomock.Start(cockroachdb.Preset())

	defer func() { require.NoError(t, gnomock.Stop(container)) }()

	require.NoError(t, err)

	connStr := fmt.Sprintf(
		"host=%s port=%d user=root dbname=%s sslmode=disable",
		container.Host, container.DefaultPort(), "mydb",
	)

	db, err := sql.Open("postgres", connStr)
	require.NoError(t, err)
	require.NoError(t, db.Close())
}

func TestPreset_wrongQueriesFile(t *testing.T) {
	t.Parallel()

	p := cockroachdb.Preset(
		cockroachdb.WithQueriesFile("./invalid"),
	)
	c, err := gnomock.Start(p)
	require.Error(t, err)
	require.Contains(t, err.Error(), "can't read queries file")
	require.NoError(t, gnomock.Stop(c))
}

Documentation

Overview

Package cockroachdb includes CockroachDB implementation of Gnomock Preset interface. This Preset can be passed to gnomock.Start() function to create a configured CockroachDB container to use in tests.

Containers created with this preset use `root` user without a password for authentication. There is currently no way to setup an initial user at create time.

By default, a new database "mydb" is created, and all the provided queries are executed against it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Preset

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

Preset creates a new Gmomock CockroachDB preset. This preset includes a CockroachDB specific healthcheck function and default CockroachDB image and port.

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

func WithDatabase(db string) Option

WithDatabase creates a database with the provided name in the container. 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 the default 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 {
	Version      string   `json:"version"`
	DB           string   `json:"db"`
	Queries      []string `json:"queries"`
	QueriesFiles []string `json:"queries_files"`
}

P is a Gnomock Preset implementation for CockroachDB.

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
y or Y : Canonical URL