Skip to content

Commit

Permalink
Allow MongoDB to be instantiated without mongomock library
Browse files Browse the repository at this point in the history
raise ImportError if requested to mock mongo without mongomock in the path
  • Loading branch information
amaltaro committed Mar 4, 2022
1 parent ac83e5f commit c6f72ac
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/python/WMCore/Database/MongoDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@

# futures
from __future__ import division, print_function

from builtins import str, object

try:
import mongomock
except ImportError:
# this library should only be required by unit tests
mongomock = None

from pymongo import MongoClient, errors, IndexModel
import mongomock


class MongoDB(object):
"""
A simple wrapper class for creating a connection to a MongoDB instance
"""
def __init__(self,
database=None,
server=None,
port=None,
replicaset=None,
create=False,
collections=None,
testIndexes=False,
logger=None,
mockMongoDB=False,
**kwargs):
def __init__(self, database=None, server=None, port=None, replicaset=None,
create=False, collections=None, testIndexes=False,
logger=None, mockMongoDB=False, **kwargs):
"""
:databases: A database Name to connect to
:server: The server url (see https://docs.mongodb.com/manual/reference/connection-string/)
Expand All @@ -44,6 +40,10 @@ def __init__(self,
self.port = port # 8230
self.logger = logger
self.mockMongoDB = mockMongoDB
if mockMongoDB and mongomock is None:
msg = "You are trying to mock MongoDB, but you do not have mongomock in the python path."
self.logger.critical(msg)
raise ImportError(msg)
try:
if mockMongoDB:
self.client = mongomock.MongoClient()
Expand Down

0 comments on commit c6f72ac

Please sign in to comment.