public void TestFilter()
        {
            var alvm = new AddressListViewModel();

            var alivm1 = new AddressListItemViewModel(new Address
            {
                FirstName = "Bob",
                LastName  = "Newbie",
                Email     = "*****@*****.**"
            });
            var alivm2 = new AddressListItemViewModel(new Address
            {
                FirstName = "Steve",
                LastName  = "steverson",
                Email     = "*****@*****.**"
            });

            alvm.AddAddressListItemViewModel(alivm1);
            alvm.AddAddressListItemViewModel(alivm2);

            alvm.Filter = "newb";
            Assert.AreSame(alivm1, alvm.AddressListItemViewModels.Single());

            alvm.Filter = "";
            Assert.AreEqual(2, alvm.AddressListItemViewModels.Count);

            alvm.Filter = "ERSON";
            Assert.AreSame(alivm2, alvm.AddressListItemViewModels.Single());

            alvm.Filter = "foo bar";
            Assert.IsFalse(alvm.AddressListItemViewModels.Any());
        }
        public void TestSort()
        {
            var alvm = new AddressListViewModel();


            var alivm1 = new AddressListItemViewModel(new Address
            {
                FirstName = "Bob",
                // lowercase to make sure case is ignored - steverson would go before newbie if it wasn't
                LastName = "newbie",
                Email    = "*****@*****.**"
            });
            var alivm2 = new AddressListItemViewModel(new Address
            {
                FirstName = "Steve",
                // lowercase to make sure case is ignored - steverson would go after newbie if it wasn't
                LastName = "Steverson",
                Email    = "*****@*****.**"
            });

            // Insert them in backwards sorted order (S before N)
            alvm.AddAddressListItemViewModel(alivm2);
            alvm.AddAddressListItemViewModel(alivm1);

            // Check that they were automatically sorted - Newbie comes before Steverson
            Assert.AreEqual(alivm1, alvm.AddressListItemViewModels[0]);
            Assert.AreEqual(alivm2, alvm.AddressListItemViewModels[1]);
        }
        public void TestExternalDeletion()
        {
            var alvm = new AddressListViewModel();

            var address = new Address
            {
                FirstName = "Bob",
                LastName  = "Newbie",
                Email     = "*****@*****.**"
            };

            using (var db = new MailerEntities())
            {
                db.Addresses.Add(address);
                db.SaveChanges();
            }

            var alivm1 = new AddressListItemViewModel(address);

            alvm.AddAddressListItemViewModel(alivm1);
            Assert.AreEqual(1, alvm.AddressListItemViewModels.Count);

            alivm1.Delete();
            Assert.AreEqual(0, alvm.AddressListItemViewModels.Count);

            using (var db = new MailerEntities())
            {
                // ensure that it was removed
                Assert.IsFalse(db.Addresses.Any(addr => addr.AddressID == address.AddressID));
            }
        }
        public void PropertiesTest()
        {
            var addr = new Address
            {
                FirstName = "Bob",
                LastName  = "Newbie",
                Email     = "*****@*****.**",
            };

            var alivm = new AddressListItemViewModel(addr);


            Assert.AreEqual("Bob", alivm.FirstName);
            Assert.AreEqual("Newbie", alivm.LastName);
            Assert.AreEqual("*****@*****.**", alivm.Email);
            Assert.AreSame(addr, alivm.Address);
            Assert.AreSame(addr, alivm.Recipient);
        }
        /// <summary>
        /// Get Default Address for given customer
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public virtual async Task <AddressListItemViewModel> GetDefaultAddressViewModelAsync(GetAddressesForCustomerParam param)
        {
            var addresses = await CustomerAddressRepository.GetCustomerAddressesAsync(
                param.CustomerId, param.Scope).ConfigureAwait(false);

            if (addresses == null)
            {
                return(null);
            }

            var addressVm      = new AddressListItemViewModel();
            var defaultAddress = addresses.Find(a => a.IsPreferredShipping);

            if (defaultAddress != null)
            {
                addressVm = ViewModelMapper.MapTo <AddressListItemViewModel>(defaultAddress, param.CultureInfo);
            }

            return(addressVm);
        }
        public void TestRemoval()
        {
            var alvm = new AddressListViewModel();

            var address = new Address
            {
                FirstName = "Bob",
                LastName  = "Newbie",
                Email     = "*****@*****.**"
            };


            var alivm1 = new AddressListItemViewModel(address);

            alvm.AddAddressListItemViewModel(alivm1);
            Assert.AreEqual(1, alvm.AddressListItemViewModels.Count);

            alvm.RemoveAddressListItemViewModel(alivm1);
            Assert.AreEqual(0, alvm.AddressListItemViewModels.Count);
        }
        public void DeleteTest()
        {
            var ml = new MailingList
            {
                Name = "TestMailingList",
            };

            var newAddress = new Address
            {
                FirstName     = "Bob",
                LastName      = "Newbie",
                Email         = "*****@*****.**",
                ReceivedMails = new Collection <ReceivedMail>
                {
                    new ReceivedMail
                    {
                        Year = 2013
                    },
                    new ReceivedMail
                    {
                        Year = 2014
                    },
                }
            };

            var mll = new MailingListLine
            {
                Address     = newAddress,
                MailingList = ml
            };


            using (var db = new MailerEntities())
            {
                db.Addresses.Add(newAddress);
                db.MailingLists.Add(ml);
                db.MailingListLines.Add(mll);
                db.SaveChanges();

                // check that the two receievedmails were added
                Assert.AreEqual(2, db.ReceivedMails.Count(rm => rm.AddressID == newAddress.AddressID));
            }

            var alivm = new AddressListItemViewModel(newAddress);

            var pumpFired = 0;

            MessagePump.OnMessage += (sender, msg) =>
            {
                if (msg == "AddressDeleted" && sender == alivm)
                {
                    pumpFired++;
                }
            };


            alivm.Delete();

            Assert.AreEqual(1, pumpFired);

            using (var db = new MailerEntities())
            {
                // ensure that the database is empty once again
                Assert.IsFalse(db.Addresses.Any(addr => addr.AddressID == newAddress.AddressID));
                Assert.IsFalse(db.MailingListLines.Any(line => line.AddressID == newAddress.AddressID));
                Assert.IsFalse(db.ReceivedMails.Any(rm => rm.AddressID == newAddress.AddressID));

                // delete the mailing list
                db.MailingLists.Attach(ml);
                db.MailingLists.Remove(ml);
                db.SaveChanges();
            }
        }