public ActionResult Save(AccommodationFormViewModel accommodationModel) { if (!ModelState.IsValid) { var viewModel = new AccommodationFormViewModel { Categories = _db.Categories.ToList() }; return(View("AccommodationForm", viewModel)); } var userId = User.Identity.GetUserId(); if (accommodationModel.Id == 0) { AccommodationManager.Add(accommodationModel, userId); } else { AccommodationManager.Update(accommodationModel); } return(RedirectToAction("Index", "Accommodations")); }
public void TestFilledSearchAccommodations() { var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>(); optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options); context.Accommodations.Add(new Accommodation() { Beds = 4, Continent = "Europe", Country = "Netherlands", Description = "Van der Valk hotels", Location = "Gilze", MaxPersons = 4, Name = "Van der valk Gilze", Rooms = 5 }); context.SaveChanges(); IAccommodationRepository repository = new EFAccommodationRepository(context); IReviewRepository reviewRepo = new EFReviewRepository(context, repository); IAccommodationManager manager = new AccommodationManager(repository, reviewRepo); SearchBarViewComponent target = new SearchBarViewComponent(manager); AccommodationSearchViewModel results = (AccommodationSearchViewModel)(target.Invoke() as ViewViewComponentResult).ViewData.Model; Assert.NotNull(results); Assert.NotEmpty(results.Accommodations); }
public void TestEmptySearchAccommodations() { var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>(); optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options); IAccommodationRepository repository = new EFAccommodationRepository(context); IReviewRepository reviewRepo = new EFReviewRepository(context, repository); IAccommodationManager manager = new AccommodationManager(repository, reviewRepo); SearchController target = new SearchController(manager); AccommodationSearchViewModel searchModel = new AccommodationSearchViewModel() { StartDate = new DateTime(2017, 10, 12), EndDate = new DateTime(2017, 10, 22), Location = "Gilze", Persons = 4 }; ViewResult result = target.Results(searchModel); AccommodationSearchResultsViewModel model = result.Model as AccommodationSearchResultsViewModel; Assert.NotNull(model); Assert.NotNull(model.Search); Assert.Empty(model.Accommodations); Assert.Equal(searchModel, model.Search); Assert.Equal("NoResults", result.ViewName); }
public void TestFilledSearchAccommodations() { var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>(); optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options); context.Accommodations.Add(new Accommodation() { NotAvailableDates = new List <DateEntity>() { new DateEntity() { Date = new DateTime(2017, 10, 11) }, new DateEntity() { Date = new DateTime(2017, 10, 23) }, }, Location = "Amsterdam", MaxPersons = 4 }); context.SaveChanges(); IAccommodationRepository repository = new EFAccommodationRepository(context); IReviewRepository reviewRepo = new EFReviewRepository(context, repository); IAccommodationManager manager = new AccommodationManager(repository, reviewRepo); SearchController target = new SearchController(manager); AccommodationSearchViewModel searchModel = new AccommodationSearchViewModel() { StartDate = new DateTime(2017, 10, 12), EndDate = new DateTime(2017, 10, 22), Location = "Amsterdam", Persons = 4 }; ViewResult result = target.Results(searchModel); AccommodationSearchResultsViewModel resultsModel = result.Model as AccommodationSearchResultsViewModel; Assert.NotNull(resultsModel); Assert.NotNull(resultsModel.Accommodations); Assert.NotEmpty(resultsModel.Accommodations); Assert.True(resultsModel.Accommodations.Count() == 1); Assert.NotNull(resultsModel.Search); Assert.Equal(searchModel, resultsModel.Search); Assert.Equal("Results", result.ViewName); }
public void TestEmptySearchAccommodations() { var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>(); optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options); IAccommodationRepository repository = new EFAccommodationRepository(context); IReviewRepository reviewRepo = new EFReviewRepository(context, repository); IAccommodationManager manager = new AccommodationManager(repository, reviewRepo); SearchBarViewComponent target = new SearchBarViewComponent(manager); AccommodationSearchViewModel results = (AccommodationSearchViewModel)(target.Invoke() as ViewViewComponentResult).ViewData.Model; Assert.NotNull(results); Assert.Empty(results.Accommodations); }
public void TestAccommodationsTrueRecommended() { var optionsBuilder = new DbContextOptionsBuilder <HomeMyDayDbContext>(); optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); HomeMyDayDbContext context = new HomeMyDayDbContext(optionsBuilder.Options); context.Accommodations.AddRange( new Accommodation() { Description = DEFAULT_ACCOMMODATION_DESCRIPTION, Recommended = false }, new Accommodation() { Description = DEFAULT_ACCOMMODATION_DESCRIPTION, Recommended = true }, new Accommodation() { Description = DEFAULT_ACCOMMODATION_DESCRIPTION, Recommended = false }, new Accommodation() { Description = DEFAULT_ACCOMMODATION_DESCRIPTION, Recommended = true } ); context.SaveChanges(); IAccommodationRepository repository = new EFAccommodationRepository(context); IReviewRepository reviewRepository = new EFReviewRepository(context, repository); IAccommodationManager manager = new AccommodationManager(repository, reviewRepository); RecommendedAccommodationViewComponent component = new RecommendedAccommodationViewComponent(manager); IEnumerable <Accommodation> accommodations = ((IEnumerable <Accommodation>)(component.Invoke() as ViewViewComponentResult).ViewData.Model); Assert.True(accommodations.Count() == 2); }
/// <summary> /// Initializes an instance of BookingController, complete with mocked repositories and services. /// </summary> /// <param name="shouldHaveAccommodations">If true, the mocked repository will always return an accommodation. If false, always throws a KeyNotFoundException instead.</param> /// <returns>An instance of BookingController with mocked services.</returns> private BookingController GetController(bool shouldHaveAccommodations) { //Mock accommodation repo var accommodationRepo = new Mock <IAccommodationRepository>(); var reviewRepo = new EFReviewRepository(null, accommodationRepo.Object); var accommodationManager = new AccommodationManager(accommodationRepo.Object, reviewRepo); if (shouldHaveAccommodations) { //Setup fake accommodation var accommodation = new Accommodation() { Id = 1, MaxPersons = 4, Name = "Test Accommodation" }; accommodationRepo.Setup(r => r.GetAccommodation(It.IsAny <long>())).Returns(accommodation); } else { //If there are no accommodations, always throw a KeyNotFoundException accommodationRepo.Setup(r => r.GetAccommodation(It.IsAny <long>())).Throws(new KeyNotFoundException()); } //Setup fake countries var countries = new List <Country>() { new Country() { Id = 1, CountryCode = "NED", Name = "Netherlands", }, new Country() { Id = 2, CountryCode = "USA", Name = "United States", }, new Country() { Id = 3, CountryCode = "NOR", Name = "Norway", }, }; //Mock country repo var countryRepo = new Mock <ICountryRepository>(); var countryManager = new CountryManager(countryRepo.Object); countryRepo.Setup(r => r.Countries).Returns(countries); //Setup fake google API options var fakeApiOptions = new GoogleApiServiceOptions() { ClientApiKey = "Testkey" }; //Mock google api options var googleOpts = new Mock <IOptions <GoogleApiServiceOptions> >(); googleOpts.Setup(g => g.Value).Returns(fakeApiOptions); var googleOptsManager = new GoogleMapService(googleOpts.Object); var sessionMock = new Mock <ISession>(); byte[] emptyJsonObjectString = Encoding.ASCII.GetBytes("{}"); sessionMock.Setup(s => s.TryGetValue(It.IsAny <string>(), out emptyJsonObjectString)); sessionMock.Setup(s => s.Set(It.IsAny <string>(), It.IsAny <byte[]>())); //Set up a default HTTP context so the session can be mocked var httpContext = new DefaultHttpContext(); httpContext.Session = sessionMock.Object; //Setup controller var controller = new BookingController(accommodationManager, countryManager, googleOptsManager); controller.ControllerContext = new ControllerContext() { HttpContext = httpContext, RouteData = new RouteData() }; return(controller); }