Browse Source

Add unit tests

pull/19073/head
liangshiwei 2 years ago
parent
commit
a3046889e3
  1. 3
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasCreateCollectionOptions.cs
  2. 4
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs
  3. 15
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityModelBuilder.cs
  4. 2
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs
  5. 13
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs
  6. 8
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/TestAppMongoDbContext.cs

3
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<BsonDocument> CreateCollectionOptions { get; }
}

4
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs

@ -13,7 +13,7 @@ public interface IMongoEntityModelBuilder<TEntity>
BsonClassMap<TEntity> BsonMap { get; }
CreateCollectionOptions<TEntity> CreateCollectionOptions { get; }
CreateCollectionOptions<BsonDocument> CreateCollectionOptions { get; }
void ConfigureIndexes(Action<IMongoIndexManager<BsonDocument>> action);
}
@ -26,7 +26,7 @@ public interface IMongoEntityModelBuilder
BsonClassMap BsonMap { get; }
CreateCollectionOptions CreateCollectionOptions { get; }
CreateCollectionOptions<BsonDocument> CreateCollectionOptions { get; }
void ConfigureIndexes(Action<IMongoIndexManager<BsonDocument>> action);
}

15
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityModelBuilder.cs

@ -18,36 +18,27 @@ public class MongoEntityModelBuilder<TEntity> :
public string CollectionName { get; set; } = default!;
public Action<IMongoIndexManager<BsonDocument>>? IndexesAction { get; set; }
public CreateCollectionOptions<BsonDocument> CreateCollectionOptions { get; }
BsonClassMap IMongoEntityModelBuilder.BsonMap => _bsonClassMap;
BsonClassMap<TEntity> IMongoEntityModelBuilder<TEntity>.BsonMap => _bsonClassMap;
private readonly BsonClassMap<TEntity> _bsonClassMap;
CreateCollectionOptions<TEntity> IMongoEntityModelBuilder<TEntity>.CreateCollectionOptions => _createCollectionOptions;
CreateCollectionOptions IMongoEntityModelBuilder.CreateCollectionOptions => _createCollectionOptions;
private readonly CreateCollectionOptions<TEntity> _createCollectionOptions;
public MongoEntityModelBuilder()
{
EntityType = typeof(TEntity);
_bsonClassMap = new BsonClassMap<TEntity>();
_createCollectionOptions = new CreateCollectionOptions<TEntity>();
_bsonClassMap.ConfigureAbpConventions();
CreateCollectionOptions = new CreateCollectionOptions<BsonDocument>();
}
public BsonClassMap GetMap()
{
return _bsonClassMap;
}
public CreateCollectionOptions GetCreateCollectionOptions()
{
return _createCollectionOptions;
}
public void ConfigureIndexes(Action<IMongoIndexManager<BsonDocument>>? indexesAction)
{

2
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<IHasCreateCollectionOptions>().GetCreateCollectionOptions();
var createCollectionOptions = entityModel.As<IHasCreateCollectionOptions>().CreateCollectionOptions;
var indexesAction = entityModel.As<IHasMongoIndexManagerAction>().IndexesAction;
CreateCollectionIfNotExists(dbContext, entityModel.CollectionName, createCollectionOptions);
CreateCollectionIndexes(dbContext, entityModel.CollectionName, indexesAction);

13
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<AbpMongoDbTestModul
person.Phones.Count.ShouldBe(1);
person.Phones.Any(p => 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();
}
}

8
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<Person>(b =>
{
b.CreateCollectionOptions.Collation = new Collation(locale:"en_US", strength: CollationStrength.Secondary);
});
}
}

Loading…
Cancel
Save