public void Test20ListCustomersViaAutoMapperOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var log = new List<string>();
                var pageSize = 5;
                var pageNumber = 0;
                Mapper.CreateMap<Customer, TestListCustomerDto>().ForMember(d => d.TotalAllOrders,
                    opt => opt.MapFrom(c => c.SalesOrderHeaders.Sum(x => (decimal?) x.TotalDue) ?? 0));

                var custs = db.Customers.Project().To<TestListCustomerDto>().Decompile();
                var filteredQuery = custs.Where(x => x.CompanyName.Contains("Bike") && x.TotalAllOrders > 0);
                var pagedQuery = filteredQuery.OrderBy(x => x.CompanyName).Skip(pageSize * pageNumber).Take(pageSize);

                db.Database.Log = log.Add;

                //ATTEMPT
                var customers = pagedQuery.ToList();

                //VERIFY
                customers.Count.ShouldBeGreaterThan(0);
                foreach (var line in log)
                {
                    Console.WriteLine(line);
                }
            }
        }
        public void Test05AddressCreateViaServiceOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var setupService = new CreateSetupService(db);
                var service = new CreateService(db);
                var lastCustomer = db.Customers.AsNoTracking().Include(x => x.CustomerAddresses).OrderByDescending(x => x.CustomerID).First();

                //ATTEMPT
                var dto =
                    setupService.GetDto<CrudCustomerAddressDto>()
                        .SetCustomerIdWhenCreatingNewEntry(lastCustomer.CustomerID);
                dto.AddressType = "Unit Test";
                dto.Address.AddressLine1 = "Some street";
                dto.Address.AddressLine2 = Guid.NewGuid().ToString("D");
                dto.Address.City = "some town";
                dto.Address.StateProvince = "a state";
                dto.Address.CountryRegion = "the world";
                dto.Address.PostalCode = "XXX 111";
                var status = service.Create(dto);

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                var newLastCustomer = db.Customers.AsNoTracking().Include( x => x.CustomerAddresses.Select( y => y.Address)).OrderByDescending(x => x.CustomerID).First();
                newLastCustomer.CustomerAddresses.Count.ShouldEqual(lastCustomer.CustomerAddresses.Count+1);
                newLastCustomer.CustomerAddresses.OrderByDescending(x => x.AddressID).First().Address.AddressLine2.ShouldEqual(dto.Address.AddressLine2);
            }
        }
        public void Test10ListCustomersViaLinqPartsNoNullOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var log = new List<string>();
                var pageSize = 5;
                var pageNumber = 0;
                var realCustomers = db.Customers.Where(c => c.SalesOrderHeaders.Any());
                var details = realCustomers.Select(x => new
                {
                    x.CompanyName,
                    ContactName = x.Title + " " + x.FirstName + " " + x.MiddleName + " " + x.LastName + " " + x.Suffix
                });
                var filteredQuery = details.Where(x => x.CompanyName.Contains("Bike"));
                var pagedQuery = filteredQuery.OrderBy(x => x.CompanyName).Skip(pageSize * pageNumber).Take(pageSize);

                db.Database.Log = log.Add;

                //ATTEMPT
                var customers = pagedQuery.ToList();

                //VERIFY
                customers.Count.ShouldBeGreaterThan(0);
                foreach (var line in log)
                {
                    Console.WriteLine(line);
                }
            }
        }
        public void CheckSnapShot(AdventureWorksLt2012 db, int customers = 0, int custAddressesChange = 0, int addressesChange = 0)
        {
            var newSnap = new CustomerSnapShot(db);

            newSnap.NumCustomers.ShouldEqual(NumCustomers + customers, "customers wrong");
            newSnap.NumCustomerAddresses.ShouldEqual(NumCustomerAddresses + custAddressesChange, "CustomerAddresses wrong");
            newSnap.NumAddresses.ShouldEqual(NumAddresses + addressesChange, "addresses wrong");
        }
 public void FixtureSetup()
 {
     //we do this to force EF setup otherwise the timing below fails
     using (var db = new AdventureWorksLt2012())
     {
         db.ProductDescriptions.ToList();
     }
 }
        public void FixtureTearDown()
        {
            //we delete our unit test SaleOrderHeader
            using (var db = new AdventureWorksLt2012())
            {
                var service = new DeleteService(db);

                var status = service.Delete<SalesOrderHeader>(_salesOrderId);
                status.ShouldBeValid();
            }
        }
        public void Test01ListCustomerOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP

                //ATTEMPT
                var customers = db.Customers.ToList();

                //VERIFY
                customers.Count.ShouldBeGreaterThan(0);
            }
        }
        public void Test04ListCustomerSumOrdersOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP

                //ATTEMPT
                var customerSums = db.Customers.Select(c => c.SalesOrderHeaders.Sum(x => (decimal?)x.TotalDue)).ToList();

                //VERIFY
                customerSums.Count.ShouldBeGreaterThan(0);
            }
        }
        public void Test01CustomerNormalFullNameOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP

                //ATTEMPT
                var list = db.Customers.Select(c => c.Title + " " + c.FirstName + " " + c.LastName + " " + c.Suffix).ToList();

                //VERIFY
                list.Count.ShouldBeGreaterThan(0);
            }
        }
        public void Test02CustomerWithComputedFullNameOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP

                //ATTEMPT
                var list = db.Customers.Select(x => x.FullName).Decompile().ToList();

                //VERIFY
                list.Count.ShouldBeGreaterThan(0);
            }
        }
        public void Test05CustomerWithComputedHasSalesOrderOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP

                //ATTEMPT
                var list = db.Customers.Where(x => x.HasBoughtBefore).Decompile().ToList();

                //VERIFY
                list.Count.ShouldBeGreaterThan(0);
                list.Count.ShouldBeLessThan(100);
            }
        }
        public void Test01CheckSetupWeHaveASalesOrderHeaderOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP

                //ATTEMPT

                //VERIFY
                _salesOrderId.ShouldBeGreaterThan(0);
                var soh = db.SalesOrderHeaders.SingleOrDefault(x => x.SalesOrderID == _salesOrderId);
                soh.ShouldNotEqualNull();
                soh.ShipMethod.ShouldEqual("Unit Test");
            }
        }
        public void Test02ListSalesOrdersViaServiceOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var service = new ListService(db);

                //ATTEMPT
                var list = service.GetAll<ListSalesOrderDto>().ToList();

                //VERIFY
                list.Count.ShouldBeGreaterThan(0);
                list[0].CustomerCompanyName.ShouldNotEqualNull();
            }
        }
        public void Test01ListOrderDetailViaServiceOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var service = new ListService(db);

                //ATTEMPT
                var list = service.GetAll<CrudSalesOrderDetailDto>().ToList();

                //VERIFY
                list.Count.ShouldBeGreaterThan(0);
                list.First().ProductName.ShouldNotEqualNull();
            }
        }
        public void Test05ListCustomersViaServiceProductCategoryIdNonNullOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var service = new ListService(db);

                //ATTEMPT
                var list = service.GetAll<Product>().Computed().ToList();

                //VERIFY
                list.Count.ShouldBeGreaterThan(0);
                list.Any( x => x.ProductCategoryIDNonNull != 0).ShouldEqual(true);
            }
        }
        public void Test10AddressWithComputedFullAddressOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP

                //ATTEMPT
                var list = db.Addresses.Select(x => x.FullAddress).Decompile().ToList();

                //VERIFY
                list.Count.ShouldBeGreaterThan(0);
                string.IsNullOrEmpty( list.First()).ShouldEqual(false);
                list.Any( x => x.Contains(", ,")).ShouldEqual(false);
            }
        }
        public void Test05DetailSalesOrderViaServiceOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var service = new DetailService(db);
                var firstOrderHeader = db.SalesOrderHeaders.First();

                //ATTEMPT
                var status = service.GetDetail<CrudSalesOrderDto>(firstOrderHeader.SalesOrderID);

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                status.Result.SalesOrderDetails.Count().ShouldBeGreaterThan(0);
            }
        }
        public void Test02AddressDetailViaServiceOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var service = new DetailService(db);
                var customerWithAddresses = db.Customers.Include( x => x.CustomerAddresses).First(x => x.CustomerAddresses.Count > 1);

                //ATTEMPT
                var status = service.GetDetail<CrudCustomerAddressDto>(customerWithAddresses.CustomerID, customerWithAddresses.CustomerAddresses.First().AddressID);

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                status.Result.Address.ShouldNotEqualNull();
            }
        }
        public void Test01ListCustomersCheckIsOnSaleOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP

                var service = new ListService(db);

                //ATTEMPT
                var list = service.GetAll<ListProductDto>().Computed().ToList();

                //VERIFY
                list.Count.ShouldBeGreaterThan(0);
                list.Where( c => c.SellStartDate < DateTime.Today && (DateTime.Today <= (c.SellEndDate ?? DateTime.Today))).All( x => x.IsOnSale).ShouldEqual(true);
                list.Where(c => !(c.SellStartDate < DateTime.Today && (DateTime.Today <= (c.SellEndDate ?? DateTime.Today)))).All(x => x.IsOnSale).ShouldEqual(false);
            }
        }
        public void Test05UpdateSetupOrderDetailViaServiceOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var service = new UpdateSetupService(db);
                var firstOrderHeader = db.SalesOrderHeaders.First();

                //ATTEMPT
                var status = service.GetOriginal<CrudSalesOrderDto>(firstOrderHeader.SalesOrderID);

                //VERIFY
                status.ShouldBeValid();
                status.Result.ShipToOptions.KeyValueList.Count.ShouldBeGreaterThan(0);

            }
        }
        public void Test02TrackedEntitiesUpdateOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var description = db.ProductDescriptions.OrderByDescending(x => x.ProductDescriptionID).First();
                var originalGuid = description.rowguid;

                //ATTEMPT
                description.Description = Guid.NewGuid().ToString();
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                description.ModifiedDate.Ticks.ShouldEqualWithTolerance(DateTime.UtcNow.Ticks, HalfSecondInTicks);
                description.rowguid.ShouldEqual(originalGuid);
            }
        }
        public void Test05ListCustomersPerformanceOrgOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var service = new ListService(db);
                var log = new List<string>();
                db.Database.Log = log.Add;

                //ATTEMPT
                var query = service.GetAll<ListCustomerOrgDto>().Where(x => x.HasBoughtBefore);
                var customers = query.ToList();

                //VERIFY
                customers.Count.ShouldBeGreaterThan(0);
                customers[0].FullName.ShouldNotEqualNull();
                customers.All(x => x.TotalAllOrders > 0).ShouldEqual(true);
            }
        }
        public void Test01TrackedEntitiesAddOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var description = new ProductDescription
                {
                    Description = Guid.NewGuid().ToString()
                };

                //ATTEMPT
                db.ProductDescriptions.Add(description);
                var status = db.SaveChangesWithChecking();

                //VERIFY
                status.IsValid.ShouldEqual(true, status.Errors);
                description.ModifiedDate.Ticks.ShouldEqualWithTolerance(DateTime.UtcNow.Ticks, HalfSecondInTicks);
                description.rowguid.ShouldNotEqual( new Guid());
            }
        }
        public void Test04ListCustomerNumOrdersOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP

                //ATTEMPT
                var customerSums =
                    db.Customers.Select(
                        x =>
                            new
                            {
                                x.CustomerID,
                                numOrders =
                                    x.SalesOrderHeaders.Sum(y => (int?) y.SalesOrderDetails.Count())
                            }).ToList();

                //VERIFY
                customerSums.Count.ShouldBeGreaterThan(0);
            }
        }
        public void FixtureSetup()
        {
            GenericServicesConfig.UseDelegateDecompilerWhereNeeded = true;

            //we create a SaleOrderHeader to use in these tests
            using (var db = new AdventureWorksLt2012())
            {
                _customerId = db.Customers.OrderBy(x => x.CustomerID).First().CustomerID;
                _productToUse = db.Products.OrderByDescending(x => x.ListPrice).First();
                var dto = new CreateSetupService(db).GetDto<NewOrderDto>();
                dto.CustomerID = _customerId;
                dto.ShipMethod = "Unit Test";
                dto.PurchaseOrderNumber = "Unit Test";
                dto.AccountNumber = "Unit Test";

                var service = new CreateService(db);
                var status = service.Create(dto);
                status.ShouldBeValid();
                _salesOrderId = dto.SalesOrderID;
            }
        }
        public void Test01DetailWithAddressesOk()
        {
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP

                //ATTEMPT
                var customer = db.Customers.Where( x => x.CustomerAddresses.Count == 2).Select(x => new
                {
                    x.CustomerID,
                    addresses = x.CustomerAddresses.Select(y => new
                    {
                        y.AddressType,
                        address =
                            y.Address.AddressLine1 + ", " + y.Address.AddressLine2 + ", " + y.Address.City + ", " + y.Address.StateProvince + ", " + y.Address.PostalCode + ", " + y.Address.CountryRegion
                    })
                }).First();

                //VERIFY
                customer.addresses.Count().ShouldEqual(2);
            }
        }
 private static void TimedNormalAccessButCallDecompileAnyway(int NumTimes, AdventureWorksLt2012 db, int NumTakes)
 {
     using (var timer = new TimerToConsole("Normal access, but calling decompile anyway", NumTimes))
         for (int i = 0; i < NumTimes; i++)
         {
             var item1 =
                 db.Customers.Select(c => c.Title + " " + c.FirstName + " " + c.LastName + " " + c.Suffix)
                     .Take(NumTakes)
                     .Decompile()
                     .ToList();
         }
 }
 private static void TimedDelegateDecompiler(int NumTimes, AdventureWorksLt2012 db, int NumTakes)
 {
     using (var timer = new TimerToConsole("With DelegateDecompiler", NumTimes))
         for (int i = 0; i < NumTimes; i++)
         {
             var item1 =
                 db.Customers.Select(x => x.FullName).Take(NumTakes).Decompile().ToList();
         }
 }
        private static void TimedCheckIfItNeedsDecompile(int NumTimes, AdventureWorksLt2012 db, int NumTakes, bool okToUseDecompileIfNeeded)
        {
            using (var timer = new TimerToConsole("Normal access, but checks if needed. Decompile " + (okToUseDecompileIfNeeded ? "was added" : "was NOT added"), NumTimes))
            {
                var addDecompile = typeof (Customer).GetCustomAttribute<ComputedAttribute>() != null &&
                                   okToUseDecompileIfNeeded;
                for (int i = 0; i < NumTimes; i++)
                {
                    var expression = db.Customers.Select(
                        c => c.Title + " " + c.FirstName + " " + c.LastName + " " + c.Suffix)
                        .Take(NumTakes);

                    var item1 = addDecompile ? expression.Decompile().ToList() : expression.ToList();
                }
            }
        }
        public void Test10TimingDifferenceOk()
        {
            const int NumTimes = 1000;
            const int NumTakes = 10;
            using (var db = new AdventureWorksLt2012())
            {
                //SETUP
                var firstId = db.Customers.Select(x => x.CustomerID).First();

                //ATTEMPT
                TimedDelegateDecompiler(NumTimes, db, NumTakes);
                TimedCheckIfItNeedsDecompile(NumTimes, db, NumTakes, false);
                TimedCheckIfItNeedsDecompile(NumTimes, db, NumTakes, true);
                TimedCheckIfItNeedsDecompile(NumTimes, db, NumTakes, false);
                TimedNormalAccess(NumTimes, db, NumTakes);
                TimedNormalAccessButCallDecompileAnyway(NumTimes, db, NumTakes);
                TimedDelegateDecompiler(NumTimes, db, NumTakes);
                TimedNormalAccess(NumTimes, db, NumTakes);
                TimedNormalAccessButCallDecompileAnyway(NumTimes, db, NumTakes);
                TimedDelegateDecompiler(NumTimes, db, NumTakes);

                //VERIFY

            }
        }