mongomock

package module
v0.0.0-...-6439478 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2023 License: MIT Imports: 9 Imported by: 0

README

mongomock A mocking MongoDB database

mongomock is a simple to use library that mocks features of a mongodb server but in memory and as a library with no external dependencies.

This package is very handy to use in a testing envourment

Docs

Quick start

go get -u github.com/mjarkk/mongomock
package main

import (
    "log"

    "github.com/mjarkk/mongomock"
    "go.mongodb.org/mongo-driver/bson/primitive"
)

type User {
    ID   primitive.ObjectID `bson:"_id" json:"id"`
    Name string             `bson:"username"`
    Email string            `bson:"email"`
}

func main() {
    db := mongomock.NewDB()
    collection := db.Collection("users")
    err := collection.InsertOne(User{
        ID:   primitive.NewObjectID(),
        Name: "test",
        Email: "example@example.org",
    })
    if err != nil {
        log.Fatal(err)
    }

    user := User{}
    err = collection.FindOne(&user, bson.M{"name": "test"})
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Found user: %+v\n", user)
    // After exit the database data is gone
}

Supported methods

Count - Count documents in a collection
nr, err := db.Collection("users").Count(bson.M{})
Delete - Delete documents in a collection
err := db.Collection("users").Delete(bson.M{})
DeleteFirst - Delete a document in a collection
err := db.Collection("users").DeleteFirst(bson.M{})
DeleteFirst - Delete a document in a collection
err := db.Collection("users").DeleteFirst(bson.M{})
DeleteByID - Delete a document by ID
err := db.Collection("users").DeleteByID(primitive.NewObjectID())
DeleteByIDs - Delete documents by their IDs
err := db.Collection("users").DeleteByID(primitive.NewObjectID(), primitive.NewObjectID(), primitive.NewObjectID())
Dump - Dump the database to std{out,err}
// Dump the full database with all it's collections in json format to stdout
// Change the
panicResults false
err := db.Dump(panicResults)

// Dump a single collection to stdout
err := db.Collection("users").Dump(panicResults)
FindFirst - Find a single doucment in a collection
user := User{}
err := db.Collection("users").FindFirst(&user, bson.M{"email": "example@example.org"})
Find - Find documents in a collection
users := []User{}
err := db.Collection("users").Find(&users, bson.M{})
FindCursor - Find documents in a collection using a cursor
cursor, err := db.Collection("users").FindCursor(bson.M{})
if err != nil {
    log.Fatal(err)
}
for cursor.Next() {
    user := User{}
    err := cursor.Decode(&user)
    if err != nil {
        log.Fatal(err)
    }
}
Insert - Insert a single document into a collection
err := db.Collection("users").Insert(User{
    ID:    primitive.NewObjectID(),
    Name:  "test",
    Email: "example@example.org",
})
ReplaceFirstByID - Replace a document
err := db.Collection("users").ReplaceFirst(bson.M{"email": "foo@example.org"}, User{
    ID:    primitive.NewObjectID(),
    Name:  "test",
    Email: "example@example.org",
})
ReplaceFirstByID - Replace a document by ID
err := db.Collection("users").ReplaceFirstByID(primitive.NewObjectID(), User{
    ID:    primitive.NewObjectID(),
    Name:  "test",
    Email: "example@example.org",
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

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

Collection contains all the data for a collection

func (*Collection) Count

func (c *Collection) Count(filter bson.M) (uint64, error)

Count returns the number of documents in the collection of entity

func (*Collection) Delete

func (c *Collection) Delete(filter bson.M) error

Delete deletes all documents matching the filter The query used here is {"_id": {"$in": ids}}

func (*Collection) DeleteByID

func (c *Collection) DeleteByID(id primitive.ObjectID) error

DeleteByID deletes a document by it's ID The query used here is {"_id": id}

func (*Collection) DeleteByIDs

func (c *Collection) DeleteByIDs(ids ...primitive.ObjectID) error

DeleteByIDs deletes documents by their IDs The query used here is {"_id": {"$in": ids}}

func (*Collection) DeleteFirst

func (c *Collection) DeleteFirst(filter bson.M) error

DeleteFirst deletes the first document that matches the filter

func (*Collection) Dump

func (c *Collection) Dump(shouldPanicResults bool)

DumpCollection prints a full database collection it's contents in the console This can be used in tests to dump the contents of the database might something fail or to debug

shouldPanic controls if the output is only printed or also should panic

func (*Collection) Find

func (c *Collection) Find(results any, filter bson.M) error

Find finds documents in the collection of the base The results can be filtered using filters The filters should work equal to MongoDB filters (https://docs.mongodb.com/manual/tutorial/query-documents/) tough this might miss features compared to mongoDB's filters

func (*Collection) FindCursor

func (c *Collection) FindCursor(filter bson.M) (*Cursor, error)

FindCursor finds documents in the collection of the base

func (*Collection) FindFirst

func (c *Collection) FindFirst(placeInto any, filter bson.M) error

FindFirst finds the first document in the collection matching the filter and places it into placeInto The result can be filtered using filters The filters should work equal to MongoDB filters (https://docs.mongodb.com/manual/tutorial/query-documents/) tough this might miss features compared to mongoDB's filters

func (*Collection) Insert

func (c *Collection) Insert(documents ...any) error

Insert inserts an item into the database Implements db.Connection

func (*Collection) ReplaceFirst

func (c *Collection) ReplaceFirst(filter bson.M, value any) error

ReplaceFirst updates the first document in the database that matches the filter

func (*Collection) ReplaceFirstByID

func (c *Collection) ReplaceFirstByID(id primitive.ObjectID, value any) error

ReplaceFirstByID updates a document in the database by its ID The query used here is {"_id": id}

func (*Collection) UnsafeInsert

func (c *Collection) UnsafeInsert(documents ...any) error

UnsafeInsert inserts data directly into the database without locking it

type Cursor

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

Cursor is a cursor for the testingdb implementing the db.Cursor

func (*Cursor) Decode

func (c *Cursor) Decode(e any) error

Decode decodes the current item within the cursor into e

func (*Cursor) Next

func (c *Cursor) Next() bool

Next looks for the next item within the cursor returns true if there is a next item returns false if there is no next item

type TestConnection

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

TestConnection is the struct that implements db.Connection

func NewDB

func NewDB() *TestConnection

NewDB returns a testing database connection that is compatible with db.Connection

func (*TestConnection) Collection

func (c *TestConnection) Collection(name string) *Collection

func (*TestConnection) Dump

func (c *TestConnection) Dump(shouldPanicResults bool)

Dump prints the full database contents in the console This can be used in tests to dump the contents of the database might something fail or to debug

shouldPanic controls if the output is only printed or also should panic

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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