mongotest

package
v0.1.67 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package mongotest runs a store's test suite against MongoDB.

It is the document-store counterpart to sqlxtest. MongoDB stays hand-written rather than sharing an implementation with the SQL backends, which makes it the one place where a behaviour can drift unnoticed: most domains ship a MongoDB store with no test that touches a database. A suite written against the domain's Store interface can run on every backend, so a domain's behaviour is asserted once and checked everywhere.

MongoDB runs only when MONGO_TEST_DSN names a reachable server; otherwise the subtest skips. The variable is deliberately not MONGODB_URL — a suite that creates and drops databases should take a separate, explicit opt-in.

Index

Constants

View Source
const DSNEnv = "MONGO_TEST_DSN"

DSNEnv names the environment variable holding the test MongoDB connection string.

Variables

This section is empty.

Functions

func DatabaseName

func DatabaseName(testName string, counter uint64) string

DatabaseName builds a unique database name for a test.

The counter only separates subtests inside one process, and `go test ./...` runs packages in parallel — two packages that happen to share a test name would otherwise create *and drop* the same database. The pid separates them.

MongoDB rejects names of 64 bytes or more, so the test name is bounded rather than concatenated whole: a nested subtest name easily runs past the limit on its own.

func New

func New(t *testing.T) *mongo.Database

New returns an empty MongoDB database for one test, or nil after skipping when no test server is configured.

func Run

func Run(t *testing.T, fn func(t *testing.T, db *mongo.Database))

Run executes fn against an empty MongoDB database that is dropped afterwards, as a subtest named "mongodb". It skips when no server is configured.

Types

This section is empty.

Jump to

Keyboard shortcuts

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