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 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);
            }
        }
Exemple #3
0
        public void TestMethod1()
        {
            var options = new DbContextOptionsBuilder <PeopleSearchDbContext>()
                          .UseInMemoryDatabase(databaseName: "PeopleListDatabase")
                          .Options;

            // Insert seed data into the database using one instance of the context
            using (var context = new PeopleSearchDbContext(options))
            {
                context.Peoples.Add(new People {
                    FirstName = "Jone", LastName = "Sams", Age = 22, Address = "TestAddress1", Interest = "TestInterest1"
                });
                context.Peoples.Add(new People {
                    FirstName = "Laura", LastName = "John", Age = 32, Address = "TestAddress2", Interest = "TestInterest2"
                });
                context.Peoples.Add(new People {
                    FirstName = "Sam", LastName = "Kite", Age = 23, Address = "TestAddress3", Interest = "TestInterest3"
                });
                context.SaveChanges();
            }

            using (var context = new PeopleSearchDbContext(options))
            {
                PeopleService peopleService = new PeopleService(context);
                var           peopleList    = peopleService.GetAsync().Result;
                Assert.AreEqual(3, peopleList.Count);

                peopleList = peopleService.GetbyNameAsync("Jo").Result;
                Assert.AreEqual(2, peopleList.Count);
            }
        }
        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_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);
            }
        }
        public async Task AddOrUpdatePerson_GivenPerson_ShouldReturnPersonCorrectly()
        {
            // Arrange
            using (PeopleSearchDbContext testContext = GetTestContext())
            {
                Person expected = TestData.TestPerson1();

                PersonRepository repository = new PersonRepository(testContext);

                // Act
                Person actual = await repository.AddOrUpdatePerson(expected);

                // Assert
                ModelComparisonHelper.AssertPeopleAreEqual(expected, actual);
            }
        }
        public async Task GetByIdAsync_GivenNoPeopleExist_ShouldReturnNull()
        {
            // Arrange
            using (PeopleSearchDbContext testContext = GetTestContext())
            {
                PersonRepository repository = new PersonRepository(testContext);

                Person expected = null;

                // Act
                Person actual = await repository.GetByIdAsync(0);

                // Assert
                ModelComparisonHelper.AssertPeopleAreEqual(expected, actual);
            }
        }
        public async Task AddOrUpdatePerson_GivenNullPerson_ShouldNotAdd()
        {
            // Arrange
            using (PeopleSearchDbContext testContext = GetTestContext())
            {
                PersonRepository repository = new PersonRepository(testContext);

                int expected = 0;

                // Act
                await repository.AddOrUpdatePerson(null);

                // Assert
                int actual = await testContext.People.CountAsync();

                Assert.AreEqual(expected, actual);
            }
        }
        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 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);
            }
        }
Exemple #15
0
 public PeopleService(PeopleSearchDbContext context)
 {
     Context = context;
 }
Exemple #16
0
 public PeopleController(ILogger <PeopleController> logger, PeopleSearchDbContext context, IPeopleService peopleService)
 {
     _logger       = logger;
     Context       = context;
     PeopleService = peopleService;
 }
 public PeopleController(PeopleSearchDbContext context, IMapper mapper)
 {
     this.mapper  = mapper;
     this.context = context;
 }
Exemple #18
0
 public SqlPersonData(PeopleSearchDbContext context)
 {
     _context = context;
 }
Exemple #19
0
 /// <summary>
 /// Constructor which expects a PeopleSearchDbContext to use as the repository.
 /// </summary>
 /// <param name="context">The context to use as the repository.</param>
 public PersonRepository(PeopleSearchDbContext context)
 {
     _context = context;
 }