Commit b5691cd4 by Don Mitchell

Upgrade pymongo usage from deprecated connection to client

LMS-1551
parent 39f4cb80
...@@ -1604,7 +1604,7 @@ class ContentStoreTest(ModuleStoreTestCase): ...@@ -1604,7 +1604,7 @@ class ContentStoreTest(ModuleStoreTestCase):
data = parse_json(resp) data = parse_json(resp)
self.assertRegexpMatches( self.assertRegexpMatches(
data['locator'], data['locator'],
r"^MITx.999.Robot_Super_Course/branch/draft/block/chapter([0-9]|[a-f]){3}$" r"^MITx.999.Robot_Super_Course/branch/draft/block/chapter([0-9]|[a-f]){3,}$"
) )
def test_capa_module(self): def test_capa_module(self):
......
from pymongo import Connection import pymongo
import gridfs import gridfs
from gridfs.errors import NoFile from gridfs.errors import NoFile
...@@ -24,8 +24,14 @@ class MongoContentStore(ContentStore): ...@@ -24,8 +24,14 @@ class MongoContentStore(ContentStore):
:param collection: ignores but provided for consistency w/ other doc_store_config patterns :param collection: ignores but provided for consistency w/ other doc_store_config patterns
""" """
logging.debug('Using MongoDB for static content serving at host={0} db={1}'.format(host, db)) logging.debug('Using MongoDB for static content serving at host={0} db={1}'.format(host, db))
_db = pymongo.database.Database(
_db = Connection(host=host, port=port, **kwargs)[db] pymongo.MongoClient(
host=host,
port=port,
**kwargs
),
db
)
if user is not None and password is not None: if user is not None and password is not None:
_db.authenticate(user, password) _db.authenticate(user, password)
......
...@@ -274,25 +274,30 @@ class MongoModuleStore(ModuleStoreWriteBase): ...@@ -274,25 +274,30 @@ class MongoModuleStore(ModuleStoreWriteBase):
""" """
Create & open the connection, authenticate, and provide pointers to the collection Create & open the connection, authenticate, and provide pointers to the collection
""" """
self.collection = pymongo.connection.Connection( self.database = pymongo.database.Database(
host=host, pymongo.MongoClient(
port=port, host=host,
tz_aware=tz_aware, port=port,
**kwargs tz_aware=tz_aware,
)[db][collection] **kwargs
),
db
)
self.collection = self.database[collection]
if user is not None and password is not None: if user is not None and password is not None:
self.collection.database.authenticate(user, password) self.database.authenticate(user, password)
do_connection(**doc_store_config) do_connection(**doc_store_config)
# Force mongo to report errors, at the expense of performance # Force mongo to report errors, at the expense of performance
self.collection.safe = True self.collection.write_concern = {'w': 1}
# Force mongo to maintain an index over _id.* that is in the same order # Force mongo to maintain an index over _id.* that is in the same order
# that is used when querying by a location # that is used when querying by a location
self.collection.ensure_index( self.collection.ensure_index(
zip(('_id.' + field for field in Location._fields), repeat(1)) zip(('_id.' + field for field in Location._fields), repeat(1)),
name="location_index"
) )
if default_class is not None: if default_class is not None:
......
...@@ -69,7 +69,11 @@ class TestMixedModuleStore(object): ...@@ -69,7 +69,11 @@ class TestMixedModuleStore(object):
""" """
Set up the database for testing Set up the database for testing
""" """
cls.connection = pymongo.connection.Connection(HOST, PORT) cls.connection = pymongo.MongoClient(
host=HOST,
port=PORT,
tz_aware=True,
)
cls.connection.drop_database(DB) cls.connection.drop_database(DB)
cls.fake_location = Location(['i4x', 'foo', 'bar', 'vertical', 'baz']) cls.fake_location = Location(['i4x', 'foo', 'bar', 'vertical', 'baz'])
cls.import_org, cls.import_course, cls.import_run = IMPORT_COURSEID.split('/') cls.import_org, cls.import_course, cls.import_run = IMPORT_COURSEID.split('/')
......
...@@ -20,7 +20,7 @@ from xmodule.modulestore.xml_importer import import_from_xml, perform_xlint ...@@ -20,7 +20,7 @@ from xmodule.modulestore.xml_importer import import_from_xml, perform_xlint
from xmodule.contentstore.mongo import MongoContentStore from xmodule.contentstore.mongo import MongoContentStore
from xmodule.modulestore.tests.test_modulestore import check_path_to_location from xmodule.modulestore.tests.test_modulestore import check_path_to_location
from IPython.testing.nose_assert_methods import assert_in, assert_not_in from IPython.testing.nose_assert_methods import assert_in
from xmodule.exceptions import NotFoundError from xmodule.exceptions import NotFoundError
from xmodule.modulestore.exceptions import InsufficientSpecificationError from xmodule.modulestore.exceptions import InsufficientSpecificationError
...@@ -39,7 +39,11 @@ class TestMongoModuleStore(object): ...@@ -39,7 +39,11 @@ class TestMongoModuleStore(object):
'''Tests!''' '''Tests!'''
@classmethod @classmethod
def setupClass(cls): def setupClass(cls):
cls.connection = pymongo.connection.Connection(HOST, PORT) cls.connection = pymongo.MongoClient(
host=HOST,
port=PORT,
tz_aware=True,
)
cls.connection.drop_database(DB) cls.connection.drop_database(DB)
# NOTE: Creating a single db for all the tests to save time. This # NOTE: Creating a single db for all the tests to save time. This
...@@ -50,8 +54,8 @@ class TestMongoModuleStore(object): ...@@ -50,8 +54,8 @@ class TestMongoModuleStore(object):
@classmethod @classmethod
def teardownClass(cls): def teardownClass(cls):
cls.connection = pymongo.connection.Connection(HOST, PORT) if cls.connection:
cls.connection.drop_database(DB) cls.connection.drop_database(DB)
@staticmethod @staticmethod
def initdb(): def initdb():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment