Example #1
0
        public void Should_Audit_When_EntityState_SoftDeleted()
        {
            var options      = DatabaseOptions <FakeDbContext>();
            var fakeEntities = new FakeEntities();

            using (IDbContext context = new FakeDbContext(options))
            {
                var dbSet = context.Set <FakeEntity>();
                dbSet.AddRange(fakeEntities);
                context.SaveChanges();
            }

            using (IDbContext context = new FakeDbContext(options))
            {
                var dbSet = context.Set <FakeEntity>();
                dbSet.RemoveRange(fakeEntities);
                context.Audit();

                var auditEntries = context.GetCurrentEntries();

                Assert.All(auditEntries, prop => Assert.Equal(Principal.Identity.Name, prop.CurrentValues[nameof(FakeEntity.ModifiedBy)]));
                Assert.All(auditEntries, prop => Assert.True((bool)prop.CurrentValues[nameof(FakeEntity.IsDeleted)]));
                Assert.All(auditEntries, prop => Assert.Equal((DateTime)prop.CurrentValues[nameof(FakeEntity.ModifiedTime)], (DateTime)prop.CurrentValues[nameof(FakeEntity.DeletedTime)], TimeSpan.FromMinutes(1)));
            }
        }
        public void EF_Diagnostics_Success_Test()
        {
            var processorObserver = new TracingDiagnosticProcessorObserver(new[]
            {
                new EntityFrameworkCoreDiagnosticProcessor()
            });

            DiagnosticListener.AllListeners.Subscribe(processorObserver);

            using (var tracerContextListener = new FakeIgnoreTracerContextListener())
            {
                try
                {
                    using (var dbContext = new FakeDbContext(_options))
                    {
                        dbContext.Users.Add(new FakeUser("Zhang", "San"));
                        dbContext.SaveChanges();
                    }
                }
                catch
                {
                    // ignored
                }
                Assert.Equal(1, tracerContextListener.Counter);
            }
        }
        public void AddFakeEntityWithTransaction()
        {
            var entities = new List <FakeEntity>
            {
                new FakeEntity {
                    Name = Guid.NewGuid().ToString(), CreatedTime = DateTime.Now
                },
                new FakeEntity {
                    Name = Guid.NewGuid().ToString(), CreatedTime = DateTime.Now
                },
            };

            using (var transaction = _fakeDbContext.Database.BeginTransaction())
            {
                try
                {
                    _fakeDbContext.Set <FakeEntity>().AddRange(entities);
                    _fakeDbContext.SaveChanges();
                    //throw new Exception();
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                }
            }
        }
Example #4
0
        public void Should_Audit_When_EntityState_Modified()
        {
            var options      = DatabaseOptions <FakeDbContext>();
            var fakeEntities = new FakeEntities();

            using (IDbContext context = new FakeDbContext(options))
            {
                var dbSet = context.Set <FakeEntity>();
                dbSet.AddRange(fakeEntities);
                context.SaveChanges();
            }

            using (IDbContext context = new FakeDbContext(options))
            {
                var dbSet = context.Set <FakeEntity>();
                dbSet.AttachRange(fakeEntities);
                fakeEntities.ForEach(entity => entity.EntityName = "Modified");
                context.Audit();

                var auditEntries   = context.GetCurrentEntries();
                var modifyDateTime = (DateTime)auditEntries.First().CurrentValues[nameof(FakeEntity.ModifiedTime)];

                Assert.All(auditEntries, prop => Assert.Equal("Modified", prop.CurrentValues[nameof(FakeEntity.EntityName)]));
                Assert.All(auditEntries, prop => Assert.Equal(Principal.Identity.Name, prop.CurrentValues[nameof(FakeEntity.ModifiedBy)]));
                Assert.All(auditEntries, prop => Assert.Equal(modifyDateTime, (DateTime)prop.CurrentValues[nameof(FakeEntity.ModifiedTime)], TimeSpan.FromMinutes(1)));
            }
        }
        /// <summary>
        /// Ensures that there is fake aggregate roots with the specified count
        /// </summary>
        /// <param name="count">Count of fake aggregate roots</param>
        protected IReadOnlyCollection <FakeAggregateRoot> EnsureHasFakeAggregateRoots(int count)
        {
            for (int i = 0; i < count; i++)
            {
                var fakeAggregateRoot = new FakeAggregateRoot(GenerateRandomName());

                _dbContext
                .Set <FakeAggregateRoot>()
                .Add(fakeAggregateRoot);
            }

            _dbContext.SaveChanges();

            return(_dbContext
                   .Set <FakeAggregateRoot>()
                   .ToList());
        }
Example #6
0
 public void Setup()
 {
     System.Data.Common.DbConnection connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());
     _ctx        = new FakeDbContext(connection);
     _repository = new EmployeeRepository(_ctx);
     _employee   = ObjectMother.GetValidEmployee();
     //Seed
     _employeeSeed = ObjectMother.GetValidEmployee();
     _ctx.Employees.Add(_employeeSeed);
     _ctx.SaveChanges();
 }
Example #7
0
 public void Setup()
 {
     System.Data.Common.DbConnection connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());
     _ctx        = new FakeDbContext(connection);
     _repository = new SpendingRepository(_ctx);
     _spending   = ObjectMother.GetValidSpending();
     //Seed
     _spendingSeed = ObjectMother.GetValidSpending();
     _ctx.Spendings.Add(_spendingSeed);
     _ctx.SaveChanges();
 }
Example #8
0
        public void Setup()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _context    = new FakeDbContext(connection);
            _repository = new EmitenteRepository(_context);
            _emitente   = ObjectMother.GetEmitenteValido();
            //Seed
            _emitenteSeed = ObjectMother.GetEmitenteValido();
            _context.Emitentes.Add(_emitenteSeed);
            _context.SaveChanges();
        }
Example #9
0
        public void SetUp()
        {
            var conexao = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _contexto           = new FakeDbContext(conexao);
            _contaRepositorio   = new ContaRepositorio(_contexto);
            _clienteRepositorio = new ClienteRepositorio(_contexto);
            _conta     = ObjectMother.ObtemContaValida();
            _contaSeed = ObjectMother.ObtemContaValida();
            _contexto.Contas.Add(_contaSeed);
            _contexto.SaveChanges();
        }
Example #10
0
        public void Setup()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _context    = new FakeDbContext(connection);
            _repository = new ProductRepository(_context);
            _product    = ObjectMother.ProductValidWithId();
            //Seed
            _productBase = ObjectMother.ProductValidWithId();
            _context.Products.Add(_productBase);
            _context.SaveChanges();
        }
Example #11
0
        public void Setup()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _ctx        = new FakeDbContext(connection);
            _repository = new UserRepository(_ctx);
            _user       = ObjectMother.GetValidUser();
            //Seed
            _userSeed = ObjectMother.GetValidUser();
            _ctx.Users.Add(_userSeed);
            _ctx.SaveChanges();
        }
Example #12
0
        public void Setup()
        {
            var conexao = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _ctx        = new FakeDbContext(conexao);
            _repository = new IssuerRepository(_ctx);
            _issuer     = ObjectMother.IssuerValidWithIdAndWithAddress();
            //Seed
            _issuerBase = ObjectMother.IssuerValidWithIdAndWithAddress();
            _ctx.Issuers.Add(_issuerBase);
            _ctx.SaveChanges();
        }
        public void Setup()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _context    = new FakeDbContext(connection);
            _repository = new ProdutoRepository(_context);
            _produto    = ObjectMother.ProdutoDefault();
            //Seed
            _produtoSeed = ObjectMother.ProdutoDefault();
            _context.Produtos.Add(_produtoSeed);
            _context.SaveChanges();
        }
Example #14
0
        public void Setup()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _context     = new FakeDbContext(connection);
            _repository  = new BankAccountRepository(_context);
            _bankAccount = ObjectMother.BankAccountWithClientWithId();
            //Seed
            _bankAccountSeed = ObjectMother.BankAccountWithClientWithoutId();
            _context.BankAccounts.Add(_bankAccountSeed);
            _context.SaveChanges();
        }
Example #15
0
        public void Setup()
        {
            var conexao = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _ctx             = new FakeDbContext(conexao);
            _repository      = new ShippingCompanyRepository(_ctx);
            _shippingCompany = ObjectMother.ShippingCompanyValidWithIdWithAddress();
            //Seed
            _shippingCompanyBase = ObjectMother.ShippingCompanyValidWithIdWithAddress();
            _ctx.ShippingCompanies.Add(_shippingCompanyBase);
            _ctx.SaveChanges();
        }
Example #16
0
        public void Setup()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _ctx        = new FakeDbContext(connection);
            _repository = new ClientRepository(_ctx);
            _client     = ObjectMother.GetClientValid();
            //Seed
            _clientSeed = ObjectMother.GetClientValid();
            _ctx.Clients.Add(_clientSeed);
            _ctx.SaveChanges();
        }
        public void Setup()
        {
            var conexao = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _ctx        = new FakeDbContext(conexao);
            _repository = new AddresseReposiotory(_ctx);
            _addressee  = ObjectMother.AddresseeValidWithIdWithAddress();
            //Seed
            _addresseeBase = ObjectMother.AddresseeValidWithIdWithAddress();
            _ctx.Addressees.Add(_addresseeBase);
            _ctx.SaveChanges();
        }
Example #18
0
        public void Setup()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _ctx         = new FakeDbContext(connection);
            _repository  = new TransactionRepository(_ctx);
            _transaction = ObjectMother.GetTransactionCredit();
            //Seed
            _transactionSeed = ObjectMother.GetTransactionDebt();
            _ctx.Transactions.Add(_transactionSeed);
            _ctx.SaveChanges();
        }
        public void DbContext_Works_Success_Test()
        {
            using (var dbContext = new FakeDbContext(_options))
            {
                dbContext.Users.Add(new FakeUser("Zhang", "San"));
                dbContext.SaveChanges();
            }

            using (var dbContext = new FakeDbContext(_options))
            {
                Assert.Single(dbContext.Users);
            }
        }
        public void Setup()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _context    = new FakeDbContext(connection);
            _repository = new PurchaseRepository(_context);
            _purchase   = ObjectMother.GetDefaultPurchase();

            //Seed
            _purchaseBase = ObjectMother.GetDefaultPurchase();
            _context.Purchases.Add(_purchaseBase);
            _context.SaveChanges();
        }
        public void Delete_ExistingEntity_Deleted()
        {
            // Arrange
            const int id = 30;

            var dbContextOptionsBuilder = new DbContextOptionsBuilder <DbContext>().UseInMemoryDatabase(nameof(Delete_ExistingEntity_Deleted));
            var fakeDbContext           = new FakeDbContext(dbContextOptionsBuilder.Options, "dbo");
            var entity = fakeDbContext.Add(new FakeEntity {
                Id = id
            }).Entity;

            fakeDbContext.SaveChanges();

            var repository = new Repository <FakeEntity, int>(fakeDbContext);

            // Act
            repository.Delete(entity);
            fakeDbContext.SaveChanges();

            // Assert
            Assert.False(fakeDbContext.Set <FakeEntity>().Any(x => x.Id == id));
        }
Example #22
0
        public void Setup()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _ctx        = new FakeDbContext(connection);
            _repository = new CheckingAccountRepository(_ctx);
            _account    = ObjectMother.GetCheckingAccountValid();
            //Seed
            _accountSeed = ObjectMother.GetCheckingAccountValid();
            _accountSeed.Transactions.Add(ObjectMother.GetTransactionCredit());
            _ctx.CheckingAccounts.Add(_accountSeed);
            _ctx.SaveChanges();
        }
Example #23
0
        public void Setup()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _context    = new FakeDbContext(connection);
            _repository = new UserRepository(_context);
            _user       = ObjectMother.GetDefaultUser();

            //Seed
            _userBase = ObjectMother.GetDefaultUser();
            _context.Users.Add(_userBase);
            _context.SaveChanges();
        }
Example #24
0
        public void Setup()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _context    = new FakeDbContext(connection);
            _repository = new SessionRepository(_context);
            _session    = ObjectMother.GetDefaultSession();

            //Seed
            _sessionBase = ObjectMother.GetDefaultSession();
            _context.Sessions.Add(_sessionBase);
            _context.SaveChanges();
        }
        public void SetUp()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            Context     = new FakeDbContext(connection);
            _repository = new SeatRepository(Context);
            _seat       = ObjectMother.seatDefault;

            _seatSeed = ObjectMother.seatDefault;

            Context.Seats.Add(_seatSeed);
            Context.SaveChanges();
        }
        public void SetUp()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            Context     = new FakeDbContext(connection);
            _repository = new MovieRepository(Context);
            _movie      = ObjectMother.movieDefault;

            _movieSeed = ObjectMother.movieDefault;

            Context.Movies.Add(_movieSeed);
            Context.SaveChanges();
        }
        public void Update_ExistingEntity_Updated()
        {
            // Arrange
            const int id = 10;

            var dbContextOptionsBuilder = new DbContextOptionsBuilder <DbContext>().UseInMemoryDatabase(nameof(Update_ExistingEntity_Updated));
            var fakeDbContext           = new FakeDbContext(dbContextOptionsBuilder.Options, "dbo");
            var entity = fakeDbContext.Add(new FakeEntity {
                Id = id
            }).Entity;

            fakeDbContext.SaveChanges();

            var repository = new Repository <FakeEntity, int>(fakeDbContext);

            // Act
            entity = repository.Update(entity);
            fakeDbContext.SaveChanges();

            // Assert
            Assert.Equal(id, entity.Id);
        }
Example #28
0
        public void Orders_Repository_GetAllWithSize_ShouldBeOk()
        {
            //Arrange
            _ctx.Orders.Add(_order); // Adiciono mais um (além do seed) na base de dados
            _ctx.SaveChanges();
            var size = 1;
            //Action
            var orders = _repository.GetAll(size).ToList();

            //Assert
            orders.Should().NotBeNull();
            orders.Should().HaveCount(size);
            orders.First().Should().Be(_orderSeed);
        }
        public void Set_Transportador()
        {
            var connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());

            _fakeDbContext = new FakeDbContext(connection);
            _transportador = new Transportador();
            _repository    = new TransportadorRepository(_fakeDbContext);

            _transportadorSeed = ObjectMother.transportadorDefault;

            _fakeDbContext.Transportadores.Add(_transportadorSeed);

            _fakeDbContext.SaveChanges();
        }
Example #30
0
        public void Initialize()
        {
            System.Data.Common.DbConnection connection = DbConnectionFactory.CreatePersistent(Guid.NewGuid().ToString());
            _context       = new FakeDbContext(connection);
            userRepository = new UserRepository(_context);

            User userSeed = new User()
            {
                Password = "******", Username = "******"
            };

            _context.Users.Add(userSeed);
            _context.SaveChanges();
        }
Example #31
0
        public void UpdatingSpecialtyTitleWontThrowException()
        {
            var fakeDbContext = new FakeDbContext();

            var specialty = fakeDbContext.Specialties.FirstOrDefault();
            specialty.Title = "KST";
            fakeDbContext.SaveChanges();

            var expected = "KST";
            var actual = specialty.Title;
            Assert.AreEqual(expected, actual);
        }
Example #32
0
        public void DeletingThesisWillDecreaseThesesCount()
        {
            var fakeDbContext = new FakeDbContext();

            var thesis = fakeDbContext.Theses.FirstOrDefault();
            fakeDbContext.Theses.Remove(thesis);

            fakeDbContext.SaveChanges();

            var expected = 49;
            var actual = fakeDbContext.Theses.Count();
            Assert.AreEqual(expected, actual);
        }