def test_Priority(self): """ Check that connection-level, database-level, collection-level and query-level write concerns are respected with correct priority """ conn = MongoConnection(mongo_host, mongo_port, w=1, wtimeout=500) try: with self.mock_gle() as mock: yield conn.mydb.mycol.insert({'x': 42}) mock.assert_called_once_with("mydb", w=1, wtimeout=500) db_w0 = Database(conn, "mydb", write_concern=WriteConcern(w=0)) with self.mock_gle() as mock: yield db_w0.mycol.insert({'x': 42}) self.assertFalse(mock.called) coll = Collection(db_w0, "mycol", write_concern=WriteConcern(w=2)) with self.mock_gle() as mock: yield coll.insert({'x': 42}) mock.assert_called_once_with("mydb", w=2) with self.mock_gle() as mock: yield coll.insert({'x': 42}, j=True) mock.assert_called_once_with("mydb", j=True) finally: yield conn.mydb.mycol.drop() yield conn.disconnect()
def test_Levels(self): as_son = CodecOptions(document_class=SON) doc = yield self.coll.find_one() self.assertIsInstance(doc, dict) try: conn = MongoConnection(mongo_host, mongo_port, codec_options=as_son) doc = yield conn.db.coll.find_one() self.assertIsInstance(doc, SON) finally: yield conn.disconnect() doc = yield Database(self.conn, "db", codec_options=as_son).coll.find_one() self.assertIsInstance(doc, SON) doc = yield self.coll.with_options(codec_options=as_son).find_one() self.assertIsInstance(doc, SON)
def __getitem__(self, name): return Database(self, name)
def __getitem__(self, database_name): return Database(self.__factory, database_name)