public async Task <ICollection <Restaurant> > GetRestaurantsByCityState(string city, string state) { ICollection <Restaurant> foundRestaurants = null; using (RestaurantReviewsUnitOfWork uow = new RestaurantReviewsUnitOfWork(dbPath)) { var _results = await uow.FindRestaurantsByCityStateAsync(city, state); if (_results.Successful) { foundRestaurants = _results.Results; } else { // here we can add special handling in the case of failure } } return(foundRestaurants); }
public async Task GetListOfRestaurantsByCityStateTests() { RestaurantAddress address1 = new RestaurantAddress() { streetAddress = "100 Main St", addrLine2 = string.Empty, city = "Pittsburgh", state = "PA", zipcode = "15108" }; Restaurant restaurant1 = new Restaurant() { name = "Restaurant 1", address = address1, addressId = address1.id, dateCreated = DateTime.Now, dateModified = DateTime.Now, phoneNumber = "412-444-4444", acceptsReservations = false }; RestaurantAddress address2 = new RestaurantAddress() { streetAddress = "100 Main St", addrLine2 = string.Empty, city = "Pittsburgh", state = "PA", zipcode = "15108" }; Restaurant restaurant2 = new Restaurant() { name = "Restaurant 2", address = address2, addressId = address2.id, dateCreated = DateTime.Now, dateModified = DateTime.Now, phoneNumber = "412-555-5555", acceptsReservations = false }; List <Restaurant> restaurants = new List <Restaurant>(); restaurants.Add(restaurant1); restaurants.Add(restaurant2); // Test the 'Happy Path' repositoryMock.Setup(m => m.FindRestaurantsByCityStateAsync(It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(restaurants); using (RestaurantReviewsUnitOfWork uow = new RestaurantReviewsUnitOfWork(repositoryMock.Object)) { var _results = await uow.FindRestaurantsByCityStateAsync("pittsburgh", "pa"); Assert.IsNotNull(_results); Assert.IsInstanceOfType(_results.Results, typeof(ICollection <Restaurant>)); Assert.IsTrue(_results.Results.Count == 2); } // Test No restaurants found restaurants.Clear(); repositoryMock.Setup(m => m.FindRestaurantsByCityStateAsync(It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(restaurants); using (RestaurantReviewsUnitOfWork uow = new RestaurantReviewsUnitOfWork(repositoryMock.Object)) { var _results = await uow.FindRestaurantsByCityStateAsync("boston", "ma"); Assert.IsNotNull(_results); Assert.IsInstanceOfType(_results.Results, typeof(ICollection <Restaurant>)); Assert.IsTrue(_results.Results.Count == 0); Assert.AreEqual(_results.Message, "Unable to locate any Restaurants in the city/state provided"); } // Test empty city/state Handling using (RestaurantReviewsUnitOfWork uow = new RestaurantReviewsUnitOfWork(repositoryMock.Object)) { var _results = await uow.FindRestaurantsByCityStateAsync(string.Empty, string.Empty); Assert.IsNotNull(_results); Assert.IsNotNull(_results.Exception); Assert.AreEqual(_results.Exception.Message, "City and State are both required"); Assert.IsNull(_results.Results); } // Test Exception Handling Exception ex = new Exception("Any Exception"); repositoryMock.Setup(m => m.FindRestaurantsByCityStateAsync(It.IsAny <string>(), It.IsAny <string>())).ThrowsAsync(ex); using (RestaurantReviewsUnitOfWork uow = new RestaurantReviewsUnitOfWork(repositoryMock.Object)) { var _results = await uow.FindRestaurantsByCityStateAsync("pittsburgh", "pa"); Assert.IsNotNull(_results); Assert.IsNotNull(_results.Exception); Assert.IsNull(_results.Results); } }