public async Task ShouldReturnData()
        {
            // Arrange
            var city     = "barcelona";
            var page     = 1;
            var size     = 1;
            var expected = size;

            // Act
            var actual = await sut.GetByCityAsync(city, page, size);

            // Assert
            Assert.IsNotNull(actual);
        }
示例#2
0
        public void ShouldReturnContributorsWhenTakeIs50()
        {
            // Arrange
            var filter = ContributorFilterDtoFactory.CreateWithTake(50);

            var expected = ContributorFactory.CreateList(50);

            contributorRepository.GetByCityAsync(filter.CityName, Arg.Any <int>(), Arg.Any <int>()).Returns(expected);
            contributorFilterValidator.Validate(filter).Returns(true);

            // Act
            var actual = sut.GetContributorsByCityAsync(filter);

            // Assert
            Assert.IsNotNull(actual);
        }
        public async Task <IEnumerable <Contributor> > GetContributorsByCityAsync(ContributorFilterDto filter)
        {
            Validate(filter);

            // I tried some values to check performance and I saw that no matter the size of the page I asked for
            // the response took the same time so I made the decission of keeping that simple and make a
            // generic approach that works the same for all the requests
            var tasks = new List <Task <IEnumerable <Contributor> > >();
            var page  = 1;

            for (var count = 0; count < filter.Take; count += MaxPage)
            {
                tasks.Add(contributorRepository.GetByCityAsync(filter.CityName, page, MaxPage));
                page++;
            }

            var contributorPages = await Task.WhenAll(tasks.ToArray()).ConfigureAwait(false);

            var result = contributorPages.SelectMany(x => x).ToList().Take(filter.Take);

            return(result);
        }