Browse Source

Moved some EF core tests to a common library to share with mongodb tests.

pull/272/head
Halil İbrahim Kalkan 9 years ago
parent
commit
a4badfb9c9
  1. 80
      test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/MultiTenant_Filter_Tests.cs
  2. 68
      test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/SoftDelete_Filter_Tests.cs
  3. 8
      test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DomainEvents/DomainEvents_Tests.cs
  4. 2
      test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DomainEvents/EntityChangeEvents_Tests.cs
  5. 86
      test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/EntityFrameworkCoreTestBase.cs
  6. 8
      test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/EfCore_Repository_Basic_Tests.cs
  7. 9
      test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Basic_Tests.cs
  8. 41
      test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Queryable_Tests.cs
  9. 79
      test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Tests.cs
  10. 7
      test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Transactions/Transaction_Tests.cs
  11. 2
      test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/MongoDb_Repository_Basic_Tests.cs
  12. 4
      test/Volo.Abp.TestApp/Volo.Abp.TestApp.csproj
  13. 23
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs
  14. 13
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/MongoDbTestBase.cs
  15. 86
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/MultiTenant_Filter_Tests.cs
  16. 12
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Basic_Tests.cs
  17. 31
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Queryable_Tests.cs
  18. 73
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/SoftDelete_Filter_Tests.cs
  19. 101
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/TestAppTestBase.cs

80
test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/MultiTenant_Filter_Tests.cs

@ -1,85 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.MultiTenancy;
using Volo.Abp.TestApp;
using Volo.Abp.TestApp.Domain;
using Xunit;
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.EntityFrameworkCore.DataFiltering
{
public class MultiTenant_Filter_Tests : EntityFrameworkCoreTestBase //TODO: This class is same of Volo.Abp.MemoryDb.DataFilters.MemoryDb_MultiTenant_Filter_Tests. Can we share source code?
public class MultiTenant_Filter_Tests : MultiTenant_Filter_Tests<AbpEntityFrameworkCoreTestModule>
{
private ICurrentTenant _fakeCurrentTenant;
private readonly IRepository<Person, Guid> _personRepository;
private readonly IDataFilter<IMultiTenant> _multiTenantFilter;
public MultiTenant_Filter_Tests()
{
_personRepository = GetRequiredService<IRepository<Person, Guid>>();
_multiTenantFilter = GetRequiredService<IDataFilter<IMultiTenant>>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
_fakeCurrentTenant = Substitute.For<ICurrentTenant>();
services.AddSingleton(_fakeCurrentTenant);
}
[Fact]
public void Should_Get_Person_For_Current_Tenant()
{
WithUnitOfWork(() =>
{
//TenantId = null
_fakeCurrentTenant.Id.Returns((Guid?)null);
var people = _personRepository.ToList();
people.Count.ShouldBe(1);
people.Any(p => p.Name == "Douglas").ShouldBeTrue();
//TenantId = TestDataBuilder.TenantId1
_fakeCurrentTenant.Id.Returns(TestDataBuilder.TenantId1);
people = _personRepository.ToList();
people.Count.ShouldBe(2);
people.Any(p => p.Name == TestDataBuilder.TenantId1 + "-Person1").ShouldBeTrue();
people.Any(p => p.Name == TestDataBuilder.TenantId1 + "-Person2").ShouldBeTrue();
//TenantId = TestDataBuilder.TenantId2
_fakeCurrentTenant.Id.Returns(TestDataBuilder.TenantId2);
people = _personRepository.ToList();
people.Count.ShouldBe(0);
});
}
[Fact]
public void Should_Get_All_People_When_MultiTenant_Filter_Is_Disabled()
{
WithUnitOfWork(() =>
{
List<Person> people;
using (_multiTenantFilter.Disable())
{
//Filter disabled manually
people = _personRepository.ToList();
people.Count.ShouldBe(3);
}
//Filter re-enabled automatically
people = _personRepository.ToList();
people.Count.ShouldBe(1);
});
}
}
}

68
test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/SoftDelete_Filter_Tests.cs

@ -1,71 +1,9 @@
using System;
using System.Linq;
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.TestApp.Domain;
using Xunit;
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.EntityFrameworkCore.DataFiltering
{
public class SoftDelete_Filter_Tests : EntityFrameworkCoreTestBase
public class SoftDelete_Filter_Tests : SoftDelete_Filter_Tests<AbpEntityFrameworkCoreTestModule>
{
private readonly IRepository<Person, Guid> _personRepository;
private readonly IDataFilter _dataFilter;
public SoftDelete_Filter_Tests()
{
_personRepository = GetRequiredService<IRepository<Person, Guid>>();
_dataFilter = GetRequiredService<IDataFilter>();
}
[Fact]
public void Should_Not_Get_Deleted_Entities_By_Default()
{
WithUnitOfWork(() =>
{
var people = _personRepository.ToList();
people.Count.ShouldBe(1);
people.Any(p => p.Name == "Douglas").ShouldBeTrue();
});
}
[Fact]
public void Should_Get_Deleted_Entities_When_Filter_Is_Disabled()
{
WithUnitOfWork(() =>
{
//Soft delete is enabled by default
var people = _personRepository.ToList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeFalse();
using (_dataFilter.Disable<ISoftDelete>())
{
//Soft delete is disabled
people = _personRepository.ToList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeTrue();
using (_dataFilter.Enable<ISoftDelete>())
{
//Soft delete is enabled again
people = _personRepository.ToList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeFalse();
}
//Soft delete is disabled (restored previous state)
people = _personRepository.ToList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeTrue();
}
//Soft delete is enabled (restored previous state)
people = _personRepository.ToList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeFalse();
});
}
}
}

8
test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DomainEvents/DomainEvents_Tests.cs

@ -0,0 +1,8 @@
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.EntityFrameworkCore.DomainEvents
{
public class DomainEvents_Tests : DomainEvents_Tests<AbpEntityFrameworkCoreTestModule>
{
}
}

2
test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/EntityChangeEvents_Tests.cs → test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DomainEvents/EntityChangeEvents_Tests.cs

@ -6,7 +6,7 @@ using Volo.Abp.EventBus;
using Volo.Abp.TestApp.Domain;
using Xunit;
namespace Volo.Abp.EntityFrameworkCore
namespace Volo.Abp.EntityFrameworkCore.DomainEvents
{
public class EntityChangeEvents_Tests : EntityFrameworkCoreTestBase
{

86
test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/EntityFrameworkCoreTestBase.cs

@ -11,91 +11,5 @@ namespace Volo.Abp.EntityFrameworkCore
{
options.UseAutofac();
}
//TODO: Move WithUnitOfWork to a common place
#region WithUnitOfWork
protected virtual void WithUnitOfWork(Action action)
{
WithUnitOfWork(new UnitOfWorkOptions(), action);
}
protected virtual void WithUnitOfWork(UnitOfWorkOptions options, Action action)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
action();
uow.Complete();
}
}
}
protected virtual Task WithUnitOfWorkAsync(Func<Task> func)
{
return WithUnitOfWorkAsync(new UnitOfWorkOptions(), func);
}
protected virtual async Task WithUnitOfWorkAsync(UnitOfWorkOptions options, Func<Task> action)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
await action();
await uow.CompleteAsync();
}
}
}
protected virtual TResult WithUnitOfWork<TResult>(Func<TResult> func)
{
return WithUnitOfWork(new UnitOfWorkOptions(), func);
}
protected virtual TResult WithUnitOfWork<TResult>(UnitOfWorkOptions options, Func<TResult> func)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
var result = func();
uow.Complete();
return result;
}
}
}
protected virtual Task<TResult> WithUnitOfWorkAsync<TResult>(Func<Task<TResult>> func)
{
return WithUnitOfWorkAsync(new UnitOfWorkOptions(), func);
}
protected virtual async Task<TResult> WithUnitOfWorkAsync<TResult>(UnitOfWorkOptions options, Func<Task<TResult>> func)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
var result = await func();
await uow.CompleteAsync();
return result;
}
}
}
#endregion
}
}

8
test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/EfCore_Repository_Basic_Tests.cs

@ -1,8 +0,0 @@
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.EntityFrameworkCore.Repositories
{
public class EfCore_Repository_Basic_Tests : Repository_Basic_Tests<AbpEntityFrameworkCoreTestModule>
{
}
}

9
test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Basic_Tests.cs

@ -0,0 +1,9 @@
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.EntityFrameworkCore.Repositories
{
public class Repository_Basic_Tests : Repository_Basic_Tests<AbpEntityFrameworkCoreTestModule>
{
}
}

41
test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Queryable_Tests.cs

@ -0,0 +1,41 @@
using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext;
using Volo.Abp.TestApp.Testing;
using Xunit;
namespace Volo.Abp.EntityFrameworkCore.Repositories
{
public class Repository_Queryable_Tests : Repository_Queryable_Tests<AbpEntityFrameworkCoreTestModule>
{
private readonly IRepository<BookInSecondDbContext, Guid> _bookRepository;
private readonly IRepository<PhoneInSecondDbContext> _phoneInSecondDbContextRepository;
public Repository_Queryable_Tests()
{
_bookRepository = ServiceProvider.GetRequiredService<IRepository<BookInSecondDbContext, Guid>>();
_phoneInSecondDbContextRepository = ServiceProvider.GetRequiredService<IRepository<PhoneInSecondDbContext>>();
}
[Fact]
public void GetBookList()
{
WithUnitOfWork(() =>
{
_bookRepository.Any().ShouldBeTrue();
});
}
[Fact]
public void GetPhoneInSecondDbContextList()
{
WithUnitOfWork(() =>
{
_phoneInSecondDbContextRepository.Any().ShouldBeTrue();
});
}
}
}

79
test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Tests.cs

@ -1,79 +0,0 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext;
using Volo.Abp.TestApp.Domain;
using Xunit;
namespace Volo.Abp.EntityFrameworkCore.Repositories
{
//TODO: Remove WithUnitOfWork
//TODO: Share same abstract base class for repository tests (with mongodb and others)
public class Repository_Tests : EntityFrameworkCoreTestBase
{
private readonly IRepository<Person, Guid> _personRepository;
private readonly IRepository<BookInSecondDbContext, Guid> _bookRepository;
private readonly IRepository<PhoneInSecondDbContext> _phoneInSecondDbContextRepository;
private readonly ICityRepository _cityRepository;
public Repository_Tests()
{
_personRepository = ServiceProvider.GetRequiredService<IRepository<Person, Guid>>();
_bookRepository = ServiceProvider.GetRequiredService<IRepository<BookInSecondDbContext, Guid>>();
_phoneInSecondDbContextRepository = ServiceProvider.GetRequiredService<IRepository<PhoneInSecondDbContext>>();
_cityRepository = GetRequiredService<ICityRepository>();
}
[Fact]
public void GetPersonList()
{
WithUnitOfWork(() =>
{
_personRepository.Any().ShouldBeTrue();
});
}
[Fact]
public void GetBookList()
{
WithUnitOfWork(() =>
{
_bookRepository.Any().ShouldBeTrue();
});
}
[Fact]
public void GetPhoneInSecondDbContextList()
{
WithUnitOfWork(() =>
{
_phoneInSecondDbContextRepository.Any().ShouldBeTrue();
});
}
[Fact]
public async Task GetPeopleInTheCityAsync()
{
var people = await _cityRepository.GetPeopleInTheCityAsync("London");
people.Count.ShouldBeGreaterThan(0);
}
[Fact]
public async Task InsertAsync()
{
await WithUnitOfWorkAsync(async () =>
{
var personId = Guid.NewGuid();
await _personRepository.InsertAsync(new Person(personId, "Adam", 42));
var person = await _personRepository.FindAsync(personId);
person.ShouldNotBeNull();
});
}
}
}

7
test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Transaction_Tests.cs → test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Transactions/Transaction_Tests.cs

@ -1,16 +1,17 @@
using System;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.TestApp.Testing;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.EntityFrameworkCore
namespace Volo.Abp.EntityFrameworkCore.Transactions
{
public class Transaction_Tests : EntityFrameworkCoreTestBase
public class Transaction_Tests : TestAppTestBase<AbpEntityFrameworkCoreTestModule>
{
private readonly IBasicRepository<Person, Guid> _personRepository;
private readonly IBasicRepository<BookInSecondDbContext, Guid> _bookRepository;

2
test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/MongoDb_Repository_Basic_Tests.cs

@ -45,7 +45,7 @@ namespace Volo.Abp.MongoDB
}
[Fact]
public async Task InsertAsync()
public override async Task InsertAsync()
{
var person = new Person(Guid.NewGuid(), "New Person", 35);
person.Phones.Add(new Phone(person.Id, "1234567890"));

4
test/Volo.Abp.TestApp/Volo.Abp.TestApp.csproj

@ -19,4 +19,8 @@
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>

23
test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DomainEvents_Tests.cs → test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DomainEvents_Tests.cs

@ -1,23 +1,24 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Shouldly;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus;
using Volo.Abp.Modularity;
using Volo.Abp.TestApp.Domain;
using Xunit;
namespace Volo.Abp.EntityFrameworkCore
namespace Volo.Abp.TestApp.Testing
{
public class DomainEvents_Tests : EntityFrameworkCoreTestBase
public abstract class DomainEvents_Tests<TStartupModule> : TestAppTestBase<TStartupModule> where TStartupModule : IAbpModule
{
private readonly IRepository<Person, Guid> _personRepository;
private readonly IEventBus _eventBus;
protected readonly IRepository<Person, Guid> PersonRepository;
protected readonly IEventBus EventBus;
public DomainEvents_Tests()
protected DomainEvents_Tests()
{
_personRepository = GetRequiredService<IRepository<Person, Guid>>();
_eventBus = GetRequiredService<IEventBus>();
PersonRepository = GetRequiredService<IRepository<Person, Guid>>();
EventBus = GetRequiredService<IEventBus>();
}
[Fact]
@ -27,7 +28,7 @@ namespace Volo.Abp.EntityFrameworkCore
var isTriggered = false;
_eventBus.Register<PersonNameChangedEvent>((data) =>
EventBus.Register<PersonNameChangedEvent>((data) =>
{
data.OldName.ShouldBe("Douglas");
data.Person.Name.ShouldBe("Douglas-Changed");
@ -38,9 +39,9 @@ namespace Volo.Abp.EntityFrameworkCore
await WithUnitOfWorkAsync(async () =>
{
var dougles = await _personRepository.SingleAsync(b => b.Name == "Douglas");
var dougles = PersonRepository.Single(b => b.Name == "Douglas");
dougles.ChangeName("Douglas-Changed");
await _personRepository.UpdateAsync(dougles);
await PersonRepository.UpdateAsync(dougles);
});
//Assert

13
test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/MongoDbTestBase.cs

@ -1,13 +0,0 @@
using Volo.Abp.Modularity;
namespace Volo.Abp.TestApp.Testing
{
public abstract class TestAppTestBase<TStartupModule> : AbpIntegratedTest<TStartupModule>
where TStartupModule : IAbpModule
{
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options)
{
options.UseAutofac();
}
}
}

86
test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/MultiTenant_Filter_Tests.cs

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.TestApp.Domain;
using Xunit;
namespace Volo.Abp.TestApp.Testing
{
public abstract class MultiTenant_Filter_Tests<TStartupModule> : TestAppTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
private ICurrentTenant _fakeCurrentTenant;
private readonly IRepository<Person, Guid> _personRepository;
private readonly IDataFilter<IMultiTenant> _multiTenantFilter;
protected MultiTenant_Filter_Tests()
{
_personRepository = GetRequiredService<IRepository<Person, Guid>>();
_multiTenantFilter = GetRequiredService<IDataFilter<IMultiTenant>>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
_fakeCurrentTenant = Substitute.For<ICurrentTenant>();
services.AddSingleton(_fakeCurrentTenant);
}
[Fact]
public void Should_Get_Person_For_Current_Tenant()
{
WithUnitOfWork(() =>
{
//TenantId = null
_fakeCurrentTenant.Id.Returns((Guid?)null);
var people = _personRepository.ToList();
people.Count.ShouldBe(1);
people.Any(p => p.Name == "Douglas").ShouldBeTrue();
//TenantId = TestDataBuilder.TenantId1
_fakeCurrentTenant.Id.Returns(TestDataBuilder.TenantId1);
people = _personRepository.ToList();
people.Count.ShouldBe(2);
people.Any(p => p.Name == TestDataBuilder.TenantId1 + "-Person1").ShouldBeTrue();
people.Any(p => p.Name == TestDataBuilder.TenantId1 + "-Person2").ShouldBeTrue();
//TenantId = TestDataBuilder.TenantId2
_fakeCurrentTenant.Id.Returns(TestDataBuilder.TenantId2);
people = _personRepository.ToList();
people.Count.ShouldBe(0);
});
}
[Fact]
public void Should_Get_All_People_When_MultiTenant_Filter_Is_Disabled()
{
WithUnitOfWork(() =>
{
List<Person> people;
using (_multiTenantFilter.Disable())
{
//Filter disabled manually
people = _personRepository.ToList();
people.Count.ShouldBe(3);
}
//Filter re-enabled automatically
people = _personRepository.ToList();
people.Count.ShouldBe(1);
});
}
}
}

12
test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Basic_Tests.cs

@ -20,7 +20,6 @@ namespace Volo.Abp.TestApp.Testing
CityRepository = GetRequiredService<ICityRepository>();
}
[Fact]
public async Task FindAsync_Should_Return_Null_For_Not_Found_Entity()
{
@ -50,5 +49,16 @@ namespace Volo.Abp.TestApp.Testing
city.ShouldNotBeNull();
city.Name.ShouldBe("Istanbul");
}
[Fact]
public virtual async Task InsertAsync()
{
var personId = Guid.NewGuid();
await PersonRepository.InsertAsync(new Person(personId, "Adam", 42));
var person = await PersonRepository.FindAsync(personId);
person.ShouldNotBeNull();
}
}
}

31
test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Queryable_Tests.cs

@ -0,0 +1,31 @@
using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Modularity;
using Volo.Abp.TestApp.Domain;
using Xunit;
namespace Volo.Abp.TestApp.Testing
{
public abstract class Repository_Queryable_Tests<TStartupModule> : TestAppTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
protected readonly IRepository<Person, Guid> PersonRepository;
protected Repository_Queryable_Tests()
{
PersonRepository = ServiceProvider.GetRequiredService<IRepository<Person, Guid>>();
}
[Fact]
public void GetPersonList()
{
WithUnitOfWork(() =>
{
PersonRepository.Any().ShouldBeTrue();
});
}
}
}

73
test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/SoftDelete_Filter_Tests.cs

@ -0,0 +1,73 @@
using System;
using System.Linq;
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Modularity;
using Volo.Abp.TestApp.Domain;
using Xunit;
namespace Volo.Abp.TestApp.Testing
{
public abstract class SoftDelete_Filter_Tests<TStartupModule> : TestAppTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
protected readonly IRepository<Person, Guid> PersonRepository;
protected readonly IDataFilter DataFilter;
protected SoftDelete_Filter_Tests()
{
PersonRepository = GetRequiredService<IRepository<Person, Guid>>();
DataFilter = GetRequiredService<IDataFilter>();
}
[Fact]
public void Should_Not_Get_Deleted_Entities_By_Default()
{
WithUnitOfWork(() =>
{
var people = PersonRepository.ToList();
people.Count.ShouldBe(1);
people.Any(p => p.Name == "Douglas").ShouldBeTrue();
});
}
[Fact]
public void Should_Get_Deleted_Entities_When_Filter_Is_Disabled()
{
WithUnitOfWork(() =>
{
//Soft delete is enabled by default
var people = PersonRepository.ToList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeFalse();
using (DataFilter.Disable<ISoftDelete>())
{
//Soft delete is disabled
people = PersonRepository.ToList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeTrue();
using (DataFilter.Enable<ISoftDelete>())
{
//Soft delete is enabled again
people = PersonRepository.ToList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeFalse();
}
//Soft delete is disabled (restored previous state)
people = PersonRepository.ToList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeTrue();
}
//Soft delete is enabled (restored previous state)
people = PersonRepository.ToList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeFalse();
});
}
}
}

101
test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/TestAppTestBase.cs

@ -0,0 +1,101 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.Uow;
namespace Volo.Abp.TestApp.Testing
{
public abstract class TestAppTestBase<TStartupModule> : AbpIntegratedTest<TStartupModule>
where TStartupModule : IAbpModule
{
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options)
{
options.UseAutofac();
}
#region WithUnitOfWork
protected virtual void WithUnitOfWork(Action action)
{
WithUnitOfWork(new UnitOfWorkOptions(), action);
}
protected virtual void WithUnitOfWork(UnitOfWorkOptions options, Action action)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
action();
uow.Complete();
}
}
}
protected virtual Task WithUnitOfWorkAsync(Func<Task> func)
{
return WithUnitOfWorkAsync(new UnitOfWorkOptions(), func);
}
protected virtual async Task WithUnitOfWorkAsync(UnitOfWorkOptions options, Func<Task> action)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
await action();
await uow.CompleteAsync();
}
}
}
protected virtual TResult WithUnitOfWork<TResult>(Func<TResult> func)
{
return WithUnitOfWork(new UnitOfWorkOptions(), func);
}
protected virtual TResult WithUnitOfWork<TResult>(UnitOfWorkOptions options, Func<TResult> func)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
var result = func();
uow.Complete();
return result;
}
}
}
protected virtual Task<TResult> WithUnitOfWorkAsync<TResult>(Func<Task<TResult>> func)
{
return WithUnitOfWorkAsync(new UnitOfWorkOptions(), func);
}
protected virtual async Task<TResult> WithUnitOfWorkAsync<TResult>(UnitOfWorkOptions options, Func<Task<TResult>> func)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
var result = await func();
await uow.CompleteAsync();
return result;
}
}
}
#endregion
}
}
Loading…
Cancel
Save