Browse Source

Added first mongodb test.

pull/272/head
Halil İbrahim Kalkan 8 years ago
parent
commit
b153d7ea1e
  1. 7
      Volo.Abp.sln
  2. 12
      src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs
  3. 3
      src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbRepositoryRegistrar.cs
  4. 26
      test/Volo.Abp.MongoDB.Tests/Volo.Abp.MongoDB.Tests.csproj
  5. 43
      test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/AbpMongoDbTestModule.cs
  6. 10
      test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/MongoDbTestBase.cs
  7. 26
      test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/MongoDb_Repository_Tests.cs
  8. 11
      test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/ITestAppMongoDbContext.cs
  9. 19
      test/Volo.Abp.MongoDB.Tests/Volo/Abp/TestApp/MongoDb/TestAppMongoDbContext.cs
  10. 6
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs

7
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}

12
src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs

@ -62,11 +62,6 @@ namespace Volo.Abp.DependencyInjection
return this;
}
public ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories<TDefaultRepositoryDbContext>(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<TDefaultRepositoryDbContext>(bool includeAllEntities = false)
{
return AddDefaultRepositories(typeof(TDefaultRepositoryDbContext), includeAllEntities);
}
public ICommonDbContextRegistrationOptionsBuilder AddRepository<TEntity, TRepository>()

3
src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbRepositoryRegistrar.cs

@ -15,7 +15,8 @@ namespace Volo.Abp.MongoDB.DependencyInjection
protected override IEnumerable<Type> 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);
}

26
test/Volo.Abp.MongoDB.Tests/Volo.Abp.MongoDB.Tests.csproj

@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>Volo.Abp.MongoDB.Tests</AssemblyName>
<PackageId>Volo.Abp.MongoDB.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />
<ProjectReference Include="..\..\src\Volo.Abp.MongoDB\Volo.Abp.MongoDB.csproj" />
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" />
<ProjectReference Include="..\Volo.Abp.TestApp\Volo.Abp.TestApp.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Mongo2Go" Version="2.2.1" />
</ItemGroup>
</Project>

43
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<DbConnectionOptions>(options =>
{
options.ConnectionStrings.Default = _mongoDbRunner.ConnectionString;
});
services.AddMongoDbContext<TestAppMongoDbContext>(options =>
{
options.AddDefaultRepositories<ITestAppMongoDbContext>();
});
services.AddAssemblyOf<AbpMongoDbTestModule>();
}
public override void OnApplicationShutdown(ApplicationShutdownContext context)
{
_mongoDbRunner.Dispose();
}
}
}

10
test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/MongoDbTestBase.cs

@ -0,0 +1,10 @@
namespace Volo.Abp.MongoDB
{
public abstract class MongoDbTestBase : AbpIntegratedTest<AbpMongoDbTestModule>
{
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options)
{
options.UseAutofac();
}
}
}

26
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<Person, Guid> _personRepository;
public MongoDb_Repository_Tests()
{
_personRepository = GetRequiredService<IRepository<Person, Guid>>();
}
[Fact]
public async Task GetAsync()
{
(await _personRepository.GetAsync(TestDataBuilder.UserDouglasId)).ShouldNotBeNull();
}
}
}

11
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
{
}
}

19
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<MongoEntityMapping> GetMappings()
{
return new[]
{
new MongoEntityMapping(typeof(Person), "People")
};
}
}
}

6
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<Person, Guid> _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);

Loading…
Cancel
Save