mockdb

package module
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

README

🎭 data-mock

Go Reference Version Build Status Go Report Card Codecov

Mock Database for Testing

data-mock implements an in-memory "database" for testing purposes only, satisfying the benpate/data interface. It is useful for mocking tests instead of opening a real database connection.

Pull Requests Welcome

I'm trying to make data-mock the best it can be, and your help is greatly appreciated. If you find a bug or have an idea for a new feature, please open an issue or submit a pull request. We're all in this together! 🎭

Documentation

Overview

Package mockdb provides an in-memory implementation of the benpate/data interfaces, for use in unit tests.

A Server holds every record in a plain map of named collections, so tests can build a fixture as a struct literal and hand it to code that expects a real datastore. Sessions and Collections created from a Server all share it, which means writes made through one are immediately visible to the others.

Records are matched and sorted by reflection over their bson struct tags, so this package is SLOW by design and belongs only in tests. Query and HardDelete are not implemented, and WithTransaction provides no rollback.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatcherFunc

func MatcherFunc(object data.Object) exp.MatcherFunc

MatcherFunc is a helper function that uses reflection to look inside a generic data.Object and match it. Because it uses reflection, it should be considered SLOW, and only be used in the mock library.

Types

type Collection

type Collection struct {
	// contains filtered or unexported fields
}

Collection is a mock database collection

func (Collection) Context

func (collection Collection) Context() context.Context

Context returns the context for this collection

func (Collection) Count added in v0.28.0

func (collection Collection) Count(criteria exp.Expression, _ ...option.Option) (int64, error)

Count returns the number of records in the mock collection that match the criteria.

func (Collection) Delete

func (collection Collection) Delete(object data.Object, comment string) error

Delete removes a record from the mock database.

func (Collection) HardDelete added in v0.1.0

func (collection Collection) HardDelete(criteria exp.Expression) error

HardDelete is not implemented by the mock collection, and always returns an error.

func (Collection) Iterator added in v0.28.0

func (collection Collection) Iterator(criteria exp.Expression, options ...option.Option) (data.Iterator, error)

Iterator retrieves a group of records as an Iterator.

func (Collection) Load

func (collection Collection) Load(criteria exp.Expression, target data.Object, _ ...option.Option) error

Load retrieves a single record from the mock collection.

func (Collection) Query added in v0.2.2

func (collection Collection) Query(_ any, _ exp.Expression, _ ...option.Option) error

Query is not implemented by the mock collection, and always returns an error.

func (Collection) Save

func (collection Collection) Save(object data.Object, comment string) error

Save inserts a new record into the mock database, or updates an existing one.

type Iterator

type Iterator struct {
	Data    []data.Object
	Options []option.Option
	Counter int
}

Iterator represents a generic set of data that can be returned by a datasource.

func NewIterator

func NewIterator(data []data.Object, options ...option.Option) *Iterator

NewIterator returns a fully initialized Iterator over the provided objects.

func (*Iterator) Close

func (iterator *Iterator) Close() error

Close moves the iterator past the end of its dataset, so that Next returns FALSE.

func (*Iterator) Count

func (iterator *Iterator) Count() int

Count returns the total number of records contained in this iterator

func (*Iterator) Error added in v0.21.0

func (iterator *Iterator) Error() error

Error returns the most recent error encountered by the iterator (always nil for the mock).

func (*Iterator) Len

func (iterator *Iterator) Len() int

Len returns the number of elements in the collection.

func (*Iterator) Less

func (iterator *Iterator) Less(i int, j int) bool

Less reports whether the element with index i should sort before the element with index j. A return value of TRUE means that the item in position "i" should appear ahead of the item in position "j".

func (*Iterator) Next

func (iterator *Iterator) Next(output any) bool

Next moves the Iterator to the next position in the dataset. If there is another record in the dataset, it returns TRUE, and writes the next record to the "output" variable. If there are no more records, it returns FALSE.

func (*Iterator) Reset

func (iterator *Iterator) Reset()

Reset moves the iterator back to the beginning of the dataset

func (*Iterator) Swap

func (iterator *Iterator) Swap(i int, j int)

Swap swaps the elements with indexes i and j

type Server

type Server map[string][]data.Object

Server is a mock database

func New

func New() *Server

New returns a fully initialized Server.

func (*Server) Session

func (server *Server) Session(ctx context.Context) (data.Session, error)

Session returns a session that can be used as a mock database.

func (*Server) WithTransaction added in v0.31.0

func (server *Server) WithTransaction(ctx context.Context, fn data.TransactionCallbackFunc) (any, error)

WithTransaction executes a callback function within a new Session.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session is a mock database session

func (Session) Close

func (session Session) Close()

Close satisfies the data.Session interface, and does nothing.

func (Session) Collection

func (session Session) Collection(collection string) data.Collection

Collection returns a reference to a collection of records

func (Session) Context

func (session Session) Context() context.Context

Context returns the context for this session

Jump to

Keyboard shortcuts

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