fix connection problems with pymongo3 and added tests
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from pymongo import MongoClient, MongoReplicaSetClient, uri_parser
|
||||
import pymongo
|
||||
from pymongo import MongoClient, ReadPreference, uri_parser
|
||||
|
||||
|
||||
__all__ = ['ConnectionError', 'connect', 'register_connection',
|
||||
@@ -6,6 +7,10 @@ __all__ = ['ConnectionError', 'connect', 'register_connection',
|
||||
|
||||
|
||||
DEFAULT_CONNECTION_NAME = 'default'
|
||||
if pymongo.version_tuple[0] >= 3:
|
||||
READ_PREFERENCE = ReadPreference.SECONDARY_PREFERRED
|
||||
else:
|
||||
READ_PREFERENCE = False
|
||||
|
||||
|
||||
class ConnectionError(Exception):
|
||||
@@ -18,7 +23,7 @@ _dbs = {}
|
||||
|
||||
|
||||
def register_connection(alias, name=None, host=None, port=None,
|
||||
read_preference=False,
|
||||
read_preference=READ_PREFERENCE,
|
||||
username=None, password=None, authentication_source=None,
|
||||
**kwargs):
|
||||
"""Add a connection.
|
||||
@@ -109,7 +114,6 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
|
||||
# Discard replicaSet if not base string
|
||||
if not isinstance(conn_settings['replicaSet'], basestring):
|
||||
conn_settings.pop('replicaSet', None)
|
||||
connection_class = MongoReplicaSetClient
|
||||
|
||||
try:
|
||||
connection = None
|
||||
|
||||
Reference in New Issue
Block a user