def test_get_collection(): MongoDBClient.__instance = None MongoDBClient._MongoDBClient__instance = None client = MongoDBClient() collection = client.get_collection("collection") assert collection.name == "collection"
async def test_list(): MongoDBClient.__instance = None MongoDBClient._MongoDBClient__instance = None client = MongoDBClient() model = Model(id=1) cursor = client.list(model, id=1) assert cursor # Test whether it correctly handles filter by non-id _dict = client.list(model, field="value") assert _dict
async def test_insert(): MongoDBClient.__instance = None MongoDBClient._MongoDBClient__instance = None client = MongoDBClient() model = Model(id=1) insert_result = await client.insert(model) assert insert_result.inserted_id == model.id
def get_db_client(): """ Gets instance of MongoDB client for you to make DB queries. :return: MongoDBClient """ from fastapi_contrib.db.client import MongoDBClient client = MongoDBClient() return client
async def test_delete(): MongoDBClient.__instance = None MongoDBClient._MongoDBClient__instance = None client = MongoDBClient() model = Model(id=1) delete_result = await client.delete(model, id=1) assert delete_result.raw_result == {} # Test whether it correctly handles filter by non-id delete_result = await client.delete(model, field="value") assert delete_result.raw_result == {}
async def test_count(): MongoDBClient.__instance = None MongoDBClient._MongoDBClient__instance = None client = MongoDBClient() model = Model(id=1) count = await client.count(model, id=1) assert count == 1 # Test whether it correctly handles filter by non-id count = await client.count(model, field="value") assert count == 1
async def test_get(): MongoDBClient.__instance = None MongoDBClient._MongoDBClient__instance = None client = MongoDBClient() model = Model(id=1) _dict = await client.get(model, id=1) assert _dict == {"_id": 1} # Test whether it correctly handles filter by non-id _dict = await client.get(model, field="value") assert _dict == {"_id": 1}
async def test_update_one(): MongoDBClient.__instance = None MongoDBClient._MongoDBClient__instance = None client = MongoDBClient() model = Model(id=1) update_result = await client.update_one(model, filter_kwargs={"id": 1}, id=2) assert update_result.raw_result == {} # Test whether it correctly handles filter by non-id update_result = await client.update_one(model, filter_kwargs={"field": "value"}, field="value2") assert update_result.raw_result == {}
async def test_update_many_params(): with patch('fastapi_contrib.db.client.MongoDBClient.update_many', new_callable=AsyncMock) as mock_update: class Model(MongoDBTimeStampedModel): class Meta: collection = "collection" client = MongoDBClient() model = Model() await model.update_many(filter_kwargs={"id": 1}, kwargs={'$set': { 'bla': 1 }}) mock_update.mock.assert_called_with(client, Model, filter_kwargs={'id': 1}, kwargs={'$set': { 'bla': 1 }})
def test_mongodbclient_is_singleton(): MongoDBClient.__instance = None MongoDBClient._MongoDBClient__instance = None client = MongoDBClient() assert client == MongoDBClient()