public async Task TestCreate_PersonDoesNotExist()
        {
            using (ShimsContext.Create())
            {
                System.Data.Entity.Fakes.ShimDbContext.AllInstances.SetOf1 <Person>((c) =>
                {
                    return(context.People);
                });
                var personId = 1;
                EmailAddressType emailAddressType = new EmailAddressType
                {
                    EmailAddressTypeId   = EmailAddressType.Home.Id,
                    EmailAddressTypeName = EmailAddressType.Home.Value
                };
                context.SetupActions.Add(() =>
                {
                    context.EmailAddressTypes.Add(emailAddressType);
                });
                var user  = new User(1);
                var email = "*****@*****.**";
                var emailAddressTypeId = emailAddressType.EmailAddressTypeId;
                var isPrimary          = true;
                var newEmail           = new NewPersonEmailAddress(user, emailAddressTypeId, email, isPrimary, personId);

                context.Revert();
                var         message = String.Format("The email addressable entity with id [{0}] was not found.", personId);
                Action      a       = () => service.Create(newEmail);
                Func <Task> f       = () => service.CreateAsync(newEmail);
                a.ShouldThrow <ModelNotFoundException>().WithMessage(message);
                f.ShouldThrow <ModelNotFoundException>().WithMessage(message);
            }
        }
Beispiel #2
0
        public void TestConstructor()
        {
            var emailTypeId = EmailAddressType.Home.Id;
            var email       = "*****@*****.**";
            var isPrimary   = true;
            var user        = new User(1);
            var personId    = 10;
            var model       = new NewPersonEmailAddress(user, emailTypeId, email, isPrimary, personId);

            Assert.AreEqual(personId, model.PersonId);
            Assert.AreEqual(personId, model.GetEmailAddressableEntityId());
        }
        public async Task TestCreate_CheckProperties()
        {
            using (ShimsContext.Create())
            {
                System.Data.Entity.Fakes.ShimDbContext.AllInstances.SetOf1 <Person>((c) =>
                {
                    return(context.People);
                });
                var              personId         = 1;
                Person           person           = null;
                EmailAddressType emailAddressType = new EmailAddressType
                {
                    EmailAddressTypeId   = EmailAddressType.Home.Id,
                    EmailAddressTypeName = EmailAddressType.Home.Value
                };
                context.SetupActions.Add(() =>
                {
                    person = new Person
                    {
                        PersonId = personId,
                        FullName = "full name"
                    };
                    context.EmailAddressTypes.Add(emailAddressType);
                    context.People.Add(person);
                });
                var    user  = new User(1);
                var    email = "*****@*****.**";
                var    emailAddressTypeId = emailAddressType.EmailAddressTypeId;
                var    isPrimary          = true;
                var    newEmail           = new NewPersonEmailAddress(user, emailAddressTypeId, email, isPrimary, personId);
                Action beforeTester       = () =>
                {
                    Assert.AreEqual(1, context.People.Count());
                    Assert.AreEqual(0, context.EmailAddresses.Count());
                };
                Action afterTester = () =>
                {
                    Assert.AreEqual(1, context.People.Count());
                    var firstPerson = context.People.First();
                    Assert.AreEqual(1, firstPerson.EmailAddresses.Count);
                    var firstEmail = firstPerson.EmailAddresses.First();
                    Assert.AreEqual(email, firstEmail.Address);
                    Assert.AreEqual(emailAddressTypeId, firstEmail.EmailAddressTypeId);
                    Assert.AreEqual(isPrimary, firstEmail.IsPrimary);

                    Assert.AreEqual(user.Id, firstEmail.History.CreatedBy);
                    Assert.AreEqual(user.Id, firstEmail.History.RevisedBy);
                    DateTimeOffset.Now.Should().BeCloseTo(firstEmail.History.CreatedOn, 20000);
                    DateTimeOffset.Now.Should().BeCloseTo(firstEmail.History.RevisedOn, 20000);
                };
                context.Revert();
                beforeTester();
                service.Create(newEmail);
                afterTester();

                context.Revert();
                beforeTester();
                await service.CreateAsync(newEmail);

                afterTester();
            }
        }
        public async Task TestCreate_OtherEmailAddressesPrimary()
        {
            using (ShimsContext.Create())
            {
                System.Data.Entity.Fakes.ShimDbContext.AllInstances.SetOf1 <Person>((c) =>
                {
                    return(context.People);
                });
                var              personId         = 1;
                Person           person           = null;
                EmailAddressType emailAddressType = new EmailAddressType
                {
                    EmailAddressTypeId   = EmailAddressType.Home.Id,
                    EmailAddressTypeName = EmailAddressType.Home.Value
                };
                EmailAddress primaryEmail1 = null;
                EmailAddress primaryEmail2 = null;
                context.SetupActions.Add(() =>
                {
                    person = new Person
                    {
                        PersonId = personId,
                        FullName = "full name"
                    };
                    primaryEmail1 = new EmailAddress
                    {
                        IsPrimary      = true,
                        EmailAddressId = 10,
                        PersonId       = personId,
                        Person         = person
                    };
                    primaryEmail2 = new EmailAddress
                    {
                        IsPrimary      = true,
                        EmailAddressId = 20,
                        PersonId       = personId,
                        Person         = person
                    };
                    person.EmailAddresses.Add(primaryEmail1);
                    person.EmailAddresses.Add(primaryEmail2);
                    context.EmailAddressTypes.Add(emailAddressType);
                    context.People.Add(person);
                    context.EmailAddresses.Add(primaryEmail1);
                    context.EmailAddresses.Add(primaryEmail2);
                });
                var    user  = new User(1);
                var    email = "*****@*****.**";
                var    emailAddressTypeId = emailAddressType.EmailAddressTypeId;
                var    isPrimary          = true;
                var    newEmail           = new NewPersonEmailAddress(user, emailAddressTypeId, email, isPrimary, personId);
                Action beforeTester       = () =>
                {
                    Assert.AreEqual(1, context.People.Count());
                    Assert.AreEqual(2, context.EmailAddresses.Count());
                    Assert.IsTrue(primaryEmail1.IsPrimary.Value);
                    Assert.IsTrue(primaryEmail2.IsPrimary.Value);
                };
                Action afterTester = () =>
                {
                    Assert.AreEqual(3, person.EmailAddresses.Count());
                    Assert.AreEqual(2, context.EmailAddresses.Count());
                    //the new email address is added to the person, not the context
                    Assert.IsTrue(person.EmailAddresses.Last().IsPrimary.Value);
                    Assert.IsFalse(primaryEmail1.IsPrimary.Value);
                    Assert.IsFalse(primaryEmail2.IsPrimary.Value);
                };
                context.Revert();
                beforeTester();
                service.Create(newEmail);
                afterTester();

                context.Revert();
                beforeTester();
                await service.CreateAsync(newEmail);

                afterTester();
            }
        }