async def test_multiple_motor_db_connections(self): app = Quart(__name__) db1 = Motor(app=app, uri=f"{self.client_uri}test1") db2 = Motor(app=app, uri=f"{self.client_uri}test2") await app.startup() assert isinstance(db1.db, AsyncIOMotorDatabase) assert isinstance(db2.db, AsyncIOMotorDatabase)
async def test_motor_doesnt_connect_by_default(self): app = Quart(__name__) mongo = Motor(app=app, uri=self.uri) await app.startup() with pytest.raises(CouldNotConnect): _wait_until_connected(mongo, timeout=0.2)
async def test_find_one_or_404_notfound(self): app = Quart(__name__) mongo = Motor(app=app, uri=self.uri) await app.startup() await mongo.db.things.delete_many({}) with pytest.raises(NotFound): await mongo.db.things.find_one_or_404({"_id": "thing"})
async def test_find_one_or_404_found(self): app = Quart(__name__) mongo = Motor(app=app, uri=self.uri) await app.startup() await mongo.db.things.insert_one({"_id": "thing", "val": "foo"}) thing = await mongo.db.things.find_one_or_404({"_id": "thing"}) assert thing["val"] == "foo" await mongo.db.things.delete_many({})
async def test_motor_custom_document_class(self): class CustomDict(dict): pass app = Quart(__name__) mongo = Motor(app=app, uri=self.uri, document_class=CustomDict) await app.startup() things = await mongo.db.things.find_one() assert things is None await mongo.db.things.insert_one({"_id": "thing", "val": "foo"}) things = await mongo.db.things.find_one() await mongo.db.things.delete_many({}) assert type(things) == CustomDict
async def test_motor_no_database_name_in_uri(self): app = Quart(__name__) mongo = Motor(app=app, uri=self.client_uri) await app.startup() assert mongo.db is None
async def test_motor_database_failure(self): app = Quart(__name__) with pytest.raises(ValueError): mongo = Motor(app=app)
async def test_motor_database_success(self): app = Quart(__name__) mongo = Motor(app=app, uri=self.uri) await app.startup() assert isinstance(mongo.db, AsyncIOMotorDatabase)
async def test_motor_invalid_uri(self): app = Quart(__name__) with pytest.raises(InvalidURI): mongo = Motor(app=app, uri="http://localhost:27017/test")
async def test__motor_client_connection(self): app = Quart(__name__) mongo = Motor(app=app, uri=self.client_uri) assert isinstance(mongo, Motor)
async def main(): app = Quart(__name__) db = Motor(app=app, uri="mongodb://localhost:27017/test").db print(await db['things'].find_one({"_id": "59674459"}))