inmemorymongo

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: AGPL-3.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectToInMemoryDB

func ConnectToInMemoryDB(mongoURI string) (*mongo.Client, error)

ConnectToInMemoryDB connects to the in-memory MongoDB instance

func CreateInMemoryMongoDB

func CreateInMemoryMongoDB(opts ...Option) (*memongo.Server, error)

CreateInMemoryMongoDB creates and starts an in-memory MongoDB instance

func Main

func Main(m *testing.M)

Main runs the package's tests, then drops the templates and stops the servers this binary started. NewPostgresDB and NewMongoDB require it (or Run):

func TestMain(m *testing.M) { inmemorymongo.Main(m) }

Ryuk (PostgreSQL) and memongo's watcher (mongod) still reap the servers if the binary is killed before Main gets there.

func MongoURI

func MongoURI() string

MongoURI is the URI of the server NewMongoDB uses ("" before its first call), for code that dials by itself; pair it with the database's Name().

func NewMongoDB

func NewMongoDB(t testingT) *mongo.Database

NewMongoDB returns an empty database, for this test only, on the test binary's one in-memory MongoDB; it is dropped when the test ends. Tests that need a replica set or test commands keep CreateInMemoryMongoDB(opts...).

func Run

func Run(m *testing.M) int

Run is Main for a TestMain with its own setup and teardown around it: it runs the tests, tears the servers down and returns the exit code.

func RunRepoSuite

func RunRepoSuite[T any](t *testing.T, suite RepoSuite[T], backends map[string]func(t *testing.T) T)

RunRepoSuite runs suite once per entry in backends, as a subtest named after its map key (conventionally "mongo" / "postgres"). Use this so a new repository is proven to behave identically on both backends from day one, rather than trusting an interface without exercising the second implementation.

Types

type Option

type Option func(*memongo.Options) error

Option changes how CreateInMemoryMongoDB starts the server.

func WithReplicaSet

func WithReplicaSet() Option

WithReplicaSet starts a single-member replica set: what transactions need.

func WithTestCommands

func WithTestCommands() Option

WithTestCommands starts a replica set with enableTestCommands, so configureFailPoint works. memongo cannot pass mongod arguments, so it runs a wrapper script around the real binary.

shortcut: the wrapper's temp dir outlives the server (no test handle here), upgrade if it ever matters.

type PostgresContainer

type PostgresContainer struct {

	// DSN is the connection string for this database, in the same
	// postgres://user:pass@host:port/db?sslmode=... form db.buildPostgresConfig
	// assembles in production, so a test can Setenv POSTGRES_URI to it
	// directly.
	DSN string
	// Pool is a ready pgxpool.Pool already connected to DSN.
	Pool *pgxpool.Pool
	// contains filtered or unexported fields
}

PostgresContainer is a throwaway PostgreSQL instance for a test, mirroring CreateInMemoryMongoDB/ConnectToInMemoryDB's shape: a service test gets a ready-to-use database in one line and tears it down with Terminate.

func NewPostgresDB

func NewPostgresDB(t testingT, key string, setup func(ctx context.Context, pool *pgxpool.Pool) error) *PostgresContainer

NewPostgresDB returns a database for this test only, cloned from a template that setup (typically the service's migrations) builds once per key and per test binary. Its DSN points at that database alone; it is dropped when the test ends. Without Docker or POSTGRES_TEST_DSN it skips, unless CI or POSTGRES_TEST_DSN promises a database (then it fails), like RequirePostgres.

func RequirePostgres

func RequirePostgres(t testingT) *PostgresContainer

RequirePostgres is StartPostgresContainer for tests: it returns a fresh database and terminates it when the test ends.

When no PostgreSQL can be provided it SKIPS the test with a message saying what was missing, so a laptop without Docker can still run the rest of the suite. It FAILS instead whenever the environment promises a database — POSTGRES_TEST_DSN is set, or CI is (GitLab sets CI=true) — so a broken CI service can never turn these tests into silent skips.

func StartDedicatedPostgres

func StartDedicatedPostgres(ctx context.Context) (*PostgresContainer, error)

StartDedicatedPostgres starts a throwaway container for this caller alone, for a test that needs a whole server. Terminate stops it.

func StartPostgresContainer

func StartPostgresContainer(ctx context.Context) (*PostgresContainer, error)

StartPostgresContainer provides a fresh, empty PostgreSQL 18 database for a test to use, either by starting a throwaway testcontainers container (the default, requiring a local Docker daemon) or, when POSTGRES_TEST_DSN is set, by carving an isolated database for this call out of the server it points to. Callers don't need to know which mode is active — the returned PostgresContainer behaves identically either way, and Terminate cleans up whichever resource was actually created.

Without POSTGRES_TEST_DSN, the database lives on the test binary's one container (the server NewPostgresDB uses), not on a container of its own: starting one per call cost seconds each. That needs inmemorymongo.Main in the package's TestMain (it stops the container); without it each call still gets its own container. StartDedicatedPostgres still gives a test its own server.

func (*PostgresContainer) Terminate

func (c *PostgresContainer) Terminate(ctx context.Context) error

Terminate tears down whatever StartPostgresContainer created: a testcontainers container in the default local-dev path, or the per-test database and admin connection when running against an externally-provided server (POSTGRES_TEST_DSN). Safe to call on a nil receiver so a deferred cleanup doesn't need its own nil check.

type RepoSuite

type RepoSuite[T any] func(t *testing.T, newRepo func(t *testing.T) T)

RepoSuite is a repository behaviour suite written ONCE against a repository's own interface/type T, so the exact same assertions run against every backend's implementation instead of being duplicated (and silently drifting) per backend.

newRepo wires up a fresh, empty T however that backend's test needs to (e.g. dialing test_utils' in-memory Mongo vs. a StartPostgresContainer pool) and is called once per subtest.

Jump to

Keyboard shortcuts

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