コード例 #1
0
        public void TestValidCreate()
        {
            using (var connection = Effort.DbConnectionFactory.CreateTransient())
            {
                // arrange
                var dbContext = new CarAdvertDbContext(connection, false);

                var dbContextProvider = Substitute.For <IDbContextProvider>();
                dbContextProvider.Provide().Returns(p => dbContext);

                using (var unitOfWork = new DbContextUnitOfWork(dbContextProvider))
                {
                    var carAdvert = new CarAdvert()
                    {
                        Title = "Audi",
                        Price = 1500,
                        IsNew = true
                    };

                    var repository = new CarAdvertRepository(unitOfWork);

                    // act
                    repository.Create(carAdvert);
                    unitOfWork.Commit();

                    // assert
                    var item = dbContext.CarAdverts.Single();
                    Assert.AreEqual(item.Title, carAdvert.Title);
                    Assert.AreEqual(item.Price, carAdvert.Price);
                    Assert.AreEqual(item.IsNew, carAdvert.IsNew);
                }
            }
        }
コード例 #2
0
        public void TestValidDelete()
        {
            using (var connection = Effort.DbConnectionFactory.CreateTransient())
            {
                // arrange
                var dbContext = new CarAdvertDbContext(connection, false);
                var carAdvert = new CarAdvert()
                {
                    Title = "Audi",
                    Price = 1500,
                    IsNew = true
                };
                dbContext.CarAdverts.Add(carAdvert);
                dbContext.SaveChanges();

                var dbContextProvider = Substitute.For <IDbContextProvider>();
                dbContextProvider.Provide().Returns(p => dbContext);

                using (var unitOfWork = new DbContextUnitOfWork(dbContextProvider))
                {
                    var repository        = new CarAdvertRepository(unitOfWork);
                    var carAdvertToDelete = repository.Get(carAdvert.Id);

                    // act
                    repository.Delete(carAdvertToDelete);
                    unitOfWork.Commit();

                    // assert
                    var isAny = dbContext.CarAdverts.Any();
                    Assert.IsFalse(isAny);
                }
            }
        }
コード例 #3
0
        public void UsingDisposeTest()
        {
            var context = new TestDbContext();
            DbContextUnitOfWork unitOfWork;

            using (unitOfWork = new DbContextUnitOfWork(context))
            {
                //Do something
            }

            Assert.IsNull(unitOfWork.Context);
        }
コード例 #4
0
        public void TestDisposeWithoutContextCreation()
        {
            var dbContext         = Substitute.For <DbContext>();
            var dbContextProvider = Substitute.For <IDbContextProvider>();

            dbContextProvider.Provide().Returns(c => dbContext);
            var uof = new DbContextUnitOfWork(dbContextProvider);

            uof.Dispose();

            dbContext.DidNotReceive().Dispose();
        }
コード例 #5
0
        public void TestProvideContext()
        {
            var dbContext         = Substitute.For <DbContext>();
            var dbContextProvider = Substitute.For <IDbContextProvider>();

            dbContextProvider.Provide().Returns(c => dbContext);
            var uof = new DbContextUnitOfWork(dbContextProvider);

            var context = uof.ProvideContext <DbContext>();

            Assert.AreEqual(context, dbContext);
        }
コード例 #6
0
        public void TestCommit()
        {
            var dbContext         = Substitute.For <DbContext>();
            var dbContextProvider = Substitute.For <IDbContextProvider>();

            dbContextProvider.Provide().Returns(c => dbContext);
            var uof = new DbContextUnitOfWork(dbContextProvider);

            uof.Commit();

            dbContext.Received().SaveChanges();
        }
コード例 #7
0
        public async Task BasicCrud()
        {
            var db      = FarmMasterContext.InMemory();
            var species = new SpeciesManager(db);
            var uow     = new DbContextUnitOfWork <FarmMasterContext>(db);

            var goat = new Species
            {
                GestrationPeriod = TimeSpan.FromDays(121),
                Name             = "Goat"
            };

            // C & R (DbSet)
            using (var scope = uow.Begin())
            {
                var result = await species.CreateAsync(goat);

                Assert.True(result.Succeeded);
                scope.Commit();
            }
            Assert.NotEmpty(db.Species);
            Assert.Same(goat, db.Species.First());

            // U & R (Query)
            using (var scope = uow.Begin())
            {
                goat.GestrationPeriod = TimeSpan.FromDays(1);
                species.Update(goat);
                scope.Commit();
            }
            Assert.Equal(TimeSpan.FromDays(1), species.Query().AsNoTracking().First().GestrationPeriod);

            // D & R (ById exist and not exist)
            var id          = goat.SpeciesId;
            var valueResult = await species.GetByIdAsync(id);

            Assert.True(valueResult.Succeeded);
            Assert.Same(goat, valueResult.Value);

            using (var scope = uow.Begin())
            {
                species.Delete(goat);
                scope.Commit();
            }
            Assert.Empty(db.Species);

            valueResult = await species.GetByIdAsync(id);

            Assert.False(valueResult.Succeeded);
            Assert.Contains("1", valueResult.GatherErrorMessages().First());
        }
コード例 #8
0
        public void SingleScopeCommitTest()
        {
            var db  = UnitTestDbContext.InMemory();
            var uow = new DbContextUnitOfWork <UnitTestDbContext>(db);

            UnitOfWorkScope scope = null;

            using (scope = uow.Begin("Single Commit"))
            {
                db.Add(new Product {
                    Name = "Commitment Ring"
                });
                Assert.True(scope.Commit());
            }

            Assert.Equal(UnitOfWorkScopeState.Commit, scope.State);

            var product = db.Products.First(p => p.Name == "Commitment Ring");

            Assert.NotNull(product);
            Assert.Equal(EntityState.Unchanged, db.Entry(product).State); // Already saved the Added change, so should be Unchanged now.
        }
コード例 #9
0
        public void SingleScopeRollbackAndNoneTest()
        {
            var db  = UnitTestDbContext.InMemory();
            var uow = new DbContextUnitOfWork <UnitTestDbContext>(db);

            // Test .Rollback
            UnitOfWorkScope scope;

            using (scope = uow.Begin("Single Rollback"))
            {
                db.Add(new Product {
                    Name = "Roll-back"
                });
                scope.Rollback("Roll-back corp are defunct");
            }

            Assert.Equal(UnitOfWorkScopeState.Rollback, scope.State);
            Assert.Equal("Single Rollback", scope.Name);
            Assert.True(scope.RollbackReason.Length > 0);

            // Test not setting a state
            using (scope = uow.Begin("Single None"))
            {
                db.Add(new Product {
                    Name = "Void"
                });
            }

            Assert.Equal(UnitOfWorkScopeState.None, scope.State);

            // Test that changes are in fact, not committed
            Assert.Empty(db.ChangeTracker.Entries());
            Assert.Null(db.Products.FirstOrDefault(p => p.Name == "Roll-back"));

            db.SaveChanges();
            Assert.Null(db.Products.FirstOrDefault(p => p.Name == "Void"));
        }