def test_to_db_model_default(self): sorting_fields = [] expected_result = self.TestModel.Config.sorting_default result = SortBuilder(sorting_fields=sorting_fields).to_db( model=self.TestModel, append_sorting_default=False) self.assertEqual(expected_result, result)
def test_to_db_model_no_sorting_default(self): sorting_fields = [] expected_result = [("_id", pymongo.DESCENDING)] result = SortBuilder(sorting_fields=sorting_fields).to_db( model=self.TestModelNoSortingDefault) self.assertEqual(expected_result, result)
def test_to_db_pipeline_model_default(self): sorting_fields = [] expected_result = {"a": pymongo.DESCENDING} result = SortBuilder(sorting_fields=sorting_fields).to_db( model=self.TestModel, to_pipeline_stage=True, append_sorting_default=False) self.assertEqual(expected_result, result)
def test__init__(self): sorting_fields = [ ("a", pymongo.ASCENDING), ("b", pymongo.DESCENDING), ("c", pymongo.ASCENDING), ] builder = SortBuilder(sorting_fields=sorting_fields) self.assertEqual([("_id", pymongo.DESCENDING)], builder.sorting_default) self.assertEqual(sorting_fields, builder.sorting_fields)
def test_to_db_pipeline(self): sorting_fields = [ ("a", pymongo.ASCENDING), ("b", pymongo.DESCENDING), ("c", pymongo.ASCENDING), ] expected_result = { "a": pymongo.ASCENDING, "b": pymongo.DESCENDING, "_id": pymongo.DESCENDING, } result = SortBuilder(sorting_fields=sorting_fields).to_db( model=self.TestModel, to_pipeline_stage=True) self.assertEqual(expected_result, result)
def test_to_db(self): sorting_fields = [ ("a", pymongo.ASCENDING), ("b", pymongo.DESCENDING), ("c", pymongo.ASCENDING), ] expected_result = [ ("a", pymongo.ASCENDING), ("b", pymongo.DESCENDING), ("_id", pymongo.DESCENDING), ] result = SortBuilder(sorting_fields=sorting_fields).to_db( model=self.TestModel) self.assertEqual(expected_result, result)
async def users_list( self, request: fastapi.Request, query: dict, sort_by: SortBuilder, paginator: Paginator, projector: BaseProjector, ): return await self.user_repository.find( query=query, sort=sort_by.to_db(model=UserModel), skip=paginator.skip, limit=paginator.limit, session=request.state.db_session, projection=projector.to_db(), repository_config=BaseRepositoryConfig(convert=False), )
def test_to_db_model_no_sorting_fields(self): sorting_fields = [] with self.assertRaises(NotImplementedError): SortBuilder(sorting_fields=sorting_fields).to_db( model=self.TestModelNoSortingFields)