Browse Source

Added unit tests and fixed repository registration problem.

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
d6e68771f0
  1. 6
      src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs
  2. 2
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/EfCoreRepositoryRegistrar.cs
  3. 2
      src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/EfCoreRepositoryRegistrar.cs
  4. 12
      src/Volo.Abp/Volo/Abp/Data/CommonDbContextRegistrationOptions.cs
  5. 4
      src/Volo.Abp/Volo/Abp/Data/ICommonDbContextRegistrationOptionsBuilder.cs
  6. 2
      src/Volo.Abp/Volo/Abp/Domain/Repositories/IQueryableRepository.cs
  7. 2
      src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs
  8. 150
      test/Volo.Abp.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs
  9. 18
      test/Volo.DependencyInjection.Tests/Microsoft/Extensions/DependencyInjection/ServiceCollectionShouldlyExtensions.cs

6
src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/BlogPostLister.cs

@ -1,8 +1,6 @@
using System;
using System.Globalization;
using System.Linq;
using AbpDesk.Blogging;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;
using Volo.DependencyInjection;
@ -11,10 +9,10 @@ namespace AbpDesk.ConsoleDemo
{
public class BlogPostLister : ITransientDependency
{
private readonly IQueryableRepository<BlogPost, string> _blogPostRepository; //TODO: Should not be needed to string
private readonly IQueryableRepository<BlogPost> _blogPostRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public BlogPostLister(IQueryableRepository<BlogPost, string> blogPostRepository, IUnitOfWorkManager unitOfWorkManager)
public BlogPostLister(IQueryableRepository<BlogPost> blogPostRepository, IUnitOfWorkManager unitOfWorkManager)
{
_blogPostRepository = blogPostRepository;
_unitOfWorkManager = unitOfWorkManager;

2
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/EfCoreRepositoryRegistrar.cs

@ -12,7 +12,7 @@ namespace Volo.Abp.EntityFrameworkCore.DependencyInjection
{
}
public override IEnumerable<Type> GetEntityTypes(Type dbContextType)
protected override IEnumerable<Type> GetEntityTypes(Type dbContextType)
{
return DbContextHelper.GetEntityTypes(dbContextType);
}

2
src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/EfCoreRepositoryRegistrar.cs

@ -12,7 +12,7 @@ namespace Volo.Abp.MongoDB.DependencyInjection
{
}
public override IEnumerable<Type> GetEntityTypes(Type dbContextType)
protected override IEnumerable<Type> GetEntityTypes(Type dbContextType)
{
var mongoDbContext = (AbpMongoDbContext)Activator.CreateInstance(dbContextType);
return mongoDbContext.GetEntityCollectionTypes();

12
src/Volo.Abp/Volo/Abp/Data/CommonDbContextRegistrationOptions.cs

@ -10,26 +10,28 @@ namespace Volo.Abp.Data
{
//TODO: Provide an option to set base repository classes, instead of defaults.
public bool RegisterDefaultRepositories { get; set; }
public bool RegisterDefaultRepositories { get; private set; }
public bool IncludeAllEntitiesForDefaultRepositories { get; set; }
public bool IncludeAllEntitiesForDefaultRepositories { get; private set; }
public Dictionary<Type, Type> CustomRepositories { get; set; }
public Dictionary<Type, Type> CustomRepositories { get; }
public CommonDbContextRegistrationOptions()
{
CustomRepositories = new Dictionary<Type, Type>();
}
public void WithDefaultRepositories(bool includeAllEntities = false)
public ICommonDbContextRegistrationOptionsBuilder WithDefaultRepositories(bool includeAllEntities = false)
{
RegisterDefaultRepositories = true;
IncludeAllEntitiesForDefaultRepositories = includeAllEntities;
return this;
}
public void WithCustomRepository<TEntity, TRepository>()
public ICommonDbContextRegistrationOptionsBuilder WithCustomRepository<TEntity, TRepository>()
{
WithCustomRepository(typeof(TEntity), typeof(TRepository));
return this;
}
private void WithCustomRepository(Type entityType, Type repositoryType)

4
src/Volo.Abp/Volo/Abp/Data/ICommonDbContextRegistrationOptionsBuilder.cs

@ -9,7 +9,7 @@ namespace Volo.Abp.Data
/// Registers repositories only for aggregate root entities by default.
/// set <see cref="includeAllEntities"/> to true to include all entities.
/// </param>
void WithDefaultRepositories(bool includeAllEntities = false);
ICommonDbContextRegistrationOptionsBuilder WithDefaultRepositories(bool includeAllEntities = false);
/// <summary>
/// Registers custom repository for a specific entity.
@ -17,6 +17,6 @@ namespace Volo.Abp.Data
/// </summary>
/// <typeparam name="TEntity">Entity type</typeparam>
/// <typeparam name="TRepository">Repository type</typeparam>
void WithCustomRepository<TEntity, TRepository>();
ICommonDbContextRegistrationOptionsBuilder WithCustomRepository<TEntity, TRepository>();
}
}

2
src/Volo.Abp/Volo/Abp/Domain/Repositories/IQueryableRepository.cs

@ -8,7 +8,7 @@ using Volo.Abp.Domain.Entities;
namespace Volo.Abp.Domain.Repositories
{
public interface IQueryableRepository<TEntity> : IQueryableRepository<TEntity, string>
public interface IQueryableRepository<TEntity> : IQueryableRepository<TEntity, string>, IRepository<TEntity>
where TEntity : class, IEntity<string>
{

2
src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs

@ -52,7 +52,7 @@ namespace Volo.Abp.Domain.Repositories
services.AddDefaultRepository(entityType, repositoryImplementationType);
}
public abstract IEnumerable<Type> GetEntityTypes(Type dbContextType);
protected abstract IEnumerable<Type> GetEntityTypes(Type dbContextType);
protected abstract Type GetRepositoryTypeForDefaultPk(Type dbContextType, Type entityType);

150
test/Volo.Abp.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs

@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Data;
using Volo.Abp.Domain.Entities;
using Xunit;
namespace Volo.Abp.Domain.Repositories
{
public class RepositoryRegistration_Tests
{
[Fact]
public void Should_Register_Default_Repositories_For_AggregateRoots()
{
//Arrange
var services = new ServiceCollection();
var options = new CommonDbContextRegistrationOptions();
options.WithDefaultRepositories();
//Act
new MyTestRepositoryRegistrar(options).AddRepositories(services, typeof(MyFakeDbContext));
//Assert
services.ShouldContainTransient(typeof(IRepository<MyTestAggregateRootWithDefaultPk>), typeof(MyTestDefaultRepository<MyTestAggregateRootWithDefaultPk>));
services.ShouldContainTransient(typeof(IRepository<MyTestAggregateRootWithDefaultPk, string>), typeof(MyTestDefaultRepository<MyTestAggregateRootWithDefaultPk>));
services.ShouldNotContainService(typeof(IRepository<MyTestEntityWithCustomPk, int>));
}
[Fact]
public void Should_Register_Default_Repositories_For_All_Entities()
{
//Arrange
var services = new ServiceCollection();
var options = new CommonDbContextRegistrationOptions();
options.WithDefaultRepositories(true);
//Act
new MyTestRepositoryRegistrar(options).AddRepositories(services, typeof(MyFakeDbContext));
//Assert
services.ShouldContainTransient(typeof(IRepository<MyTestAggregateRootWithDefaultPk>), typeof(MyTestDefaultRepository<MyTestAggregateRootWithDefaultPk>));
services.ShouldContainTransient(typeof(IRepository<MyTestAggregateRootWithDefaultPk, string>), typeof(MyTestDefaultRepository<MyTestAggregateRootWithDefaultPk>));
services.ShouldContainTransient(typeof(IRepository<MyTestEntityWithCustomPk, int>), typeof(MyTestDefaultRepository<MyTestEntityWithCustomPk, int>));
}
[Fact]
public void Should_Register_Custom_Repository()
{
//Arrange
var services = new ServiceCollection();
var options = new CommonDbContextRegistrationOptions();
options.WithDefaultRepositories(true);
options.WithCustomRepository<MyTestAggregateRootWithDefaultPk, MyTestAggregateRootWithDefaultPkCustomRepository>();
//Act
new MyTestRepositoryRegistrar(options).AddRepositories(services, typeof(MyFakeDbContext));
//Assert
services.ShouldContainTransient(typeof(IRepository<MyTestAggregateRootWithDefaultPk>), typeof(MyTestAggregateRootWithDefaultPkCustomRepository));
services.ShouldContainTransient(typeof(IRepository<MyTestAggregateRootWithDefaultPk, string>), typeof(MyTestAggregateRootWithDefaultPkCustomRepository));
services.ShouldContainTransient(typeof(IRepository<MyTestEntityWithCustomPk, int>), typeof(MyTestDefaultRepository<MyTestEntityWithCustomPk, int>));
}
public class MyTestRepositoryRegistrar : RepositoryRegistrarBase<CommonDbContextRegistrationOptions>
{
public MyTestRepositoryRegistrar(CommonDbContextRegistrationOptions options)
: base(options)
{
}
protected override IEnumerable<Type> GetEntityTypes(Type dbContextType)
{
return new[]
{
typeof(MyTestEntityWithCustomPk),
typeof(MyTestAggregateRootWithDefaultPk)
};
}
protected override Type GetRepositoryTypeForDefaultPk(Type dbContextType, Type entityType)
{
return typeof(MyTestDefaultRepository<>).MakeGenericType(entityType);
}
protected override Type GetRepositoryType(Type dbContextType, Type entityType, Type primaryKeyType)
{
return typeof(MyTestDefaultRepository<,>).MakeGenericType(entityType, primaryKeyType);
}
}
public class MyFakeDbContext { }
public class MyTestAggregateRootWithDefaultPk : AggregateRoot
{
}
public class MyTestEntityWithCustomPk : Entity<int>
{
}
public class MyTestDefaultRepository<TEntity> : MyTestDefaultRepository<TEntity, string>, IRepository<TEntity>
where TEntity : class, IEntity<string>
{
}
public class MyTestDefaultRepository<TEntity, TPrimaryKey> : RepositoryBase<TEntity, TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
{
public override TEntity Find(TPrimaryKey id)
{
throw new NotImplementedException();
}
public override TEntity Insert(TEntity entity, bool autoSave = false)
{
throw new NotImplementedException();
}
public override TEntity Update(TEntity entity)
{
throw new NotImplementedException();
}
public override void Delete(TEntity entity)
{
throw new NotImplementedException();
}
}
public class MyTestAggregateRootWithDefaultPkCustomRepository : MyTestDefaultRepository<MyTestAggregateRootWithDefaultPk>
{
}
}
}

18
test/Volo.DependencyInjection.Tests/Microsoft/Extensions/DependencyInjection/ServiceCollectionShouldlyExtensions.cs

@ -6,34 +6,34 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionShouldlyExtensions
{
public static void ShouldContainTransient(this IServiceCollection services, Type type)
public static void ShouldContainTransient(this IServiceCollection services, Type serviceType, Type implementationType = null)
{
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == type);
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceType);
serviceDescriptor.ShouldNotBeNull();
serviceDescriptor.ImplementationType.ShouldBe(type);
serviceDescriptor.ImplementationType.ShouldBe(implementationType ?? serviceType);
serviceDescriptor.ImplementationFactory.ShouldBeNull();
serviceDescriptor.ImplementationInstance.ShouldBeNull();
serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Transient);
}
public static void ShouldContainSingleton(this IServiceCollection services, Type type)
public static void ShouldContainSingleton(this IServiceCollection services, Type serviceType, Type implementationType = null)
{
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == type);
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceType);
serviceDescriptor.ShouldNotBeNull();
serviceDescriptor.ImplementationType.ShouldBe(type);
serviceDescriptor.ImplementationType.ShouldBe(implementationType ?? serviceType);
serviceDescriptor.ImplementationFactory.ShouldBeNull();
serviceDescriptor.ImplementationInstance.ShouldBeNull();
serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Singleton);
}
public static void ShouldContainScoped(this IServiceCollection services, Type type)
public static void ShouldContainScoped(this IServiceCollection services, Type serviceType, Type implementationType = null)
{
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == type);
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceType);
serviceDescriptor.ShouldNotBeNull();
serviceDescriptor.ImplementationType.ShouldBe(type);
serviceDescriptor.ImplementationType.ShouldBe(implementationType ?? serviceType);
serviceDescriptor.ImplementationFactory.ShouldBeNull();
serviceDescriptor.ImplementationInstance.ShouldBeNull();
serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Scoped);

Loading…
Cancel
Save