public async Task DeletePersonAsync_GivenPersonDoesNotExist_ShouldDoNothing()
        {
            // Arrange
            using (PeopleSearchDbContext testContext = GetTestContext())
            {
                Person        person1  = TestData.TestPerson1();
                Person        person2  = TestData.TestPerson2();
                List <Person> expected = new List <Person> {
                    person1
                };

                testContext.People.Add(person1);
                await testContext.SaveChangesAsync();

                PersonRepository repository = new PersonRepository(testContext);

                // Act
                await repository.DeletePersonAsync(person2.Id);

                // Assert
                List <Person> actual = await testContext.People.ToListAsync();

                ModelComparisonHelper.AssertPersonListsAreEqual(expected, actual);
            }
        }
Beispiel #2
0
        public async Task GetAsync_GivenMatchingSearchString_ShouldReturnPeopleCorrectly()
        {
            // Arrange
            Person        person1  = TestData.TestPerson1();
            Person        person2  = TestData.TestPerson2();
            List <Person> matching = new List <Person> {
                person1, person2
            };
            List <Person> expected = new List <Person> {
                person1, person2
            };

            Mock <IPersonRepository> mockPersonRepository = new Mock <IPersonRepository>(MockBehavior.Loose);

            mockPersonRepository.Setup(r => r.SearchPeopleAsync(It.IsAny <string>())).ReturnsAsync(matching);

            Mock <ILatencySimulator> mockLatencySimulator = new Mock <ILatencySimulator>(MockBehavior.Loose);

            PeopleController controller = new PeopleController(mockPersonRepository.Object);

            // Act
            List <Person> actual = (await controller.GetAsync("someString")).Value.ToList();

            // Assert
            ModelComparisonHelper.AssertPersonListsAreEqual(expected, actual);
        }
        public async Task AddOrUpdatePerson_GivenInterestsRemoved_ShouldUpdate()
        {
            // Arrange
            using (PeopleSearchDbContext testContext = GetTestContext())
            {
                Person        added    = TestData.TestPerson1();
                Person        modified = TestData.TestPerson1();
                List <Person> expected = new List <Person> {
                    modified
                };

                testContext.People.Add(added);
                await testContext.SaveChangesAsync();

                modified.Id = added.Id;
                added.Interests.Clear();
                modified.Interests.Clear();

                PersonRepository repository = new PersonRepository(testContext);

                // Act
                await repository.AddOrUpdatePerson(added);

                // Assert
                List <Person> actual = await testContext.People.ToListAsync();

                ModelComparisonHelper.AssertPersonListsAreEqual(expected, actual);
            }
        }
        public async Task SearchPeopleAsync_GivenSearchStringMatchesMultiple_ShouldReturnPeopleCorrectly()
        {
            // Arrange
            using (PeopleSearchDbContext testContext = GetTestContext())
            {
                Person        added1   = TestData.TestPerson1();
                Person        added2   = TestData.TestPerson2();
                List <Person> expected = new List <Person> {
                    added1, added2
                };

                testContext.People.Add(added1);
                testContext.People.Add(added2);
                await testContext.SaveChangesAsync();

                PersonRepository repository = new PersonRepository(testContext);

                // Act
                List <Person> actual = (await repository.SearchPeopleAsync("e")).ToList();

                // Assert
                List <Person> actualSorted = actual.OrderBy(p => p.Id).ToList();
                ModelComparisonHelper.AssertPersonListsAreEqual(expected, actualSorted);
            }
        }
        public async Task SearchPeopleAsync_GivenNoPeopleExist_ShouldReturnEmptyList()
        {
            // Arrange
            using (PeopleSearchDbContext testContext = GetTestContext())
            {
                PersonRepository repository = new PersonRepository(testContext);

                List <Person> expected = new List <Person>();

                // Act
                List <Person> actual = (await repository.SearchPeopleAsync(null)).ToList();

                // Assert
                ModelComparisonHelper.AssertPersonListsAreEqual(expected, actual);
            }
        }
Beispiel #6
0
        public async Task GetAsync_GivenNoPeopleExist_ShouldReturnEmptyList()
        {
            // Arrange
            List <Person> matching = new List <Person>();
            List <Person> expected = new List <Person>();

            Mock <IPersonRepository> mockPersonRepository = new Mock <IPersonRepository>(MockBehavior.Loose);

            mockPersonRepository.Setup(r => r.SearchPeopleAsync(It.IsAny <string>())).ReturnsAsync(matching);

            PeopleController controller = new PeopleController(mockPersonRepository.Object);

            // Act
            List <Person> actual = (await controller.GetAsync(null)).Value.ToList();

            // Assert
            ModelComparisonHelper.AssertPersonListsAreEqual(expected, actual);
        }
        public async Task SearchPeopleAsync_GivenSearchStringMatchesNone_ShouldReturnEmptyList()
        {
            // Arrange
            using (PeopleSearchDbContext testContext = GetTestContext())
            {
                List <Person> expected = new List <Person>();

                testContext.People.Add(TestData.TestPerson1());
                testContext.People.Add(TestData.TestPerson2());
                await testContext.SaveChangesAsync();

                PersonRepository repository = new PersonRepository(testContext);

                // Act
                List <Person> actual = (await repository.SearchPeopleAsync("zzz")).ToList();

                // Assert
                ModelComparisonHelper.AssertPersonListsAreEqual(expected, actual);
            }
        }
        public async Task AddOrUpdatePerson_GivenPersonDoesNotExist_ShouldAdd()
        {
            // Arrange
            using (PeopleSearchDbContext testContext = GetTestContext())
            {
                Person        added    = TestData.TestPerson1();
                List <Person> expected = new List <Person> {
                    added
                };

                PersonRepository repository = new PersonRepository(testContext);

                // Act
                await repository.AddOrUpdatePerson(added);

                // Assert
                List <Person> actual = await testContext.People.ToListAsync();

                ModelComparisonHelper.AssertPersonListsAreEqual(expected, actual);
            }
        }
        public async Task SearchPeopleAsync_GivenPersonExists_ShouldReturnPersonCorrectly()
        {
            // Arrange
            using (PeopleSearchDbContext testContext = GetTestContext())
            {
                Person        added    = TestData.TestPerson1();
                List <Person> expected = new List <Person> {
                    added
                };

                testContext.People.Add(added);
                await testContext.SaveChangesAsync();

                PersonRepository repository = new PersonRepository(testContext);

                // Act
                List <Person> actual = (await repository.SearchPeopleAsync(null)).ToList();

                // Assert
                ModelComparisonHelper.AssertPersonListsAreEqual(expected, actual);
            }
        }
        public async Task DeletePersonAsync_GivenPersonExists_ShouldDoNothing()
        {
            // Arrange
            using (PeopleSearchDbContext testContext = GetTestContext())
            {
                Person        added    = TestData.TestPerson1();
                List <Person> expected = new List <Person>();

                testContext.People.Add(added);
                await testContext.SaveChangesAsync();

                testContext.Entry(added).State = EntityState.Detached;

                PersonRepository repository = new PersonRepository(testContext);

                // Act
                await repository.DeletePersonAsync(added.Id);

                // Assert
                List <Person> actual = await testContext.People.ToListAsync();

                ModelComparisonHelper.AssertPersonListsAreEqual(expected, actual);
            }
        }