public void Paging2Test()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var repository = repositoryContext.GetRepository <int, Customer>();
                var customers  = Customer.CreateMany(1000);
                foreach (var customer in customers)
                {
                    repository.Add(customer);
                }
                repositoryContext.Commit();

                var selectedCustomers = customers.Where(x => x.Id > 20000000).OrderByDescending(x => x.Id);
                var idList            = selectedCustomers.Select(x => x.Id).Skip(15).Take(15).ToList();
                var totalRecords      = selectedCustomers.Count();
                var totalPages        = (totalRecords + 14) / 15;

                var pagedResult = repository.FindAll(c => c.Id > 20000000,
                                                     new SortSpecification <int, Customer> {
                    { x => x.Id, SortOrder.Descending }
                }, 2, 15);

                Assert.Equal(totalRecords, pagedResult.TotalRecords);
                Assert.Equal(totalPages, pagedResult.TotalPages);
                Assert.Equal(2, pagedResult.PageNumber);
                Assert.Equal(15, pagedResult.PageSize);
                Assert.True(CompareIds(idList, pagedResult.As <int, Customer>().Select(x => x.Id).ToList()));
            }
        }
        public void PagingTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var repository = repositoryContext.GetRepository <int, Customer>();
                var customers  = Customer.CreateMany(21);
                foreach (var customer in customers)
                {
                    repository.Add(customer);
                }
                repositoryContext.Commit();

                var idList = customers.OrderBy(x => x.Email).Select(x => x.Id).Skip(5).Take(5).ToList();

                var pagedResult = repository.FindAll(new SortSpecification <int, Customer> {
                    { x => x.Email, SortOrder.Ascending }
                }, 2, 5);

                Assert.Equal(21, pagedResult.TotalRecords);
                Assert.Equal(5, pagedResult.TotalPages);
                Assert.Equal(2, pagedResult.PageNumber);
                Assert.Equal(5, pagedResult.PageSize);
                Assert.True(CompareIds(idList, pagedResult.As <int, Customer>().Select(x => x.Id).ToList()));
            }
        }
        public void FindAllWithSortTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var repository = repositoryContext.GetRepository <int, Customer>();
                var customers  = Customer.CreateMany(500);

                foreach (var cust in customers)
                {
                    repository.Add(cust);
                }
                repositoryContext.Commit();

                var sorted = customers.OrderBy(x => x.Email).OrderByDescending(x => x.Name).ToList();

                var result = repository.FindAll(x => true, new SortSpecification <int, Customer> {
                    { _ => _.Email, SortOrder.Ascending }, { _ => _.Name, SortOrder.Descending }
                }).ToList();

                bool match = true;
                var  total = sorted.Count();
                for (var i = 0; i < total; i++)
                {
                    if (sorted[i].Email != result[i].Email || sorted[i].Name != result[i].Name)
                    {
                        match = false;
                        break;
                    }
                }

                Assert.True(match);
            }
        }
        public void Save2AggregateRootsTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var customers  = Customer.CreateMany(2);
                var repository = repositoryContext.GetRepository <int, Customer>();
                customers.ToList().ForEach(customer => repository.Add(customer));
                repositoryContext.Commit();

                var customersCount = repository.FindAll().Count();

                Assert.Equal(2, customersCount);
            }
        }
        public void SaveAggregateRootTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var customer   = Customer.CreateOne(1, "Sunny Chen", "*****@*****.**");
                var repository = repositoryContext.GetRepository <int, Customer>();
                repository.Add(customer);
                repositoryContext.Commit();

                var customersCount = repository.FindAll().Count();

                Assert.Equal(1, customersCount);
            }
        }
        public void FindAllTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var repository = repositoryContext.GetRepository <int, Customer>();
                var customers  = Customer.CreateMany(1000);
                foreach (var cust in customers)
                {
                    repository.Add(cust);
                }
                repositoryContext.Commit();

                var result = repository.FindAll();

                Assert.Equal(1000, result.Count());
            }
        }
        public void UpdateByKeyTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var customer   = Customer.CreateOne(1, "Sunny Chen", "*****@*****.**");
                var repository = repositoryContext.GetRepository <int, Customer>();
                repository.Add(customer);
                repositoryContext.Commit();

                customer.Email = "*****@*****.**";
                repository.UpdateByKey(1, customer);
                repositoryContext.Commit();

                var updatedCustomer = repository.FindByKey(1);

                Assert.Equal("*****@*****.**", updatedCustomer.Email);
            }
        }
        public void FindAllWithPredicateTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var repository = repositoryContext.GetRepository <int, Customer>();
                var customers  = Customer.CreateMany(500);

                foreach (var cust in customers)
                {
                    repository.Add(cust);
                }
                repositoryContext.Commit();

                var subset = customers.Where(x => x.Id > 1000000);

                var result = repository.FindAll(x => x.Id > 1000000);

                Assert.Equal(subset.Count(), result.Count());
            }
        }
        public void AddNavigationPropertyTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var repository = repositoryContext.GetRepository <int, Customer>();
                var customer   = Customer.CreateOne();
                var id         = customer.Id;
                customer.Addresses = new List <Address> {
                    new Address {
                        Country = "China", State = "SH", City = "Shanghai", Street = "SF", ZipCode = "200000", Id = 1
                    }
                };
                repository.Add(customer);

                repositoryContext.Commit();

                var retrieved = repository.FindByKey(id);

                Assert.Single(retrieved.Addresses);
            }
        }