From a3046889e332a6f219db8260c021885196dca4e8 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 20 Feb 2024 11:28:26 +0800 Subject: [PATCH] Add unit tests --- .../Abp/MongoDB/IHasCreateCollectionOptions.cs | 3 ++- .../Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs | 4 ++-- .../Volo/Abp/MongoDB/MongoEntityModelBuilder.cs | 15 +++------------ .../Volo/Abp/MongoDB/MongoModelBuilder.cs | 2 +- .../Repositories/Repository_Basic_Tests.cs | 13 +++++++++++++ .../Abp/TestApp/MongoDb/TestAppMongoDbContext.cs | 8 +++++++- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasCreateCollectionOptions.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasCreateCollectionOptions.cs index ce4a08e9cb..c9340bddca 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasCreateCollectionOptions.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasCreateCollectionOptions.cs @@ -1,8 +1,9 @@ +using MongoDB.Bson; using MongoDB.Driver; namespace Volo.Abp.MongoDB; public interface IHasCreateCollectionOptions { - CreateCollectionOptions GetCreateCollectionOptions(); + CreateCollectionOptions CreateCollectionOptions { get; } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs index 4ee3f09bc6..fd781aac20 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs @@ -13,7 +13,7 @@ public interface IMongoEntityModelBuilder BsonClassMap BsonMap { get; } - CreateCollectionOptions CreateCollectionOptions { get; } + CreateCollectionOptions CreateCollectionOptions { get; } void ConfigureIndexes(Action> action); } @@ -26,7 +26,7 @@ public interface IMongoEntityModelBuilder BsonClassMap BsonMap { get; } - CreateCollectionOptions CreateCollectionOptions { get; } + CreateCollectionOptions CreateCollectionOptions { get; } void ConfigureIndexes(Action> action); } diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityModelBuilder.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityModelBuilder.cs index 2fe88cc7db..14f46e1df7 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityModelBuilder.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityModelBuilder.cs @@ -18,36 +18,27 @@ public class MongoEntityModelBuilder : public string CollectionName { get; set; } = default!; public Action>? IndexesAction { get; set; } + + public CreateCollectionOptions CreateCollectionOptions { get; } BsonClassMap IMongoEntityModelBuilder.BsonMap => _bsonClassMap; BsonClassMap IMongoEntityModelBuilder.BsonMap => _bsonClassMap; private readonly BsonClassMap _bsonClassMap; - - CreateCollectionOptions IMongoEntityModelBuilder.CreateCollectionOptions => _createCollectionOptions; - - CreateCollectionOptions IMongoEntityModelBuilder.CreateCollectionOptions => _createCollectionOptions; - - private readonly CreateCollectionOptions _createCollectionOptions; public MongoEntityModelBuilder() { EntityType = typeof(TEntity); _bsonClassMap = new BsonClassMap(); - _createCollectionOptions = new CreateCollectionOptions(); _bsonClassMap.ConfigureAbpConventions(); + CreateCollectionOptions = new CreateCollectionOptions(); } public BsonClassMap GetMap() { return _bsonClassMap; } - - public CreateCollectionOptions GetCreateCollectionOptions() - { - return _createCollectionOptions; - } public void ConfigureIndexes(Action>? indexesAction) { diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs index 5814cd2b26..d7ad79f7d0 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs @@ -74,7 +74,7 @@ public class MongoModelBuilder : IMongoModelBuilder baseClasses.AddRange(entityModel.EntityType.GetBaseClasses(includeObject: false)); - var createCollectionOptions = entityModel.As().GetCreateCollectionOptions(); + var createCollectionOptions = entityModel.As().CreateCollectionOptions; var indexesAction = entityModel.As().IndexesAction; CreateCollectionIfNotExists(dbContext, entityModel.CollectionName, createCollectionOptions); CreateCollectionIndexes(dbContext, entityModel.CollectionName, indexesAction); diff --git a/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs b/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs index 7c4bd0441d..0d5122228d 100644 --- a/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs +++ b/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using MongoDB.Driver.Linq; using Shouldly; +using Volo.Abp.Domain.Repositories; using Volo.Abp.TestApp; using Volo.Abp.TestApp.Domain; using Volo.Abp.TestApp.Testing; @@ -64,4 +65,16 @@ public class Repository_Basic_Tests : Repository_Basic_Tests p.PersonId == person.Id && p.Number == "1234567890").ShouldBeTrue(); } + + [Fact] + public async Task Filter_Case_Insensitive() + { + (await CityRepository.GetMongoQueryableAsync()).FirstOrDefault(c => c.Name == "ISTANBUL").ShouldBeNull(); + (await CityRepository.GetMongoQueryableAsync()).FirstOrDefault(c => c.Name == "istanbul").ShouldBeNull(); + (await CityRepository.GetMongoQueryableAsync()).FirstOrDefault(c => c.Name == "Istanbul").ShouldNotBeNull(); + + (await PersonRepository.GetQueryableAsync()).FirstOrDefault(p => p.Name == "douglas").ShouldNotBeNull(); + (await PersonRepository.GetQueryableAsync()).FirstOrDefault(p => p.Name == "DOUGLAS").ShouldNotBeNull(); + (await PersonRepository.GetQueryableAsync()).FirstOrDefault(p => p.Name == "Douglas").ShouldNotBeNull(); + } } diff --git a/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/TestAppMongoDbContext.cs b/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/TestAppMongoDbContext.cs index 9bc2606878..fc1ef00cd4 100644 --- a/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/TestAppMongoDbContext.cs +++ b/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/TestAppMongoDbContext.cs @@ -1,4 +1,5 @@ -using MongoDB.Driver; +using MongoDB.Bson; +using MongoDB.Driver; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; using Volo.Abp.EntityFrameworkCore.TestApp.FourthContext; @@ -35,5 +36,10 @@ public class TestAppMongoDbContext : AbpMongoDbContext, ITestAppMongoDbContext, { b.CollectionName = "MyCities"; }); + + modelBuilder.Entity(b => + { + b.CreateCollectionOptions.Collation = new Collation(locale:"en_US", strength: CollationStrength.Secondary); + }); } }