mongotest

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 is set; otherwise the subtest skips. A set but unreachable DSN fails the test rather than skipping: the variable is an explicit opt-in, and CI must not stay green while the server it started is not the one being tested. 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