def test_connect_to_mongo_read_preference(self, enum_name, mongos_name, expected_read_preference):
     """
     Test that read_preference parameter gets converted to a valid pymongo read preference.
     """
     host = 'edx.devstack.mongo' if 'BOK_CHOY_HOSTNAME' in os.environ else 'localhost'
     db = 'test_read_preference_%s' % uuid4().hex
     # Support for read_preference given in constant name form (ie. PRIMARY, SECONDARY_PREFERRED)
     connection = connect_to_mongodb(db, host, read_preference=enum_name)
     self.assertEqual(connection.client.read_preference, expected_read_preference)
     # Support for read_preference given as mongos name.
     connection = connect_to_mongodb(db, host, read_preference=mongos_name)
     self.assertEqual(connection.client.read_preference, expected_read_preference)
Exemple #2
0
 def test_connect_to_mongo_read_preference(self, enum_name, mongos_name, expected_read_preference):
     """
     Test that read_preference parameter gets converted to a valid pymongo read preference.
     """
     host = 'edx.devstack.mongo' if 'BOK_CHOY_HOSTNAME' in os.environ else 'localhost'
     db = 'test_read_preference_%s' % uuid4().hex
     # Support for read_preference given in constant name form (ie. PRIMARY, SECONDARY_PREFERRED)
     connection = connect_to_mongodb(db, host, read_preference=enum_name)
     assert connection.client.read_preference == expected_read_preference
     # Support for read_preference given as mongos name.
     connection = connect_to_mongodb(db, host, read_preference=mongos_name)
     assert connection.client.read_preference == expected_read_preference
Exemple #3
0
    def __init__(self,
                 host,
                 db,
                 port=27017,
                 tz_aware=True,
                 user=None,
                 password=None,
                 bucket='fs',
                 collection=None,
                 **kwargs):
        """
        Establish the connection with the mongo backend and connect to the collections

        :param collection: ignores but provided for consistency w/ other doc_store_config patterns
        """
        # GridFS will throw an exception if the Database is wrapped in a MongoProxy. So don't wrap it.
        # The appropriate methods below are marked as autoretry_read - those methods will handle
        # the AutoReconnect errors.
        proxy = False
        mongo_db = connect_to_mongodb(db,
                                      host,
                                      port=port,
                                      tz_aware=tz_aware,
                                      user=user,
                                      password=password,
                                      proxy=proxy,
                                      **kwargs)

        self.fs = gridfs.GridFS(mongo_db, bucket)  # pylint: disable=invalid-name

        self.fs_files = mongo_db[
            bucket + ".files"]  # the underlying collection GridFS uses
        self.chunks = mongo_db[bucket + ".chunks"]
Exemple #4
0
    def __init__(
            self,
            db,
            collection,
            host,
            port=27017,
            tz_aware=True,
            user=None,
            password=None,
            asset_collection=None,
            retry_wait_time=0.1,
            **kwargs  # lint-amnesty, pylint: disable=unused-argument
    ):
        """
        Create & open the connection, authenticate, and provide pointers to the collections
        """
        # Set a write concern of 1, which makes writes complete successfully to the primary
        # only before returning. Also makes pymongo report write errors.
        kwargs['w'] = 1

        self.database = connect_to_mongodb(db,
                                           host,
                                           port=port,
                                           tz_aware=tz_aware,
                                           user=user,
                                           password=password,
                                           retry_wait_time=retry_wait_time,
                                           **kwargs)

        self.course_index = self.database[collection + '.active_versions']
        self.structures = self.database[collection + '.structures']
        self.definitions = self.database[collection + '.definitions']
    def __init__(
        self,
        db,
        collection,
        host,
        port=27017,
        tz_aware=True,
        user=None,
        password=None,
        asset_collection=None,
        retry_wait_time=0.1,
        **kwargs
    ):
        """
        Create & open the connection, authenticate, and provide pointers to the collections
        """
        # Set a write concern of 1, which makes writes complete successfully to the primary
        # only before returning. Also makes pymongo report write errors.
        kwargs["w"] = 1

        self.database = connect_to_mongodb(
            db,
            host,
            port=port,
            tz_aware=tz_aware,
            user=user,
            password=password,
            retry_wait_time=retry_wait_time,
            **kwargs
        )

        self.course_index = self.database[collection + ".active_versions"]
        self.structures = self.database[collection + ".structures"]
        self.definitions = self.database[collection + ".definitions"]
Exemple #6
0
    def __init__(
        self, host, db,
        port=27017, tz_aware=True, user=None, password=None, bucket='fs', collection=None, **kwargs
    ):
        """
        Establish the connection with the mongo backend and connect to the collections

        :param collection: ignores but provided for consistency w/ other doc_store_config patterns
        """
        # GridFS will throw an exception if the Database is wrapped in a MongoProxy. So don't wrap it.
        # The appropriate methods below are marked as autoretry_read - those methods will handle
        # the AutoReconnect errors.
        proxy = False
        mongo_db = connect_to_mongodb(
            db, host,
            port=port, tz_aware=tz_aware, user=user, password=password, proxy=proxy, **kwargs
        )

        self.fs = gridfs.GridFS(mongo_db, bucket)  # pylint: disable=invalid-name

        self.fs_files = mongo_db[bucket + ".files"]  # the underlying collection GridFS uses
        self.chunks = mongo_db[bucket + ".chunks"]