public void FindAllWithPredicateEagerLoadsCorrectly()
        {
            var person1 = CreatePerson();

            person1.EmailAddresses.Add("*****@*****.**", ContactType.Personal);
            person1.EmailAddresses.Add("*****@*****.**", ContactType.Business);
            person1.PhoneNumbers.Add("555-123", ContactType.Personal);
            person1.PhoneNumbers.Add("555-456", ContactType.Business);
            var person2 = CreatePerson();
            var person3 = CreatePerson();

            using (new EFUnitOfWorkFactory().Create())
            {
                var repository = new PeopleRepository();
                repository.Add(person1);
                repository.Add(person2);
                repository.Add(person3);
            }

            using (new EFUnitOfWorkFactory().Create(true))
            {
                var repository = new PeopleRepository();
                var oneAndTwo  = repository.FindAll(x => x.Id == person1.Id || x.Id == person2.Id, x => x.EmailAddresses).ToList();
                oneAndTwo.Count().Should().Be(2);
                oneAndTwo.First(x => x.Id == person1.Id).Should().NotBeNull();
                oneAndTwo.First(x => x.Id == person2.Id).Should().NotBeNull();
                oneAndTwo.FirstOrDefault(x => x.Id == person3.Id).Should().BeNull();
                oneAndTwo.First(x => x.Id == person1.Id).EmailAddresses.Count.Should().Be(2);
                oneAndTwo.First(x => x.Id == person1.Id).PhoneNumbers.Count.Should().Be(0);
            }
        }
コード例 #2
0
        public async Task Create()
        {
            //act
            await peopleRepository.Add(people);

            //assert
            Assert.True(people.ID > 0);
        }
コード例 #3
0
        public void FindByWithIncludeReturnsOnlyIncludedAndNotOtherProperties()
        {
            var phoneNumber1 = new PhoneNumber {
                ContactType = ContactType.Business, Number = "555-12345678"
            };
            var phoneNumber2 = new PhoneNumber {
                ContactType = ContactType.Business, Number = "555-12345678"
            };
            var    emailAddress1 = EmailAddressTests.CreateEmailAddress();
            var    emailAddress2 = EmailAddressTests.CreateEmailAddress();
            Person person        = CreatePerson();

            person.PhoneNumbers.Add(phoneNumber1);
            person.PhoneNumbers.Add(phoneNumber2);
            person.EmailAddresses.Add(emailAddress1);
            person.EmailAddresses.Add(emailAddress2);

            using (var uow = new EFUnitOfWorkFactory().Create())
            {
                var peopleRepository = new PeopleRepository();
                peopleRepository.Add(person);
            }

            using (var uow = new EFUnitOfWorkFactory().Create(true))
            {
                var peopleRepository = new PeopleRepository();
                var check            = peopleRepository.FindById(person.Id, x => x.PhoneNumbers);
                check.PhoneNumbers.Count.Should().Be(2);
                check.EmailAddresses.Count.Should().Be(0);
            }
        }
コード例 #4
0
        public Person Add(Person person)
        {
            var repo = new PeopleRepository(_connectionString);

            repo.Add(person);
            return(person);
        }
コード例 #5
0
        public ActionResult Add(Person person)
        {
            var repo = new PeopleRepository(Properties.Settings.Default.ConStr);

            repo.Add(person);
            return(RedirectToAction("Index"));
        }
コード例 #6
0
        public void AddressTypeRoundtipsToDatabase()
        {
            int    newPersonId = 0;
            var    address     = CreateAddress(ContactType.Personal);
            Person person      = SimplePeopleTests.CreatePerson();

            person.HomeAddress = address;
            address            = CreateAddress(ContactType.Business);
            person.WorkAddress = address;

            using (new EFUnitOfWorkFactory().Create())
            {
                PeopleRepository peopleRepository = new PeopleRepository();
                peopleRepository.Add(person);
            }
            newPersonId = person.Id;

            newPersonId.Should().BeGreaterThan(0);

            ContactManagerContext context = new ContactManagerContext();
            var check = context.People.First(x => x.Id == newPersonId);

            check.Id.Should().Be(newPersonId);
            check.HomeAddress.ContactType.Should().Be(ContactType.Personal);
            check.WorkAddress.ContactType.Should().Be(ContactType.Business);
        }
コード例 #7
0
        public ActionResult AddPerson(Person person)
        {
            PeopleRepository repository = new PeopleRepository(Properties.Settings.Default.ConStr);

            repository.Add(person);
            return(Redirect("/home/index"));
        }
コード例 #8
0
        public void EmailAddressTypeRoundtipsToDatabase()
        {
            int         newPersonId = 0;
            ContactType type        = ContactType.Personal;
            var         address     = CreateEmailAddress();

            address.ContactType = type;
            Person person = SimplePeopleTests.CreatePerson();

            person.EmailAddresses.Add(address);

            using (var uwo = new EFUnitOfWorkFactory().Create())
            {
                PeopleRepository peopleRepository = new PeopleRepository();
                peopleRepository.Add(person);
            }
            newPersonId = person.Id;

            newPersonId.Should().BeGreaterThan(0);

            ContactManagerContext context = new ContactManagerContext();
            var check = context.People.Include("EmailAddresses").First(x => x.Id == newPersonId);

            check.Id.Should().Be(newPersonId);
            check.EmailAddresses.First().ContactType.Should().Be(type);
        }
        public void ClearingPhoneNumbersCollectionDeletesPhoneNumbers()
        {
            string number1 = Guid.NewGuid().ToString().Substring(0, 25);
            string number2 = Guid.NewGuid().ToString().Substring(0, 25);
            string sql     = string.Format("SELECT * FROM PhoneNumbers WHERE Number = '{0}'", number1);

            var person = CreatePerson();

            person.PhoneNumbers.Add(number1, ContactType.Personal);
            person.PhoneNumbers.Add(number2, ContactType.Personal);

            using (new EFUnitOfWorkFactory().Create())
            {
                var repository = new PeopleRepository();
                repository.Add(person);
            }

            CheckIfExists(sql).Should().BeTrue();
            int personId = person.Id;

            personId.Should().BeGreaterThan(0);
            var checkRepository = new PeopleRepository();

            using (new EFUnitOfWorkFactory().Create(true))
            {
                var checkPerson = checkRepository.FindById(personId, x => x.PhoneNumbers);
                checkPerson.PhoneNumbers.Clear();
            }
            CheckIfExists(sql).Should().BeFalse();
        }
コード例 #10
0
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            nameTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            birthdayDatePicker.GetBindingExpression(DatePicker.SelectedDateProperty).UpdateSource();

            if (Validation.GetHasError(nameTextBox) || Validation.GetHasError(birthdayDatePicker))
            {
                MessageBox.Show("Please provide correct data");
            }
            else
            {
                if (IsEdited)
                {
                    CurrentPerson.Image    = TemporaryPerson.Image;
                    CurrentPerson.Name     = TemporaryPerson.Name;
                    CurrentPerson.Birthday = TemporaryPerson.Birthday;
                    PeopleRepository.Save();
                }
                else
                {
                    var defaultOccasions = new ObservableCollection <Occasion>()
                    {
                        OccasionsRepository.GetAll().SingleOrDefault(x => x.Name == "New Year"),
                        new Occasion {
                            Date  = TemporaryPerson.Birthday,
                            Image = ImageHelper.BitmapSourceToByteArray(@"..\..\Images\DefaultOccasionImages\gift.png"),
                            Name  = TemporaryPerson.Name + "'s Birthday",
                        }
                    };
                    TemporaryPerson.Occasions = defaultOccasions;
                    PeopleRepository.Add(TemporaryPerson);
                }
                this.Close();
            }
        }
コード例 #11
0
        public IActionResult Add(Person person)
        {
            var repo = new PeopleRepository(_connectionString);

            repo.Add(person);
            return(Redirect("/"));
        }
コード例 #12
0
 static void Main(string[] args)
 {
     if (args.Length == 1)
     {
         _fileName = args[0];
     }
     using (var engine = new FileHelperAsyncEngine <ImportPerson>())
     {
         engine.BeginReadFile(_fileName);
         int success   = 0;
         int failed    = 0;
         var stopwatch = new Stopwatch();
         stopwatch.Start();
         using (var uow = new EFUnitOfWorkFactory().Create())
         {
             foreach (ImportPerson importPerson in engine)
             {
                 try
                 {
                     var person = new Person();
                     Mapper.Map(importPerson, person);
                     TryAddEmailAddress(importPerson, person);
                     TryAddPhoneNumber(importPerson, person);
                     person.HomeAddress = FixAddress(person.HomeAddress, changeMissingDataToNull: false); // False sets missing data to <unknown>.
                     person.WorkAddress = FixAddress(person.WorkAddress, changeMissingDataToNull: false); // False sets missing data to <unknown>.
                     if (!person.Validate().Any())
                     {
                         success++;
                         PeopleRepository.Add(person);
                         if (success % 30 == 0)
                         {
                             uow.Commit(true);
                         }
                     }
                     else
                     {
                         //TODO Handle invalid data. Log it, or deal with it in some way.
                         failed++;
                     }
                 }
                 catch (Exception ex)
                 {
                     //TODO Handle error. Log it, or deal with it in some way.
                     Console.WriteLine("Failed to import row " + engine.LineNumber);
                     Console.WriteLine("Error " + ex.Message);
                     failed++;
                 }
                 long   elapsed    = stopwatch.ElapsedMilliseconds;
                 double timeForOne = (elapsed / (double)success) / 1000;
                 double perSecond  = 1 / timeForOne;
                 UpdateUI(success, failed, elapsed, perSecond, importPerson);
             }
         }
         stopwatch.Stop();
         engine.Close();
     }
 }
コード例 #13
0
 public void Verify_Add_Should_AddTheEntityToTheContext()
 {
     // Arrange
     Mock<IDbSet<Person>> mockSetPeople;
     var mockContext = PeopleMockingSetup.DoMockingSetupForContext(false, out mockSetPeople);
     var repository = new PeopleRepository(mockContext.Object);
     var people = new Person { Active = true, CustomKey = "SALVATORE-RAA", };
     // Act
     repository.Add(people);
     // Assert
     mockSetPeople.Verify(x => x.Add(people), Times.Once);
 }
コード例 #14
0
        public void AddPerson(AddPersonViewModel vm)
        {
            var person = new Person
            {
                FirstName = vm.FirstName,
                LastName  = vm.LastName,
                Age       = (DateTime.Now - vm.Birthday).Days / 365
            };
            var repo = new PeopleRepository(_connectionString);

            repo.Add(person);
        }
コード例 #15
0
        public void FindByLastNameReturnsCorrectPeople()
        {
            string lastName = Guid.NewGuid().ToString().Substring(0, 25);
            var    person1  = CreatePerson();
            var    person2  = CreatePerson();

            person1.LastName = lastName;
            person2.LastName = lastName;
            using (new EFUnitOfWorkFactory().Create())
            {
                var repository = new PeopleRepository();
                repository.Add(person1);
                repository.Add(person2);
            }

            using (new EFUnitOfWorkFactory().Create(true))
            {
                var repository         = new PeopleRepository();
                var peopleWithLastName = repository.FindByLastName(lastName);
                peopleWithLastName.Count().Should().Be(2);
            }
        }
コード例 #16
0
        public void ValidationErrorsThrowModelValidationException()
        {
            var    uow = new EFUnitOfWorkFactory().Create();
            Action act = () =>
            {
                var repo = new PeopleRepository();
                repo.Add(new Person());
                uow.Commit(true);
            };

            act.ShouldThrow <ModelValidationException>().WithMessage("The FirstName field is required", ComparisonMode.Substring);
            uow.Undo();
        }
コード例 #17
0
 public void Verify_SaveChanges_Should_RunOnceAndReturnSuccessfully()
 {
     // Arrange
     Mock<IDbSet<Person>> mockSetPeople;
     var mockContext = PeopleMockingSetup.DoMockingSetupForContext(false, out mockSetPeople);
     var repository = new PeopleRepository(mockContext.Object);
     var person = new Person { Active = true, CustomKey = "SALVATORE-RAA", Name = "R.A.A Salvatore" };
     repository.Add(person);
     // Act
     var result = repository.SaveChanges();
     // Assert
     mockContext.Verify(x => x.SaveChanges(), Times.Once);
     Assert.Equal(true, result);
 }
コード例 #18
0
        public void Verify_Add_Should_AddTheEntityToTheContext()
        {
            // Arrange
            Mock <IDbSet <Person> > mockSetPeople;
            var mockContext = PeopleMockingSetup.DoMockingSetupForContext(false, out mockSetPeople);
            var repository  = new PeopleRepository(mockContext.Object);
            var people      = new Person {
                Active = true, CustomKey = "SALVATORE-RAA",
            };

            // Act
            repository.Add(people);
            // Assert
            mockSetPeople.Verify(x => x.Add(people), Times.Once);
        }
コード例 #19
0
        public void FindAllWithPredicateSearchesCorrectly()
        {
            var person1 = CreatePerson();
            var person2 = CreatePerson();
            var person3 = CreatePerson();

            using (new EFUnitOfWorkFactory().Create())
            {
                var repository = new PeopleRepository();
                repository.Add(person1);
                repository.Add(person2);
                repository.Add(person3);
            }

            using (new EFUnitOfWorkFactory().Create())
            {
                var repository = new PeopleRepository();
                var oneAndTwo  = repository.FindAll(x => x.Id == person1.Id || x.Id == person2.Id).ToList();
                oneAndTwo.Count().Should().Be(2);
                oneAndTwo.First(x => x.Id == person1.Id).Should().NotBeNull();
                oneAndTwo.First(x => x.Id == person2.Id).Should().NotBeNull();
                oneAndTwo.FirstOrDefault(x => x.Id == person3.Id).Should().BeNull();
            }
        }
コード例 #20
0
        public void EnumOnPersonTypeRountripsToDatabase()
        {
            Person person = CreatePerson();

            person.Type = PersonType.Colleague;
            using (IUnitOfWork unitOfWork = new EFUnitOfWorkFactory().Create())
            {
                PeopleRepository repository = new PeopleRepository();
                repository.Add(person);
            }
            PeopleRepository repoConfirm = new PeopleRepository();
            Person           personCheck = repoConfirm.FindById(person.Id);

            Assert.AreEqual(PersonType.Colleague, personCheck.Type);
        }
コード例 #21
0
        public void FindByFindsPerson()
        {
            var person     = CreatePerson();
            var repository = new PeopleRepository();

            using (new EFUnitOfWorkFactory().Create())
            {
                repository.Add(person);
            }
            person.Id.Should().BePositive();

            var check = repository.FindById(person.Id);

            check.Id.Should().Be(person.Id);
        }
コード例 #22
0
        public void FindAllWithPredicateSearchesCorrectlyForLastNameAndType()
        {
            string lastName = Guid.NewGuid().ToString().Substring(0, 25);
            var    person1  = CreatePerson();
            var    person2  = CreatePerson();

            person1.LastName = lastName;
            person1.Type     = PersonType.Colleague;
            person2.LastName = lastName;
            person2.Type     = PersonType.Friend;
            using (new EFUnitOfWorkFactory().Create())
            {
                var repository = new PeopleRepository();
                repository.Add(person1);
                repository.Add(person2);
            }

            using (new EFUnitOfWorkFactory().Create())
            {
                var repository = new PeopleRepository();
                repository.FindAll(x => x.LastName == lastName).Count().Should().Be(2);
                repository.FindAll(x => x.LastName == lastName && x.Type == PersonType.Colleague).Count().Should().Be(1);
            }
        }
コード例 #23
0
        public void CanGetBasicPerson()
        {
            Person           person     = CreatePerson();
            PeopleRepository repository = new PeopleRepository();

            using (IUnitOfWork unitOfWork = new EFUnitOfWorkFactory().Create())
            {
                repository.Add(person);
            }
            PeopleRepository repositoryConfirm = new PeopleRepository();
            Person           personConfirm     = repositoryConfirm.FindById(person.Id);

            personConfirm.Id.Should().Be(person.Id);

            var friendsWithATattoo = repository.FindAll(
                x => x.FirstName == "Goo" && x.Type == PersonType.Friend);
        }
コード例 #24
0
        public void Verify_SaveChanges_Should_RunOnceAndReturnSuccessfully()
        {
            // Arrange
            Mock <IDbSet <Person> > mockSetPeople;
            var mockContext = PeopleMockingSetup.DoMockingSetupForContext(false, out mockSetPeople);
            var repository  = new PeopleRepository(mockContext.Object);
            var person      = new Person {
                Active = true, CustomKey = "SALVATORE-RAA", Name = "R.A.A Salvatore"
            };

            repository.Add(person);
            // Act
            var result = repository.SaveChanges();

            // Assert
            mockContext.Verify(x => x.SaveChanges(), Times.Once);
            Assert.Equal(true, result);
        }
コード例 #25
0
 public void CreatePerson(Person person)
 {
     var peopleRepository = new PeopleRepository(_db);
     peopleRepository.Add(person);
     _db.SaveChanges();
 }
コード例 #26
0
        public void AddPeople(People entity)
        {
            PeopleRepository peopleRepository = new PeopleRepository();

            peopleRepository.Add(entity);
        }
コード例 #27
0
 public void Create(People obj)
 {
     peopleRepository.Add(obj);
 }
コード例 #28
0
        public void AddPerson(People person)
        {
            var repo = new PeopleRepository(Properties.Settings.Default.ConStr);

            repo.Add(person);
        }