private EntityFrameworkRepository <StubEntity> CreateRepository(StubDbContext dbContext, IMediator mediator = null) { return(new EntityFrameworkRepository <StubEntity>(o => o .LoggerFactory(Substitute.For <ILoggerFactory>()) .Mediator(mediator ?? Substitute.For <IMediator>()) .DbContext(dbContext))); }
public async Task FindAll_WithSpecifications_Test() { using (var context = new StubDbContext(this.DbOptions())) { // arrange this.SeedData(context); var sut = this.CreateRepository(context); // act var findResultsWithSpecification = await sut.FindAllAsync(new StubHasTenantSpecification("TestTenant")); var findResultsWithSpecifications = await sut.FindAllAsync(new[] { new StubHasTenantSpecification("TestTenant") }); var findResultsWithTenantSpecfication = await sut.FindAllAsync( new StubHasTenantSpecification("TestTenant"), new FindOptions <StubEntity>(take : 5)); // assert Assert.NotNull(findResultsWithSpecification); Assert.True(findResultsWithSpecification.Count() == 10); Assert.NotNull(findResultsWithSpecifications); Assert.True(findResultsWithSpecifications.Count() == 10); Assert.NotNull(findResultsWithTenantSpecfication); Assert.True(findResultsWithTenantSpecfication.Count() == 5); } }
public async Task AddOrUpdate_Test() { using (var context = new StubDbContext(this.DbOptions())) { // arrange this.SeedData(context); var mediator = Substitute.For <IMediator>(); var sut = this.CreateRepository(context, mediator); var entity = new StubEntity { Id = new Guid("00000000-0000-0000-0000-000000000020"), TenantId = "TestTenant20", FirstName = "FirstName20", LastName = "LastName20", Age = 20 }; // act var result = await sut.UpsertAsync(entity); var findResult = await sut.FindOneAsync(entity.Id); // assert Assert.NotNull(result.entity); Assert.NotNull(findResult); Assert.True(findResult.FirstName == "FirstName20"); await mediator.Received().Publish(Arg.Any <IDomainEvent>()); } }
public async Task FindAll_Test() { using (var context = new StubDbContext(this.DbOptions())) { // arrange this.SeedData(context); var sut = this.CreateRepository(context); // act var findResults = await sut.FindAllAsync(); // assert Assert.NotNull(findResults); Assert.True(findResults.Count() == 20); } }
public async Task FindAll_WithAndSpecification_Test() { using (var context = new StubDbContext(this.DbOptions())) { // arrange this.SeedData(context); var sut = this.CreateRepository(context); // act var findResults = await sut.FindAllAsync( new StubHasTenantSpecification("TestTenant") .And(new StubHasNameSpecification("FirstName1"))); // assert Assert.NotNull(findResults); Assert.True(findResults.Count() == 1); } }
private void SeedData(StubDbContext context) { // To empty the table and not getting a conflict with Id context.Entities.RemoveRange(context.Entities); var entities = Builder <StubEntity> .CreateListOfSize(20) .All() .With(x => x.Id == Guid.NewGuid()) .TheFirst(10) .With(x => x.TenantId = "TestTenant") .TheNext(10) .With(x => x.TenantId = "NotTestTenant") .Build(); context.Entities.AddRange(entities); context.SaveChanges(); }
public async Task FindById_Test() { using (var context = new StubDbContext(this.DbOptions())) { // arrange this.SeedData(context); var sut = this.CreateRepository(context); // act var findResult = await sut.FindOneAsync(new Guid("00000000-0000-0000-0000-000000000001")); var findResultUnknownId = await sut.FindOneAsync(new Guid("00000000-0000-0000-0000-000000000050")); // assert Assert.NotNull(findResult); Assert.True(findResult.FirstName == "FirstName1"); Assert.Null(findResultUnknownId); } }
public async Task Delete_Test() { using (var context = new StubDbContext(this.DbOptions())) { // arrange this.SeedData(context); var mediator = Substitute.For <IMediator>(); var sut = this.CreateRepository(context, mediator); // act sut.DeleteAsync(new Guid("00000000-0000-0000-0000-000000000001")).Wait(); sut.DeleteAsync(new StubEntity { Id = new Guid("00000000-0000-0000-0000-000000000002") }).Wait(); var findResults = sut.FindAllAsync().Result; // assert Assert.NotNull(findResults); Assert.True(findResults.Count() == 18); await mediator.Received().Publish(Arg.Any <IDomainEvent>()); } }