public void AddCustomer_WithExistingCustomerAddress()
        {
            var customerModel = GetBaseCustomer();

            Address address;
            using (var uow = _uow())
            {
                address = uow.Addresses.GetAll().FirstOrDefault();
            }

            var customerAddressModel = new CustomerAddressModel()
            {
                AddressID = address.AddressID,
                AddressType = "Type"
            };

            customerModel.CustomerAddresses.Add(customerAddressModel);

            using (new TransactionScope())
            {
                //Act
                var result = _service.AddCustomer(customerModel);

                //Assert
                Assert.IsTrue(result.Success);
                Assert.IsNotNull(result.Value);
                Assert.IsTrue(customerModel.CustomerID > 0);

                using (var uow = _uow())
                {
                    var dbCustomer = uow.Customers.GetAll().Include(c => c.CustomerAddresses.Select(ca => ca.Address)).SingleOrDefault(c => c.CustomerID == customerModel.CustomerID);
                    Assert.AreEqual(customerModel.FirstName, dbCustomer.FirstName);
                    Assert.AreEqual(customerModel.LastName, dbCustomer.LastName);

                    Assert.IsTrue(dbCustomer.CustomerAddresses.Count() == 1);
                    Assert.AreEqual(customerAddressModel.AddressType, dbCustomer.CustomerAddresses.First().AddressType);
                    Assert.IsNotNull(dbCustomer.CustomerAddresses.First().Address);
                    Assert.AreEqual(address.AddressLine1, dbCustomer.CustomerAddresses.First().Address.AddressLine1);
                    Assert.AreEqual(address.CountryRegion, dbCustomer.CustomerAddresses.First().Address.CountryRegion);

                }
            }
        }
        public void UpdateCustomer_AddNewAddress()
        {
            //Arrange
            Customer existingCustomer;
            Customer mappingCustomer;
            using (var uow = _uow())
            {
                existingCustomer = uow.Customers.GetAll().Include(c => c.CustomerAddresses.Select(ca => ca.Address)).FirstOrDefault(c => c.CustomerAddresses.Any());
                mappingCustomer = uow.Customers.GetAll().FirstOrDefault(c => c.CustomerID == existingCustomer.CustomerID);
            }

            var customerModel = BaseService.Mapper.Map<CustomerModel>(mappingCustomer); // no addresses on it.
            customerModel.FirstName = "Donkey Brothers";

            AddressModel addressModel = new AddressModel()
            {
                AddressLine1 = "Line 1",
                City = "Florence",
                CountryRegion = "Italy",
                PostalCode = "123",
                StateProvince = "Plumberton"
            };

            var customerAddressModel = new CustomerAddressModel()
            {
                Address = addressModel,
                AddressType = "Type"
            };

            customerModel.CustomerAddresses.Add(customerAddressModel);

            using (new TransactionScope())
            {
                //Act
                var result = _service.UpdateCustomer(customerModel);

                //Assert
                Assert.IsTrue(result.Success);

                using (var uow = _uow())
                {
                    var updatedCustomer = uow.Customers.GetAll().Include(c => c.CustomerAddresses.Select(ca => ca.Address)).SingleOrDefault(c => c.CustomerID == customerModel.CustomerID);
                    Assert.IsTrue(updatedCustomer.CustomerAddresses.Count() > existingCustomer.CustomerAddresses.Count());
                }
            }
        }
        public void UpdateCustomer_LinkToExistingAddress()
        {
            //Arrange
            Customer existingCustomer, mappingCustomer;
            using (var uow = _uow())
            {
                existingCustomer = uow.Customers.GetAll().Include(c => c.CustomerAddresses.Select(ca => ca.Address)).FirstOrDefault(c => c.CustomerAddresses.Any());
                mappingCustomer = uow.Customers.GetAll().FirstOrDefault(c => c.CustomerID == existingCustomer.CustomerID);
            }

            Address address;
            using (var uow = _uow())
            {
                address = uow.Addresses.GetAll().FirstOrDefault();
            }

            var customerAddressModel = new CustomerAddressModel()
            {
                AddressID = address.AddressID,
                AddressType = "Type"
            };

            var customerModel = BaseService.Mapper.Map<CustomerModel>(mappingCustomer);
            customerModel.FirstName = "Donkey Brothers";
            customerModel.CustomerAddresses.Add(customerAddressModel);

            using (new TransactionScope())
            {
                //Act
                var result = _service.UpdateCustomer(customerModel);

                //Assert
                Assert.IsTrue(result.Success);

                using (var uow = _uow())
                {
                    var updatedCustomer = uow.Customers.GetAll().Include(c => c.CustomerAddresses.Select(ca => ca.Address)).SingleOrDefault(c => c.CustomerID == customerModel.CustomerID);
                    Assert.IsTrue(updatedCustomer.CustomerAddresses.Count() > existingCustomer.CustomerAddresses.Count());
                    Assert.IsTrue(updatedCustomer.CustomerAddresses.Any(ca => ca.AddressID == address.AddressID));
                }
            }
        }
        public void AddCustomer_WithNewAddress()
        {
            var customerModel = GetBaseCustomer();

            AddressModel addressModel = new AddressModel()
            {
                AddressLine1 = "Line 1",
                City = "Florence",
                CountryRegion = "Italy",
                PostalCode = "123",
                StateProvince = "Plumberton"
            };

            var customerAddressModel = new CustomerAddressModel()
            {
                Address = addressModel,
                AddressType = "Type"
            };

            customerModel.CustomerAddresses.Add(customerAddressModel);

            using (new TransactionScope())
            {
                //Act
                var result = _service.AddCustomer(customerModel);

                //Assert
                Assert.IsTrue(result.Success);
                Assert.IsNotNull(result.Value);
                Assert.IsTrue(customerModel.CustomerID > 0);

                using (var uow = _uow())
                {
                    var dbCustomer = uow.Customers.GetAll().Include(c => c.CustomerAddresses.Select(ca => ca.Address)).SingleOrDefault(c => c.CustomerID == customerModel.CustomerID);
                    Assert.AreEqual(customerModel.FirstName, dbCustomer.FirstName);
                    Assert.AreEqual(customerModel.LastName, dbCustomer.LastName);
                    Assert.IsTrue(dbCustomer.CustomerAddresses.Count() == 1);
                    Assert.AreEqual(customerAddressModel.AddressType, dbCustomer.CustomerAddresses.First().AddressType);
                    Assert.IsNotNull(dbCustomer.CustomerAddresses.First().Address);
                    Assert.AreEqual(customerAddressModel.Address.AddressLine1, dbCustomer.CustomerAddresses.First().Address.AddressLine1);
                    Assert.AreEqual(customerAddressModel.Address.CountryRegion, dbCustomer.CustomerAddresses.First().Address.CountryRegion);
                    // etc...
                }
            }
        }