public void GetQuantityPeopleWithSameName() { var person = new Person { Name = "Nome" }; var person2 = new Person { Name = "Nome" }; var person3 = new Person { Name = "Nome" }; var person4 = new Person { Name = "Nome2" }; var busPublisher = new Mock <IBusEventPublisher>(); var personRepository = new Mock <IPersonRepository>(); personRepository.Setup(repo => repo.GetAllPersonByRegion(It.IsAny <string>())).ReturnsAsync(new List <Person> { person, person2, person3, person4 }); PercentagePeopleSameNameByRegionQuery query = new PercentagePeopleSameNameByRegionQuery(); PercentagePeopleSameNameByRegionQueryHandler handler = new PercentagePeopleSameNameByRegionQueryHandler(personRepository.Object); IEnumerable <PercentagePeopleSameNameByRegionQueryDto> result = handler.Handle(query, new System.Threading.CancellationToken()).Result; Assert.NotNull(result); Assert.True(result.Any()); Assert.Equal("Nome", result.FirstOrDefault().Name); Assert.True(result.FirstOrDefault().Percentage == 75); personRepository.Verify(m => m.GetAllPersonByRegion(It.IsAny <string>()), Times.Once); }
public void GetQuantityPeopleWithSameName_RepositoryReturnsNone() { var busPublisher = new Mock <IBusEventPublisher>(); var personRepository = new Mock <IPersonRepository>(); personRepository.Setup(repo => repo.GetAllPersonByRegion(It.IsAny <string>())).ReturnsAsync(new List <Person> { }); PercentagePeopleSameNameByRegionQuery query = new PercentagePeopleSameNameByRegionQuery(); PercentagePeopleSameNameByRegionQueryHandler handler = new PercentagePeopleSameNameByRegionQueryHandler(personRepository.Object); IEnumerable <PercentagePeopleSameNameByRegionQueryDto> result = handler.Handle(query, new System.Threading.CancellationToken()).Result; Assert.NotNull(result); Assert.True(!result.Any()); personRepository.Verify(m => m.GetAllPersonByRegion(It.IsAny <string>()), Times.Once); }
public async Task <IEnumerable <PercentagePeopleSameNameByRegionQueryDto> > Handle(PercentagePeopleSameNameByRegionQuery request, CancellationToken cancellationToken) { List <PercentagePeopleSameNameByRegionQueryDto> resultado = new List <PercentagePeopleSameNameByRegionQueryDto>(); IEnumerable <Person> people = await _personRepository.GetAllPersonByRegion(request.Region); if (people.Any()) { double total = people.Count(); resultado.AddRange(people.GroupBy(p => p.Name).Select(p1 => new PercentagePeopleSameNameByRegionQueryDto { Name = p1.First().Name, Percentage = p1.Count() > 1 ? (p1.Count() / total) * 100 : 0 } ).Where(p => p.Percentage > 0).ToList()); } return(resultado); }