diff --git a/Volo.Abp.sln b/Volo.Abp.sln index 9791fb908b..1aa4760487 100644 --- a/Volo.Abp.sln +++ b/Volo.Abp.sln @@ -166,6 +166,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Auditing", "src\Vo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Auditing.Tests", "test\Volo.Abp.Auditing.Tests\Volo.Abp.Auditing.Tests.csproj", "{D5733D90-8C3D-4026-85E2-41DED26C4938}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.MongoDB.Tests", "test\Volo.Abp.MongoDB.Tests\Volo.Abp.MongoDB.Tests.csproj", "{82ED4DD2-DEF8-40D5-9BF9-663AFD35B06D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -460,6 +462,10 @@ Global {D5733D90-8C3D-4026-85E2-41DED26C4938}.Debug|Any CPU.Build.0 = Debug|Any CPU {D5733D90-8C3D-4026-85E2-41DED26C4938}.Release|Any CPU.ActiveCfg = Release|Any CPU {D5733D90-8C3D-4026-85E2-41DED26C4938}.Release|Any CPU.Build.0 = Release|Any CPU + {82ED4DD2-DEF8-40D5-9BF9-663AFD35B06D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {82ED4DD2-DEF8-40D5-9BF9-663AFD35B06D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {82ED4DD2-DEF8-40D5-9BF9-663AFD35B06D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {82ED4DD2-DEF8-40D5-9BF9-663AFD35B06D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -539,6 +545,7 @@ Global {5AB7E368-1CC8-401D-9952-6CA6779305E7} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6} {03F51721-DA51-4BAE-9909-3FC88FAB7774} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6} {D5733D90-8C3D-4026-85E2-41DED26C4938} = {447C8A77-E5F0-4538-8687-7383196D04EA} + {82ED4DD2-DEF8-40D5-9BF9-663AFD35B06D} = {447C8A77-E5F0-4538-8687-7383196D04EA} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5} diff --git a/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs b/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs index ef5f33a42a..4b3f30937e 100644 --- a/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs +++ b/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs @@ -62,11 +62,6 @@ namespace Volo.Abp.DependencyInjection return this; } - public ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(bool includeAllEntities = false) - { - return AddDefaultRepositories(typeof(TDefaultRepositoryDbContext), includeAllEntities); - } - public ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(Type defaultRepositoryDbContextType, bool includeAllEntities = false) { if (!defaultRepositoryDbContextType.IsAssignableFrom(OriginalDbContextType)) @@ -76,7 +71,12 @@ namespace Volo.Abp.DependencyInjection DefaultRepositoryDbContextType = defaultRepositoryDbContextType; - return this; + return AddDefaultRepositories(includeAllEntities); + } + + public ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(bool includeAllEntities = false) + { + return AddDefaultRepositories(typeof(TDefaultRepositoryDbContext), includeAllEntities); } public ICommonDbContextRegistrationOptionsBuilder AddRepository() diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbRepositoryRegistrar.cs b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbRepositoryRegistrar.cs index 5aa002362a..e3ec7abef2 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbRepositoryRegistrar.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbRepositoryRegistrar.cs @@ -15,7 +15,8 @@ namespace Volo.Abp.MongoDB.DependencyInjection protected override IEnumerable GetEntityTypes(Type dbContextType) { - var mongoDbContext = (IAbpMongoDbContext)Activator.CreateInstance(dbContextType); + //TODO: Instead of getting from Options.OriginalDbContextType, we may consider to add entities as properties to the dbcontext, just like EF Core! + var mongoDbContext = (IAbpMongoDbContext)Activator.CreateInstance(Options.OriginalDbContextType); return mongoDbContext.GetMappings().Select(m => m.EntityType); } diff --git a/test/Volo.Abp.MongoDB.Tests/Volo.Abp.MongoDB.Tests.csproj b/test/Volo.Abp.MongoDB.Tests/Volo.Abp.MongoDB.Tests.csproj new file mode 100644 index 0000000000..f737d7a297 --- /dev/null +++ b/test/Volo.Abp.MongoDB.Tests/Volo.Abp.MongoDB.Tests.csproj @@ -0,0 +1,26 @@ + + + + netcoreapp2.0 + Volo.Abp.MongoDB.Tests + Volo.Abp.MongoDB.Tests + true + false + false + false + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/AbpMongoDbTestModule.cs b/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/AbpMongoDbTestModule.cs new file mode 100644 index 0000000000..6e714b1b2f --- /dev/null +++ b/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/AbpMongoDbTestModule.cs @@ -0,0 +1,43 @@ +using Microsoft.Extensions.DependencyInjection; +using Mongo2Go; +using Volo.Abp.Autofac; +using Volo.Abp.Data; +using Volo.Abp.Modularity; +using Volo.Abp.TestApp; +using Volo.Abp.TestApp.MongoDb; + +namespace Volo.Abp.MongoDB +{ + [DependsOn( + typeof(AbpAutofacModule), + typeof(AbpMongoDbModule), + typeof(AbpTestBaseModule), + typeof(TestAppModule) + )] + public class AbpMongoDbTestModule : AbpModule + { + private MongoDbRunner _mongoDbRunner; + + public override void ConfigureServices(IServiceCollection services) + { + _mongoDbRunner = MongoDbRunner.Start(); + + services.Configure(options => + { + options.ConnectionStrings.Default = _mongoDbRunner.ConnectionString; + }); + + services.AddMongoDbContext(options => + { + options.AddDefaultRepositories(); + }); + + services.AddAssemblyOf(); + } + + public override void OnApplicationShutdown(ApplicationShutdownContext context) + { + _mongoDbRunner.Dispose(); + } + } +} diff --git a/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/MongoDbTestBase.cs b/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/MongoDbTestBase.cs new file mode 100644 index 0000000000..22cae9b7d6 --- /dev/null +++ b/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/MongoDbTestBase.cs @@ -0,0 +1,10 @@ +namespace Volo.Abp.MongoDB +{ + public abstract class MongoDbTestBase : AbpIntegratedTest + { + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) + { + options.UseAutofac(); + } + } +} \ No newline at end of file diff --git a/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/MongoDb_Repository_Tests.cs b/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/MongoDb_Repository_Tests.cs new file mode 100644 index 0000000000..aab704f129 --- /dev/null +++ b/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/MongoDb_Repository_Tests.cs @@ -0,0 +1,26 @@ +using System; +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.TestApp; +using Volo.Abp.TestApp.Domain; +using Xunit; + +namespace Volo.Abp.MongoDB +{ + public class MongoDb_Repository_Tests : MongoDbTestBase + { + private readonly IRepository _personRepository; + + public MongoDb_Repository_Tests() + { + _personRepository = GetRequiredService>(); + } + + [Fact] + public async Task GetAsync() + { + (await _personRepository.GetAsync(TestDataBuilder.UserDouglasId)).ShouldNotBeNull(); + } + } +} diff --git a/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/ITestAppMongoDbContext.cs b/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/ITestAppMongoDbContext.cs new file mode 100644 index 0000000000..2b0a01eed1 --- /dev/null +++ b/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/ITestAppMongoDbContext.cs @@ -0,0 +1,11 @@ +using Volo.Abp.Data; +using Volo.Abp.MongoDB; + +namespace Volo.Abp.TestApp.MongoDb +{ + [ConnectionStringName("TestApp")] + public interface ITestAppMongoDbContext : IAbpMongoDbContext + { + + } +} \ No newline at end of file diff --git a/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/TestAppMongoDbContext.cs b/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/TestAppMongoDbContext.cs new file mode 100644 index 0000000000..1018012ae2 --- /dev/null +++ b/test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/TestAppMongoDbContext.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; +using Volo.Abp.Data; +using Volo.Abp.MongoDB; +using Volo.Abp.TestApp.Domain; + +namespace Volo.Abp.TestApp.MongoDb +{ + [ConnectionStringName("TestApp")] + public class TestAppMongoDbContext : AbpMongoDbContext, ITestAppMongoDbContext + { + public override IReadOnlyList GetMappings() + { + return new[] + { + new MongoEntityMapping(typeof(Person), "People") + }; + } + } +} diff --git a/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs b/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs index edc94b51ff..dae58441b8 100644 --- a/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs +++ b/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs @@ -9,6 +9,8 @@ namespace Volo.Abp.TestApp { public static Guid TenantId1 { get; } = new Guid("55687dce-595c-41b4-a024-2a5e991ac8f4"); public static Guid TenantId2 { get; } = new Guid("f522d19f-5a86-4278-98fb-0577319c544a"); + public static Guid UserDouglasId { get; } = Guid.NewGuid(); + public static Guid UserJohnDeletedId { get; } = Guid.NewGuid(); private readonly IBasicRepository _personRepository; @@ -24,13 +26,13 @@ namespace Volo.Abp.TestApp private void AddPeople() { - var douglas = new Person(Guid.NewGuid(), "Douglas", 42); + var douglas = new Person(UserDouglasId, "Douglas", 42); douglas.Phones.Add(new Phone(douglas.Id, "123456789")); douglas.Phones.Add(new Phone(douglas.Id, "123456780", PhoneType.Home)); _personRepository.Insert(douglas); - _personRepository.Insert(new Person(Guid.NewGuid(), "John-Deleted", 33) { IsDeleted = true }); + _personRepository.Insert(new Person(UserJohnDeletedId, "John-Deleted", 33) { IsDeleted = true }); var tenant1Person1 = new Person(Guid.NewGuid(), TenantId1 + "-Person1", 42, TenantId1); var tenant1Person2 = new Person(Guid.NewGuid(), TenantId1 + "-Person2", 43, TenantId1);