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 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); } }
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); } }
/// <summary> /// Adds the provided Person to the repository. /// </summary> /// <param name="person">The Person to add to the repository.</param> /// <returns>The Person that was added or null.</returns> public async Task <Person> AddPersonAsync(Person person) { if (person != null && person.Id == 0) { _context.People.Add(person); await _context.SaveChangesAsync(); } return(person); }
public async Task GetByIdAsync_GivenNonMatchingId_ShouldReturnNull() { // Arrange using (PeopleSearchDbContext testContext = GetTestContext()) { Person expected = null; testContext.People.Add(TestData.TestPerson1()); testContext.People.Add(TestData.TestPerson2()); await testContext.SaveChangesAsync(); PersonRepository repository = new PersonRepository(testContext); // Act Person actual = await repository.GetByIdAsync(123); // Assert ModelComparisonHelper.AssertPeopleAreEqual(expected, actual); } }
public async Task GetByIdAsync_GivenPersonExists_ShouldReturnPersonCorrectly() { // Arrange using (PeopleSearchDbContext testContext = GetTestContext()) { Person added = TestData.TestPerson1(); Person expected = added; testContext.People.Add(added); await testContext.SaveChangesAsync(); PersonRepository repository = new PersonRepository(testContext); // Act Person actual = await repository.GetByIdAsync(expected.Id); // Assert ModelComparisonHelper.AssertPeopleAreEqual(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 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); } }