Exemple #1
0
        public void AddAddressId(long aid)
        {
            if (CurrAddresses.Any(addr => addr.AddressID == aid))
            {
                throw new InvalidOperationException("Address ID already exists!");
            }

            using (var db = new MailerEntities())
            {
                db.MailingLists.Attach(MailingList);
                var newRm = new MailingListLine
                {
                    AddressID = aid
                };
                MailingList.MailingListLines.Add(newRm);
                var address = db.Addresses.Find(aid);
                CurrAddresses.Add(address);
                AvailAddresses.Remove(AvailAddresses.Single(addr => addr.AddressID == aid));
                db.SaveChangesAsync();
            }
        }
        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();
            }
        }