public async void YelpService_GetAvailableRestaurantOptionsAsync_ReturnsTotalNumberOfAvailableResults() { // arrange int total = 0; RestaurantCacheMock mockCache = new RestaurantCacheMock(); YelpService target = new YelpService(_apiKey, mockCache); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _apiKey); HttpResponseMessage message = await client.GetAsync("https://api.yelp.com/v3/businesses/search?categories=restaurants,!hotdogs&location=38655&radius=20000&limit=50&sort_by=best_match"); dynamic dto = JsonConvert.DeserializeObject <dynamic>(await message.Content.ReadAsStringAsync().ConfigureAwait(false)); total = dto.total; } // act DateTime startTime = DateTime.UtcNow; IEnumerable <RestaurantDto> result = await target.GetAvailableRestaurantOptionsAsync(Guid.Empty, new SearchOptions { Meal = MealTime.all }); DateTime endTime = DateTime.UtcNow; TimeSpan firstWatch = endTime - startTime; startTime = DateTime.UtcNow; await target.GetAvailableRestaurantOptionsAsync(Guid.Empty, new SearchOptions { Meal = MealTime.all }); endTime = DateTime.UtcNow; TimeSpan secondWatch = endTime - startTime; // assert Assert.Equal(total, result.Count()); Assert.True(firstWatch.TotalMilliseconds > 1000); Assert.True(secondWatch.TotalMilliseconds < 200); }
public async void YelpService_GetAvailableRestaurantOptionsAsync_ReturnsDifferentRestaurantsWhenGivenDifferentLocations() { // arrange RestaurantCacheMock mockCache = new RestaurantCacheMock(); YelpService target = new YelpService(_apiKey, new Mock <IRestaurantCache>().Object); // act IEnumerable <RestaurantDto> result = await target.GetAvailableRestaurantOptionsAsync(Guid.Empty, new SearchOptions { Zip = "38655" }); IEnumerable <RestaurantDto> result2 = await target.GetAvailableRestaurantOptionsAsync(Guid.Empty, new SearchOptions { Zip = "38834" }); // assert Assert.NotEqual(result, result2); }
public async void YelpService_GetAvailableRestaurantOptionsAsync_ReturnsFewerResultsWhenSpecifyingMeal() { // arrange RestaurantCacheMock mockCache = new RestaurantCacheMock(); YelpService target = new YelpService(_apiKey, new Mock <IRestaurantCache>().Object); // act IEnumerable <RestaurantDto> result = await target.GetAvailableRestaurantOptionsAsync(Guid.Empty, new SearchOptions { Meal = MealTime.all }); IEnumerable <RestaurantDto> result2 = await target.GetAvailableRestaurantOptionsAsync(Guid.Empty, new SearchOptions { Meal = MealTime.breakfast }); // assert Assert.True(result.Count() > result2.Count()); }
public async void LunchService_GetRestaurantAsync_FiltersOutNopes() { // arrange Mock <IGetLunchOptions> mockOptions = new Mock <IGetLunchOptions>(); Mock <IRepository> mockRepo = new Mock <IRepository>(); Mock <IChaos> mockRandom = new Mock <IChaos>(); IRestaurantCache cache = new RestaurantCacheMock(); mockRandom.Setup(m => m.Next(It.IsAny <int>())).Returns(0); Guid sessionGuid = Guid.NewGuid(); mockOptions.Setup(x => x.GetAvailableRestaurantOptionsAsync(sessionGuid, It.IsAny <SearchOptions>())).ReturnsAsync(new List <RestaurantDto> { new RestaurantDto { Name = "COok OOout", Id = "rest3" }, new RestaurantDto { Name = "Jimmy Pesto's Pizzaria", Id = "rest2" }, new RestaurantDto { Name = "Bob's Burgers", Id = "rest1" }, }); mockRepo.Setup(x => x.GetNopesAsync(It.IsAny <IEnumerable <int> >())).ReturnsAsync(new List <string> { "rest2", "rest3" }); List <int> users = new List <int> { 1, 2 }; LunchService target = new LunchService(mockOptions.Object, mockRepo.Object, mockRandom.Object, cache); // act var result = await target.GetRestaurantAsync(sessionGuid, new SearchOptions() { UserIds = users }); // assert Assert.Equal("Bob's Burgers", result.Name); }
public async void LunchService_GetRestaurantAsync_ThrowTooManyRequestsWhenOutOfSuggestions() { Mock <IGetLunchOptions> mockOptions = new Mock <IGetLunchOptions>(); Mock <IRepository> mockRepo = new Mock <IRepository>(); Mock <IChaos> mockRandom = new Mock <IChaos>(); IRestaurantCache cache = new RestaurantCacheMock(); const string expected = "bob's burgers"; Guid sessionId = Guid.NewGuid(); mockOptions.Setup(x => x.GetAvailableRestaurantOptionsAsync(sessionId, It.IsAny <SearchOptions>())).ReturnsAsync(new List <RestaurantDto> { new RestaurantDto { Name = expected, }, }); LunchService target = new LunchService(mockOptions.Object, mockRepo.Object, mockRandom.Object, cache); // act var result = await target.GetRestaurantAsync(sessionId, new SearchOptions()); // assert await Assert.ThrowsAsync <TooManyRequestsException>(async() => await target.GetRestaurantAsync(sessionId, new SearchOptions())); }