コード例 #1
0
            public void ReturnsEntityWhenTableContainsEntity()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(1);

                        var customer = repository.SingleOrDefault(x => x.Id == 1);

                        Assert.IsNotNull(customer);
                        Assert.AreEqual(1, customer.Id);
                    }
                }
            }
コード例 #2
0
            public void ReturnsCorrectEntityCount()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(100);
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(101);
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(102);

                        var customerCount = repository.Count(x => x.Id >= 100 && x.Id <= 102);

                        Assert.AreEqual(3, customerCount);
                    }
                }
            }
コード例 #3
0
            public void ReturnsCorrectEntities()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(100);
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(101);
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(102);

                        var customers = repository.GetAll().ToList();

                        Assert.IsTrue(customers.Count >= 3);
                    }
                }
            }
コード例 #4
0
            public void AddsNonExistingEntity()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        var customer = EFTestHelper.CreateCustomer(1235);

                        repository.Attach(customer);

                        dbContext.SaveChanges();

                        var fetchedCustomer = repository.GetByKey(1235);
                        Assert.AreEqual(customer, fetchedCustomer);
                    }
                }
            }
コード例 #5
0
            public void SucceedsWhenEntitiesMatchFilter()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(201);
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(202);
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(203);

                        repository.Delete(x => x.Id >= 201 && x.Id <= 203);

                        dbContext.SaveChanges();

                        Assert.IsNull(repository.GetByKey(201));
                        Assert.IsNull(repository.GetByKey(202));
                        Assert.IsNull(repository.GetByKey(203));
                    }
                }
            }
コード例 #6
0
            public void CorrectlySavesChangesWhenNotInTransaction()
            {
                using (var uow = new UnitOfWork <TestDbContextContainer>())
                {
                    var customerRepository = uow.GetRepository <IDbContextCustomerRepository>();

                    var customer = EFTestHelper.CreateCustomer(401);
                    customerRepository.Add(customer);

                    uow.SaveChanges();
                }

                using (var uow = new UnitOfWork <TestDbContextContainer>())
                {
                    var customerRepository = uow.GetRepository <IDbContextCustomerRepository>();

                    var customer = customerRepository.GetByKey(401);

                    Assert.IsNotNull(customer);
                }
            }
コード例 #7
0
            public void CorrectlyCommitsTransaction()
            {
                using (var uow = new UnitOfWork <TestDbContextContainer>())
                {
                    var customerRepository = uow.GetRepository <IDbContextCustomerRepository>();
                    var productRepository  = uow.GetRepository <IDbContextProductRepository>();
                    var orderRepository    = uow.GetRepository <IDbContextOrderRepository>();

                    uow.BeginTransaction();

                    var customer = EFTestHelper.CreateCustomer(451);
                    customerRepository.Add(customer);

                    var product = EFTestHelper.CreateProduct(451);
                    productRepository.Add(product);

                    var order = new DbContextOrder {
                        OrderCreated = DateTime.Now, Amount = 1, CustomerId = 451, ProductId = 451
                    };
                    orderRepository.Add(order);

                    uow.CommitTransaction();
                }

                using (var uow = new UnitOfWork <TestDbContextContainer>())
                {
                    var customerRepository = uow.GetRepository <IDbContextCustomerRepository>();
                    var productRepository  = uow.GetRepository <IDbContextProductRepository>();
                    var orderRepository    = uow.GetRepository <IDbContextOrderRepository>();

                    var customer = customerRepository.GetByKey(451);
                    Assert.IsNotNull(customer);

                    var product = productRepository.GetByKey(451);
                    Assert.IsNotNull(product);

                    var order = orderRepository.FirstOrDefault(x => x.CustomerId == 451 && x.ProductId == 451);
                    Assert.IsNotNull(order);
                }
            }
コード例 #8
0
            public void UpdatesEntity()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(301);

                        var customer1 = repository.GetByKey(301);
                        customer1.Name = "John Doe";

                        repository.Update(customer1);

                        dbContext.SaveChanges();

                        var customer2 = repository.GetByKey(301);

                        Assert.IsNotNull(customer2);
                        Assert.AreEqual("John Doe", customer2.Name);
                    }
                }
            }
コード例 #9
0
            public void DeletesSpecificEntity()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        EFTestHelper.CreateCustomerIfNotAlreadyExists(201);

                        var customer1 = repository.GetByKey(201);

                        Assert.IsNotNull(customer1);

                        repository.Delete(customer1);

                        dbContext.SaveChanges();

                        var customer2 = repository.GetByKey(201);

                        Assert.IsNull(customer2);
                    }
                }
            }