Beispiel #1
0
    def test_auto_ref_and_deref(self):
        # Test same functionality as in PyMongo's test_database.py; the
        # implementation for Motor for async is a little complex so we test
        # that it works here, and we don't just rely on synchrotest
        # to cover it.
        db = self.db

        # We test a special hack where add_son_manipulator corrects our mistake
        # if we pass a MotorDatabase, instead of Database, to AutoReference.
        db.add_son_manipulator(AutoReference(db))
        db.add_son_manipulator(NamespaceInjector())

        a = {"hello": "world"}
        b = {"test": a}
        c = {"another test": b}

        yield db.a.delete_many({})
        yield db.b.delete_many({})
        yield db.c.delete_many({})
        yield db.a.save(a)
        yield db.b.save(b)
        yield db.c.save(c)
        a["hello"] = "mike"
        yield db.a.save(a)
        result_a = yield db.a.find_one()
        result_b = yield db.b.find_one()
        result_c = yield db.c.find_one()

        self.assertEqual(a, result_a)
        self.assertEqual(a, result_b["test"])
        self.assertEqual(a, result_c["another test"]["test"])
        self.assertEqual(b, result_b)
        self.assertEqual(b, result_c["another test"])
        self.assertEqual(c, result_c)
Beispiel #2
0
 def __init__(self):
     host = MONGODB_SETTINGS['host']
     port = MONGODB_SETTINGS['port']
     max_pool = MONGODB_SETTINGS['max_pool']
     self.connection = pymongo.Connection(host, port, max_pool)
     self.db = self.connection["blade"]
     self.db.add_son_manipulator(AutoReference(self.db))
Beispiel #3
0
    def test_auto_ref_and_deref(self):
        # Legacy API.
        db = self.client.pymongo_test
        db.add_son_manipulator(AutoReference(db))
        db.add_son_manipulator(NamespaceInjector())

        db.test.a.remove({})
        db.test.b.remove({})
        db.test.c.remove({})

        a = {"hello": u"world"}
        db.test.a.save(a)

        b = {"test": a}
        db.test.b.save(b)

        c = {"another test": b}
        db.test.c.save(c)

        a["hello"] = "mike"
        db.test.a.save(a)

        self.assertEqual(db.test.a.find_one(), a)
        self.assertEqual(db.test.b.find_one()["test"], a)
        self.assertEqual(db.test.c.find_one()["another test"]["test"], a)
        self.assertEqual(db.test.b.find_one(), b)
        self.assertEqual(db.test.c.find_one()["another test"], b)
        self.assertEqual(db.test.c.find_one(), c)
Beispiel #4
0
def initialize(name=None, seeds=None, max_pool_size=None):
    """
    Initialize the connection pool and top-level database for pulp.
    """
    global _CONNECTION, _DATABASE

    try:
        if name is None:
            name = config.config.get('database', 'name')

        if seeds is None:
            seeds = config.config.get('database', 'seeds')

        if max_pool_size is None:
            # we may want to make this configurable, but then again, we may not
            max_pool_size = _DEFAULT_MAX_POOL_SIZE

        _LOG.info("Attempting Database connection with seeds = %s" % (seeds))

        _CONNECTION = pymongo.Connection(seeds, max_pool_size=max_pool_size)

        _DATABASE = getattr(_CONNECTION, name)
        _DATABASE.add_son_manipulator(NamespaceInjector())
        _DATABASE.add_son_manipulator(AutoReference(_DATABASE))

        _LOG.info(
            "Database connection established with: seeds = %s, name = %s" %
            (seeds, name))

    except:
        _LOG.critical('Database initialization failed')
        _CONNECTION = None
        _DATABASE = None
        raise
Beispiel #5
0
 def __init__(self):
     host = MONGODB_SETTINGS['host']
     port = MONGODB_SETTINGS['port']
     max_pool = MONGODB_SETTINGS['max_pool']
     self.connection = motor.MotorClient(host, port, max_pool).open_sync()
     self.db = self.connection[MONGODB_SETTINGS["database"]]
     self.db.add_son_manipulator(NamespaceInjector())
     self.db.add_son_manipulator(AutoReference(self.db))
Beispiel #6
0
 def database(self):
     """
     Returns a connection to the given database.
     """
     connection = self.connection
     database = getattr(connection, self.db_name)
     database.add_son_manipulator(NamespaceInjector())
     database.add_son_manipulator(AutoReference(database))
     return database
Beispiel #7
0
def connect(db_name='pulp_database'):
    """
    Connect to a test database.
    Optional argument, db_name, specifies the name of the database.
    """
    global _connection, _db
    _connection = pymongo.Connection()
    _db = getattr(_connection, db_name)
    _db.add_son_manipulator(NamespaceInjector())
    _db.add_son_manipulator(AutoReference(_db))
Beispiel #8
0
 def __init__(cls):
     uri = 'mongodb://' + MONGODB_USERNAME + ':' + MONGODB_PWD + '@' + MONGODB_SERVER + ':' + MONGODB_PORT \
           + '/' + MONGODB_DB
     client = MongoClient(uri)
     cls.database = client.get_database(MONGODB_DB)
     database = client.get_database(MONGODB_DB)
     # 自动创建与解引用
     database.add_son_manipulator(NamespaceInjector())
     database.add_son_manipulator(AutoReference(database))
     cls.blogItemCollection = database.get_collection(
         MONGODB_BLOG_ITEM_COLLECTION)
     cls.authorItemCollection = database.get_collection(
         MONGODB_AUTHOR_ITEM_COLLECTION)
Beispiel #9
0
 def test_manipulator_properties(self):
     db = self.client.foo
     self.assertEqual([], db.incoming_manipulators)
     self.assertEqual([], db.incoming_copying_manipulators)
     self.assertEqual([], db.outgoing_manipulators)
     self.assertEqual([], db.outgoing_copying_manipulators)
     db.add_son_manipulator(AutoReference(db))
     db.add_son_manipulator(NamespaceInjector())
     db.add_son_manipulator(ObjectIdShuffler())
     self.assertEqual(1, len(db.incoming_manipulators))
     self.assertEqual(db.incoming_manipulators, ['NamespaceInjector'])
     self.assertEqual(2, len(db.incoming_copying_manipulators))
     for name in db.incoming_copying_manipulators:
         self.assertTrue(name in ('ObjectIdShuffler', 'AutoReference'))
     self.assertEqual([], db.outgoing_manipulators)
     self.assertEqual(['AutoReference'], db.outgoing_copying_manipulators)
Beispiel #10
0
    def session(self):
        """ Returns MongoDB
        """
        if not hasattr(self, "db"):
            self.db = self.connection[self.app.config['MONGODB_DATABASE']]
            # we need namespaces in any case
            self.db.add_son_manipulator(NamespaceInjector())

            if self.app.config['MONGODB_AUTOREF']:
                self.db.add_son_manipulator(AutoReference(self.db))

            if self.app.config['MONGODB_AUTOINCREMENT']:
                self.db.add_son_manipulator(AutoincrementId())

            self.db.add_son_manipulator(SavedObject())
        return self.db
Beispiel #11
0
    def test_auto_ref_and_deref_list(self):
        # Legacy API.
        db = self.client.pymongo_test
        db.add_son_manipulator(AutoReference(db))
        db.add_son_manipulator(NamespaceInjector())

        db.drop_collection("users")
        db.drop_collection("messages")

        message_1 = {"title": "foo"}
        db.messages.save(message_1)
        message_2 = {"title": "bar"}
        db.messages.save(message_2)

        user = {"messages": [message_1, message_2]}
        db.users.save(user)
        db.messages.update(message_1, {"title": "buzz"})

        self.assertEqual("buzz", db.users.find_one()["messages"][0]["title"])
        self.assertEqual("bar", db.users.find_one()["messages"][1]["title"])
    def test_marc(self):
        db = self.connection.pymongo_test
        db.add_son_manipulator(AutoReference(db))
        db.add_son_manipulator(NamespaceInjector())

        db.drop_collection("users")
        db.drop_collection("messages")

        message_1 = {"title": "foo"}
        db.messages.save(message_1)
        message_2 = {"title": "bar"}
        db.messages.save(message_2)

        user = {"name": "marc", "messages": [message_1, message_2]}
        db.users.save(user)

        message = db.messages.find_one()
        db.messages.update(message, {"title": "buzz"})

        self.assertEqual("buzz", db.users.find_one()["messages"][0]["title"])
        self.assertEqual("bar", db.users.find_one()["messages"][1]["title"])
Beispiel #13
0
def initialize(name=None, seeds=None):
    """
    Initialize the connection pool and top-level database for pulp.
    """
    global _connection, _database
    try:
        if not name:
            name = config.config.get('database', 'name')
        if not seeds:
            seeds = config.config.get('database', 'seeds')
        _log.info("Attempting Database connection with seeds = %s" % (seeds))
        _connection = pymongo.Connection(seeds)
        _database = getattr(_connection, name)
        _database.add_son_manipulator(NamespaceInjector())
        _database.add_son_manipulator(AutoReference(_database))
        _log.info(
            "Database connection established with: seeds = %s, name = %s" %
            (seeds, name))
    except Exception:
        _log.critical('Database initialization failed')
        _connection = None
        _database = None
        raise
Beispiel #14
0
    def testRemoveWithRef(self):
        """
        Remove a collection object while it is referenced in a 
        separate collection.  Use of AutoReference causes the external
        reference to become None, reflecting the object was deleted.
        """
        connection = Connection()
        db = connection._test_dbrefs
        db.add_son_manipulator(NamespaceInjector())
        db.add_son_manipulator(AutoReference(db))
        db.drop_collection("books")
        db.drop_collection("groups")
        books = db.books
        groups = db.groups
        # Create book-1
        bk1 = {}
        bk1["name"] = "Test-A"
        bk1["created-time"] = time.time()
        books.save(bk1)
        # Create group-1
        grp1 = {}
        grp1["name"] = "FirstGroup"
        grp1["books"] = [bk1]
        groups.save(grp1)
        
        # lookup 'Test-A', then delete it
        b = books.find_one({"name":"Test-A"})
        books.remove(b)
        found = [f for f in books.find({"name": "Test-A"})]
        self.assertTrue(len(found) == 0)

        found = [f for f in groups.find({"name": "FirstGroup"})]
        self.assertTrue(len(found) == 1)
        self.assertTrue(found[0]["name"] == "FirstGroup")
        self.assertTrue(len(found[0]["books"]) == 1)
        self.assertTrue(found[0]["books"][0] == None)
Beispiel #15
0
 def test_add_son_manipulator_deprecation(self):
     db = self.client.pymongo_test
     self.assertRaises(DeprecationWarning,
                       lambda: db.add_son_manipulator(AutoReference(db)))
Beispiel #16
0
	def __init__(self):
		self.conn = pymongo.Connection(HOST, PORT, MAX_POOL_SIZE)
		self.db = self.conn["youthimg"]
		self.db.add_son_manipulator(AutoReference(self.db))
Beispiel #17
0
    parser.add_option('-a',
                      '--autoref',
                      action='store_true',
                      help='Run with SON AutoReference Manipulators',
                      default=False)
    parser.add_option('-r',
                      '--num_repos',
                      action='store',
                      help='How many repos to display, default is all',
                      default=None)
    parser.add_option('-p',
                      '--num_packages',
                      action='store',
                      help='How many packages to display',
                      default=5)
    options, args = parser.parse_args()

    connection = Connection()
    db = connection.pulp_database
    if options.autoref:
        db.add_son_manipulator(NamespaceInjector())
        db.add_son_manipulator(AutoReference(db))

    repos = db.repos
    found = repos.find()
    found_slice = found
    if options.num_repos:
        found_slice = found[:int(options.num_repos)]
    for r in found_slice:
        print "\nRepo: ", r
Beispiel #18
0
 def _database(self, db_name):
     connection = self._connection()
     database = getattr(connection, db_name)
     database.add_son_manipulator(NamespaceInjector())
     database.add_son_manipulator(AutoReference(database))
     return database
Beispiel #19
0
    def testChangesWithRef(self):
        """
        Change a collection object while it is referenced in 
        another collection, use AutoReference Manipulator so 
        the change is reflected.
        """
        connection = Connection()
        db = connection._test_dbrefs
        #manipulators are required to keep references in sync
        db.add_son_manipulator(NamespaceInjector())
        db.add_son_manipulator(AutoReference(db))
        db.drop_collection("books")
        db.drop_collection("groups")
        books = db.books
        groups = db.groups
        # Create book-1
        bk1 = {}
        bk1["name"] = "Test-A"
        bk1["created-time"] = time.time()
        books.save(bk1)
        # Create group-1
        grp1 = {}
        grp1["name"] = "FirstGroup"
        grp1["books"] = [bk1]
        groups.save(grp1)

        #Ensure that we have only 1 instance in each books/groups
        found = [f for f in books.find({"name": "Test-A"})]
        self.assertTrue(len(found) == 1)
        bk1_id = found[0]["_id"]
        found = [f for f in groups.find({"name": "FirstGroup"})]
        self.assertTrue(len(found) == 1)
        # Verify that we saved 'Test-A' under groups
        self.assertTrue(len(found[0]["books"]) == 1)
        self.assertTrue(found[0]["books"][0]["name"] == 'Test-A')
        self.assertTrue(found[0]["books"][0]["_id"] == bk1_id)

        # lookup 'Test-A', save it (no-modifications), ensure we still have only 
        # 1 instance of 'Test-A'
        b = books.find_one({"name":"Test-A"})
        books.save(b)
        found = [f for f in books.find({"name": "Test-A"})]
        self.assertTrue(len(found) == 1)

        # lookup 'Test-A' and modify it and save it, ensure we only have
        # 1 instance of 'Test-A'
        b = books.find_one({"name":"Test-A"})
        b["newEntry"] = "newValue"
        b["modified_time"] = time.time()
        books.save(b)
        found = [f for f in books.find({"name": "Test-A"})]
        self.assertTrue(len(found) == 1)
        # Ensure _id didn't change after our modification
        self.assertTrue(found[0]["_id"] == bk1_id)


        found = [f for f in groups.find({"name": "FirstGroup"})]
        self.assertTrue(len(found) == 1)
        self.assertTrue(found[0]["name"] == "FirstGroup")
        self.assertTrue(found[0]["books"][0]["_id"] == bk1_id)
        self.assertTrue(found[0]["books"][0].has_key("newEntry"))
        self.assertTrue(found[0]["books"][0]["newEntry"] == "newValue")