Esempio n. 1
0
 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)));
 }
Esempio n. 2
0
        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")).AnyContext();

                var findResultsWithSpecifications = await sut.FindAllAsync(new[] { new StubHasTenantSpecification("TestTenant") }).AnyContext();

                var findResultsWithTenantSpecfication = await sut.FindAllAsync(
                    new StubHasTenantSpecification("TestTenant"),
                    new FindOptions <StubEntity>(take : 5)).AnyContext();

                // 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);
            }
        }
Esempio n. 3
0
        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).AnyContext();

                var findResult = await sut.FindOneAsync(entity.Id).AnyContext();

                // assert
                Assert.NotNull(result.entity);
                Assert.NotNull(findResult);
                Assert.True(findResult.FirstName == "FirstName20");
                await mediator.Received().Publish(Arg.Any <IDomainEvent>()).AnyContext();
            }
        }
Esempio n. 4
0
        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().AnyContext();

                // assert
                Assert.NotNull(findResults);
                Assert.True(findResults.Count() == 20);
            }
        }
Esempio n. 5
0
        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"))).AnyContext();

                // assert
                Assert.NotNull(findResults);
                Assert.True(findResults.Count() == 1);
            }
        }
Esempio n. 6
0
        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();
        }
Esempio n. 7
0
        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")).AnyContext();

                var findResultUnknownId = await sut.FindOneAsync(new Guid("00000000-0000-0000-0000-000000000050")).AnyContext();

                // assert
                Assert.NotNull(findResult);
                Assert.True(findResult.FirstName == "FirstName1");
                Assert.Null(findResultUnknownId);
            }
        }
Esempio n. 8
0
        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>()).AnyContext();
            }
        }