Questions tagged [mongomock]

Small library for mocking pymongo collection objects for testing purposes

Documentation

28 questions
0
votes
2 answers

How to realize tests on functions which are using mongoengine connection in Python?

I have Fast API with a Mongo Database I try to realize my tests for some functions which need mongo connection Here my collection users and its fields: from mongoengine import Document, EmailField, StringField, EnumField class User(Document): …
Rickross
  • 1
  • 2
0
votes
0 answers

Using mongomock to mock database requests for Flask app

When testing my Flask application with Pytest I run into connection problems with the database since it is not running. I read that mongomock is often used to mock requests to the database but I am having trouble implementing it to the existing test…
jndr
  • 3
  • 3
0
votes
0 answers

Test django with mongomock for django auth models

I'm trying to perform unit tests on Django with db mocking by mongomock. My connection to the DB is made using settings.py: DATABASES = { 'dbname': { 'ENGINE': 'djongo', 'NAME': 'db1', 'ENFORCE_SCHEMA': False, …
E.Bloch
  • 156
  • 1
  • 5
0
votes
1 answer

Is there a way to mock an instance variable when imported using "from .." in python?

New to Python pytest, below are the code snippets which I am using to mock a MongoDB connection. My Mongo Connection util, which is used by my all dao layers: connection_util.py from pymongo import MongoClient mongo_connection = MongoClient() This…
Dinesh
  • 1,135
  • 2
  • 15
  • 22
0
votes
1 answer

FastAPI dependency override faild

I'm using MongoMock to test my FastAPI app, but I can' override FastAPI's Dependency. The create_test_data will insert some fake test data with MongoMock' Client and return the task_id which is need by api request. from mongomock import…
nickchen
  • 93
  • 1
  • 1
  • 5
0
votes
1 answer

mongoengine Document, multiple tenant databases, and pytest+mongomock

I've inherited a well established flask-based API service that makes extensive use of mongoengine. We are making this single-database API into a multi-tenant service, and unclear on the best practice. For many reasons, tenant data will be…
hikaru
  • 2,444
  • 4
  • 22
  • 29
0
votes
1 answer

mongomock aggregate $lookup does not resolve collection name like real mongo db (python)

I have a simple aggregate lookup query like this: cursor = db.my_db['shop'].aggregate([ { '$lookup': { 'from': 'customer', 'localField': 'in_shop', …
user2887278
  • 165
  • 2
  • 13
0
votes
0 answers

How to use mongomock with unittest python

I try to mock my Mongo database on tests. I find article which I follow, but unfortunately my tests write to real mongo database instead of mocked. I have database.py file with PyMongo creation: from flask_pymongo import PyMongo mongo =…
Vladyslav
  • 2,018
  • 4
  • 18
  • 44
0
votes
1 answer

Update records with new objects

Say I have the following MongoDB collection (am using mongomock for this example so it's easy to reproduce): import mongomock collection = mongomock.MongoClient().db.collection objects = [{'name': 'Alice', 'age': 21}, {'name': 'Bob', 'age':…
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
0
votes
0 answers

pytest monkeypatch not able to patch the mongodb collection

I'm trying to unit test the following function with pytest. @login_sys.route("/users/register", methods=["POST"]) def register(): users = mongo.db.users # TRYING TO PATCH THIS print("--->{}".format(type(users))) email =…
BOB
  • 151
  • 3
  • 13
0
votes
0 answers

Test cases are failing using mongomock pymongo mongoengine

I am trying to run test cases with mongomock having pymongo version as 2.X and mongoengine 0.10.6 I have tried to change the version for pymongo to 2.9.5 from 2.6.3 and mongoengine from 0.10.6 to 0.17.0. I have tried with mongomock from 3.0.0 to…
0
votes
1 answer

Mongoengine + MongoMock do not return data as expected

I am trying to test a response made by the controller. The response is different when I am running it through gunicorn vs a testFramework My server.py looks like the following: app = Flask(__name__, static_url_path='/pub') api = Api(app,…
Angel.King.47
  • 7,922
  • 14
  • 60
  • 85
-2
votes
2 answers

Python unit test coverage using pytest

I am new to pytest and wanted to add the below 3 methods for unit test coverage without actually using a real mongo db instance but rather mock it. Could try using a real db instance but it isn't recommended. Request for an example on how to mock…
Suraj Prasad
  • 241
  • 3
  • 24
1
2