Small library for mocking pymongo collection objects for testing purposes
Questions tagged [mongomock]
28 questions
12
votes
0 answers
MongoMock and mongoengine not clearing database
During unittesting I'm calling:
self.connection = connect(db="testdb", host="mongomock://localhost")
self.connection.drop_database("testdb")
between tests, however data is persisting. Is there a known fix for this?

James MV
- 8,569
- 17
- 65
- 96
11
votes
1 answer
Using a fake mongoDB for pytest testing
I have code that connects to a MongoDB Client and I'm trying to test it. For testing, I don't want to connect to the actual client, so I'm trying to figure out make a fake one for testing purposes. The basic flow of the code is I have a function…

TheStrangeQuark
- 2,257
- 5
- 31
- 58
5
votes
1 answer
How to implement pytest for FastAPI with MongoDB(Motor)
I want to write tests for my FastAPI endpoints
example for my code:
from fastapi import FastAPI
from fastapi.testclient import TestClient
app = FastAPI()
@app.get("/todos")
async def get_todo_by_title(title: str,current_user: User =…

John
- 73
- 1
- 5
5
votes
1 answer
Testing $lookup aggregations with Mongomock
I got an aggregation query with $lookup inside it:
pipeline = [{
'$match': {
'_id': ObjectId(layout_id)
}
}, {
'$lookup': {
'from': 'units',
'localField': 'unit_id',
'foreignField': '_id',
'as':…

lesz3k
- 148
- 9
4
votes
1 answer
How can I use mock for testing inside greenlet?
I use bottle & gevent for my python (2.7.6) application.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from gevent import spawn, monkey
from bottle import Bottle
from .settings import MONGODB_HOST, MONGODB_PORT,…

Ruslan Ksalov
- 111
- 1
- 7
3
votes
2 answers
How to mock mongodb when it is called from another function?
I need help while mocking mongodb. I am using mongomock to mock mongodb.
My project structure is:
-- my_mongo.py
-- code.py
-- my_test.py
my_mongo.py has :
from pymongo import MongoClient
def get_db():
client =…

cell-in
- 709
- 2
- 11
- 27
2
votes
0 answers
Mocking MongoDB for Testing REST API designed in Flask
I have a Flask application, where the REST APIs are built using flask_restful with a MongoDB backend. I want to write Functional tests using pytest and mongomock for mocking the MongoDB to test the APIs but not able to configure that. Can anyone…

AR7
- 366
- 1
- 16
2
votes
1 answer
$sum inside aggregate in mongomock seems not to be working
I have an aggregate Mongo query that projects some fields and calculates two other ones using $sum. The query works as expected, so I created an unit test for it, and to my surprise the test was failing.
I created a minimal, complete, and verifiable…

gmauch
- 1,316
- 4
- 25
- 39
2
votes
1 answer
Tornado, Motor with mongomock for testing
I'm writing a test module for a tornado based web application. The application uses motor as mongodb connector and I wish that my tests run on a temporary database. I am using a mocking technic on the delegate_class of the connector client as…

ScotchAndSoda
- 3,811
- 4
- 34
- 38
1
vote
0 answers
How to mock mongodb connection for testing using Mockito in Springboot?
I'm new to testing framework
I have to test a function which have involves storing data from Db into list.
Note: I don't have any POJO Class or Entity class for this.
I'm just connecting to the database. Following is the method for which I want to…

Rishabh
- 125
- 7
1
vote
2 answers
Insert ObjectId into mongodb
I'm working on a mongo database which, for some reason, has the user id stored as ObjectId.
In order to test some functions, I'd like to be able to populate a test database - for example, as follows:
import mongomock
from bson import…

ignoring_gravity
- 6,677
- 4
- 32
- 65
1
vote
1 answer
gomock, Go,mango package ,MongoMock
I am trying to mock the below method using gomock
func GetS(tenantName string) (*mgo.Session, error) {
ctx := apiContext.TContext{}
url, err := connectionURLList.get(tenantName)
if err != nil {
log.GenericWarning(ctx,
…

Parasa Charles
- 43
- 7
1
vote
0 answers
python mongoengine: mongomock drop_collection does not work as expected
When using mongomock to mock mongoengine, the connection.drop_database() method do not work properly: the first time I use it, it works as expected but the second time does not work at all.
The following test should be a comprehensive…

Riccardo Petraglia
- 1,943
- 1
- 13
- 25
0
votes
0 answers
Mongomock throws an error when pymongo doesn't
I am using the aggregation pipeline below which results in success with pymongo itself. With mongomock there is the error message TypeError: int() argument must be a string, a bytes-like object or a real number, not 'datetime.datetime'. Is this a…

creyD
- 1,972
- 3
- 26
- 55
0
votes
0 answers
How to assert data saved in mongomock?
Below is the function for saving data in Mongo.
def _save(data):
db_client = DBConnect().client
try:
db = db_client[os.environ['DB']]
table = db['Table']
table.insert_many([data], ordered=False)
except Exception…

XYZ
- 13
- 4