public void CreateForUserShould_ReturnFalseIfInvalidUser() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_CustomVoucher") .Options; var dbContext = new CarRentalDbContext(options); var user = new ApplicationUser() { Id = Guid.NewGuid().ToString(), Email = "*****@*****.**", FirstName = "Admin", LastName = "LastAdmin" }; dbContext.Users.Add(user); var usersServiceMock = new Mock <IUsersService>(); usersServiceMock.Setup(p => p.GetUserIdByEmail(user.Email)). Returns(user.Id); var vouchersService = new VouchersService(dbContext, usersServiceMock.Object, this.mapper); var result = vouchersService.CreateForUser(Guid.NewGuid().ToString()).GetAwaiter().GetResult(); Assert.False(result); }
public void CreateCustomForUserShould_CreateVoucherForUser() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_CustomVoucher") .Options; var dbContext = new CarRentalDbContext(options); var user = new ApplicationUser() { Id = Guid.NewGuid().ToString(), Email = "*****@*****.**", FirstName = "Admin", LastName = "LastAdmin" }; dbContext.Users.Add(user); var usersServiceMock = new Mock <IUsersService>(); usersServiceMock.Setup(p => p.GetUserIdByEmail(user.Email)). Returns(user.Id); var vouchersService = new VouchersService(dbContext, usersServiceMock.Object, this.mapper); Assert.False(dbContext.Vouchers.Any(x => x.ApplicationUserId == user.Id)); var random = new Random(); var discount = random.Next(1, 100); vouchersService.CreateForUserCustom(user.Email, discount); Assert.True(dbContext.Vouchers.Any(x => x.ApplicationUserId == user.Id && x.Discount == discount)); }
public void NewCustomerIsCreated() { const string customerLastName = CustomersLastNameToBeDeleted; //Arrange var systemUnderTest = new CarRentalBusinessLayer(); var customerToBeCreated = _autoFixture.Build <CustomerModel>().Without(property => property.CustomerId).With(property => property.CustomerType, CustomerModel.Consumer).With(property => property.LastName, customerLastName) .Create(); var expectedResult = customerToBeCreated; //Act systemUnderTest.CreateNewCustomer( customerToBeCreated.FirstName, customerToBeCreated.LastName, customerToBeCreated.DateOfBirth, customerToBeCreated.Street, customerToBeCreated.City, customerToBeCreated.Postcode, customerToBeCreated.CustomerType); //Assert Customer actualResult; using (var carRentalDbContext = new CarRentalDbContext()) { actualResult = carRentalDbContext.Customers.SingleOrDefault(c => c.LastName == customerLastName); } Assert.True(actualResult != null && expectedResult.LastName == actualResult.LastName); }
public void CreateReviewShould_ReturnFalseIfCantCreateVoucher() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_CreateReview_NoVoucher") .Options; var dbContext = new CarRentalDbContext(options); var user = new ApplicationUser() { Id = Guid.NewGuid().ToString(), Email = "*****@*****.**", FirstName = "Admin", LastName = "LastAdmin" }; var vouchersServiceMock = new Mock <IVouchersService>(); vouchersServiceMock.Setup(x => x.CreateForUser(user.Email)). ReturnsAsync(false); var ordersServiceMock = new Mock <IOrdersService>(); ordersServiceMock.Setup(x => x.DeleteReviewFromOrder(It.IsAny <int>())). ReturnsAsync(true); var reviewsService = new ReviewsService(dbContext, vouchersServiceMock.Object, this.mapper, ordersServiceMock.Object); var random = new Random(); var reviewRating = random.Next(1, 5); var reviewComment = Guid.NewGuid().ToString(); var result = reviewsService.CreateReview(Guid.NewGuid().ToString(), reviewRating, reviewComment).GetAwaiter().GetResult(); Assert.False(result); }
public void CancelRezervationTest() { using (var context = new CarRentalDbContext(this.dbContextOptions)) { var dbRezervation = context.Rezervations.First(x => !x.IsPickedUp && !x.IsReturned); var clientAccountService = new ClientAccountService(context); var rezervationService = new RezervationService(context, clientAccountService); var cancelationFeeRate = 2.00m; var isCancelled = rezervationService.CancelRezervation(dbRezervation.RezervationId, cancelationFeeRate); Assert.IsTrue(isCancelled); var carType = CarTypes.GetCarType((CarTypeEnum)dbRezervation.CarType); // Test the actual calculations in the car type class. var cancellationFee = carType.CancellationFee * cancelationFeeRate; dbRezervation = context.Rezervations.Single(x => x.RezervationId == dbRezervation.RezervationId); Assert.IsTrue(dbRezervation.IsCancelled); Assert.AreEqual(dbRezervation.CancellationFee, cancellationFee); Assert.AreEqual(dbRezervation.CancelationFeeRate, cancelationFeeRate); } }
public void GetUserIdByEmailShould_ReturnCorrectId() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_GetUserIdByEmail") .Options; var dbContext = new CarRentalDbContext(options); var user = new ApplicationUser() { Id = Guid.NewGuid().ToString(), Email = "*****@*****.**", FirstName = "Admin", LastName = "LastAdmin" }; dbContext.Users.Add(user); dbContext.SaveChanges(); var userManager = MockUserManager <ApplicationUser>(); userManager.Setup(x => x.FindByEmailAsync(user.Email)). ReturnsAsync(dbContext.Users.FirstOrDefault(x => x.Email == user.Email)); var usersService = new UsersService(userManager.Object, dbContext); var foundUserId = usersService.GetUserIdByEmail(user.Email); Assert.Equal(user.Id, foundUserId); }
public void GetAllForUserShould_ReturnAllVouchersForUser() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_AllForUser") .Options; var dbContext = new CarRentalDbContext(options); var user = new ApplicationUser() { Id = Guid.NewGuid().ToString(), Email = "*****@*****.**", FirstName = "Admin", LastName = "LastAdmin" }; dbContext.Users.Add(user); var usersServiceMock = new Mock <IUsersService>(); var vouchersService = new VouchersService(dbContext, usersServiceMock.Object, this.mapper); var expectedVouchersCount = 5; for (int i = 0; i < expectedVouchersCount; i++) { vouchersService.CreateForUser(user.Email); } var expected = dbContext.Vouchers. Where(x => x.User.Email == user.Email). Count(); var result = vouchersService.GetAllForUser(user.Email).Count; Assert.Equal(expected, result); }
public void GetDiscountForCodeShould_0IfEmptyString() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_DiscountInvalid_EmptyString") .Options; var dbContext = new CarRentalDbContext(options); var user = new ApplicationUser() { Id = Guid.NewGuid().ToString(), Email = "*****@*****.**", FirstName = "Admin", LastName = "LastAdmin" }; dbContext.Users.Add(user); var usersServiceMock = new Mock <IUsersService>(); usersServiceMock.Setup(p => p.GetUserIdByEmail(user.Email)). Returns(user.Id); var vouchersService = new VouchersService(dbContext, usersServiceMock.Object, this.mapper); var result = vouchersService.GetDiscountForCode(String.Empty).GetAwaiter().GetResult(); Assert.Equal(0, result); }
public void Delete_Should_ReturnTrueIfCorrectId() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_DeleteValidCar") .Options; var dbContext = new CarRentalDbContext(options); var car = new Car { Id = 1, Model = CarModelTestOne, Description = CarModelDescriptionTwo, GearType = Models.Enums.GearType.Automatic, LocationId = locationIdOne, PricePerDay = CarPricePerDayOne, Image = CarImageTest, Year = DateTime.UtcNow.Year }; dbContext.Cars.Add(car); dbContext.SaveChanges(); var carsService = new CarsService(dbContext, this.cloudinary, this.mapper); carsService.DeleteCar(car.Id); var result = dbContext.Cars.Find(car.Id).inUse; Assert.False(result); }
public void AddCar_ShouldInsertValidCar() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_Insert") .Options; var dbContext = new CarRentalDbContext(options); var car = new Car { Id = 1, Model = CarModelTestOne, Description = CarModelDescriptionTwo, GearType = Models.Enums.GearType.Automatic, LocationId = locationIdOne, PricePerDay = CarPricePerDayOne, Image = CarImageTest, Year = DateTime.UtcNow.Year }; var carsService = new CarsService(dbContext, this.cloudinary, this.mapper); carsService.AddCar(car); var expected = 1; var result = dbContext.Cars.Count(); Assert.Equal(expected, result); }
public void ChangeLocationShould_SuccessfullyChangeLocation() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_CarChangeLocation") .Options; var dbContext = new CarRentalDbContext(options); var carsService = new CarsService(dbContext, this.cloudinary, this.mapper); var car = new Car { Id = 1, Model = CarModelTestOne, Description = CarModelDescriptionOne, GearType = Models.Enums.GearType.Automatic, LocationId = locationIdOne, PricePerDay = CarPricePerDayTwo, Image = CarImageTest, Year = DateTime.UtcNow.Year }; carsService.AddCar(car); var expectedLocation = locationIdOne; var actualResultLocation = dbContext.Cars.FirstOrDefault().LocationId; Assert.Equal(expectedLocation, actualResultLocation); carsService.ChangeLocation(car.Id, locationIdTwo); var updatedCar = carsService.FindCar(car.Id).GetAwaiter().GetResult(); Assert.Equal(locationIdTwo, updatedCar.LocationId); }
public void DeleteLocationShould_ReturnFalseIfTheLocationIsReturnPlaceForOrder() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_DeleteLocationWithOrders") .Options; var dbContext = new CarRentalDbContext(options); var locationsService = new LocationsService(dbContext); var location = new Location { Name = locationNameOne }; locationsService.CreateLocation(location); var order = new Order { CarId = 1, ApplicationUserId = Guid.NewGuid().ToString(), PickUpLocationId = 1, ReturnLocationId = 1, Price = 100, RentStart = DateTime.UtcNow.Date, RentEnd = DateTime.UtcNow.Date.AddDays(2) }; dbContext.Orders.Add(order); var result = locationsService.DeleteLocation(locationNameTwo).GetAwaiter().GetResult(); Assert.False(result); }
public IEnumerable <CarModel> FindAvailableCars(DateTime requestedReservationStartDateTime, DateTime requestedReservationEndDateTime, string cityForRequestedReservation) { using (var carRentalDbContext = new CarRentalDbContext()) { var idsOfCarsNotAvailableNow = carRentalDbContext.Reservations .Where(placedReservation => placedReservation.ReservationStart <= requestedReservationEndDateTime && placedReservation.ReservationEnd >= requestedReservationStartDateTime) .Select(placedReservation => placedReservation.CarId); var availableCars = carRentalDbContext.Cars.Where(car => !idsOfCarsNotAvailableNow.Contains(car.CarId) && car.Office.City == cityForRequestedReservation); var resultAvailableCars = availableCars.Select(availableCar => new CarModel { CarId = availableCar.CarId, Category = availableCar.Category, CarBrand = availableCar.CarBrand, KilometerReading = availableCar.KilometerReading, LicenceNumber = availableCar.LicenceNumber, OfficeId = availableCar.OfficeId, Office = new OfficeModel { City = availableCar.Office.City, Country = availableCar.Office.Country, OfficeId = availableCar.Office.OfficeId, Postcode = availableCar.Office.Postcode, Street = availableCar.Office.Street } }).ToList(); return(resultAvailableCars); } }
public void DeleteReviewShould_SuccesfullyDeleteReviewFromOrder() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_DeleteReview") .Options; var dbContext = new CarRentalDbContext(options); var user = new ApplicationUser() { Id = Guid.NewGuid().ToString(), Email = "*****@*****.**", FirstName = "Admin", LastName = "LastAdmin" }; var vouchersServiceMock = new Mock <IVouchersService>(); vouchersServiceMock.Setup(x => x.CreateForUser(user.Email)). ReturnsAsync(true); var order = new Order { Id = Guid.NewGuid().ToString(), ApplicationUserId = user.Id, CarId = 1, PickUpLocationId = 1, ReturnLocationId = 1, Price = 1, }; dbContext.Orders.Add(order); var random = new Random(); var reviewRating = random.Next(1, 5); var reviewComment = Guid.NewGuid().ToString(); order.Review = new Review { Comment = reviewComment, Rating = reviewRating, Id = 1, CarId = 1 }; dbContext.SaveChanges(); var ordersServiceMock = new Mock <IOrdersService>(); ordersServiceMock.Setup(x => x.DeleteReviewFromOrder(order.Review.Id)). ReturnsAsync(true); var reviewsService = new ReviewsService(dbContext, vouchersServiceMock.Object, this.mapper, ordersServiceMock.Object); Assert.True(order.Review != null); reviewsService.DeleteReview(order.Review.Id); var result = dbContext.Orders.FirstOrDefault(x => x.Id == order.Id).Review == null; Assert.True(result); }
public void GetAllLocationNamesShould_ReturnAllNames() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_AllLocationNames") .Options; var dbContext = new CarRentalDbContext(options); var locationsService = new LocationsService(dbContext); var locationList = new List <Location>() { new Location { Name = locationNameOne }, new Location { Name = locationNameTwo }, new Location { Name = locationNameThree }, }; dbContext.Locations.AddRange(locationList); dbContext.SaveChanges(); var expected = new List <string> { locationNameOne, locationNameTwo, locationNameThree }; var result = locationsService.GetAllLocationNames().ToList(); Assert.Equal(expected, result); }
public CustomerModel GetCustomerByCustomerNumber(string customerNumber) { CustomerModel customerModel = null; Customer foundCustomer; using (var carRentalDbContext = new CarRentalDbContext()) { foundCustomer = carRentalDbContext.Customers.SingleOrDefault(customer => customer.CustomerNumber == customerNumber); } if (foundCustomer != null) { customerModel = new CustomerModel { CustomerType = foundCustomer.CustomerType, City = foundCustomer.City, CustomerId = foundCustomer.CustomerId, CustomerNumber = foundCustomer.CustomerNumber, DateOfBirth = foundCustomer.DateOfBirth, Street = foundCustomer.Street, Postcode = foundCustomer.Postcode, LastName = foundCustomer.LastName, FirstName = foundCustomer.FirstName }; } return(customerModel); }
public void Delete_Should_ReturnFalseIfInvalidId() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_DeleteCar") .Options; var dbContext = new CarRentalDbContext(options); var car = new Car { Id = 1, Model = CarModelTestOne, Description = CarModelDescriptionTwo, GearType = Models.Enums.GearType.Automatic, LocationId = locationIdOne, PricePerDay = CarPricePerDayOne, Image = CarImageTest, Year = DateTime.UtcNow.Year }; var carsService = new CarsService(dbContext, this.cloudinary, this.mapper); carsService.AddCar(car); var resultDelete = carsService.DeleteCar(2).GetAwaiter().GetResult(); Assert.False(resultDelete); }
public ReviewsService(CarRentalDbContext dbContext, IVouchersService vouchersService, IMapper mapper, IOrdersService ordersService) { this.dbContext = dbContext; this.vouchersService = vouchersService; this.mapper = mapper; this.ordersService = ordersService; }
public async Task InvokeAsync(HttpContext context, UserManager <ApplicationUser> userManager, RoleManager <ApplicationRole> roleManager, CarRentalDbContext dbContext) { SeedRolesAndLocations(roleManager, dbContext).GetAwaiter().GetResult(); SeedUserInRoles(userManager, dbContext).GetAwaiter().GetResult(); await next(context); }
public OrdersService(CarRentalDbContext dbContext, IUsersService usersService, IMapper mapper, ILocationsService locationsService, ICarsService carsService, IVouchersService vouchersService) { this.dbContext = dbContext; this.usersService = usersService; this.mapper = mapper; this.locationsService = locationsService; this.carsService = carsService; this.vouchersService = vouchersService; }
public void TestCleanup() { using (var carRentalDbContext = new CarRentalDbContext()) { var customerToBeDeleted = carRentalDbContext.Customers.SingleOrDefault(c => c.LastName == CustomersLastNameToBeDeleted); if (customerToBeDeleted != null) { carRentalDbContext.Customers.Remove(customerToBeDeleted); carRentalDbContext.SaveChanges(); } } }
public void UpdateClientAccountTest() { using (var context = new CarRentalDbContext(this.dbContextOptions)) { var service = new ClientAccountService(context); ClientAccountModificationParams parameters = new ClientAccountModificationParams { ClientId = 1, Email = "*****@*****.**", FullName = "Update Client", Phone = "+12345", }; var clientAccountModel = service.Update(parameters); Assert.AreEqual(parameters.Email, clientAccountModel.Email); Assert.AreEqual(parameters.Phone, clientAccountModel.Phone); Assert.AreEqual(parameters.FullName, clientAccountModel.FullName); Assert.AreEqual(parameters.ClientId, clientAccountModel.ClientId); try { service.Update(null); Assert.Fail(); } catch (InvalidParameterException) { } catch { Assert.Fail(); } try { service.Update(new ClientAccountModificationParams() { ClientId = 1000, Email = "*****@*****.**", FullName = "Update Client", Phone = "+12345", }); Assert.Fail(); } catch (NotFoundException) { } catch { Assert.Fail(); } } }
public void ChangeLocationShould_ReturnFalseIfInvalidCarId() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_CarChangeLocation_InvalidCarId") .Options; var dbContext = new CarRentalDbContext(options); var carsService = new CarsService(dbContext, this.cloudinary, this.mapper); var result = carsService.ChangeLocation(1, locationIdOne).GetAwaiter().GetResult(); Assert.False(result); }
private async Task <IRepository> GetFakeRepository() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; var context = new CarRentalDbContext(options); context.Database.EnsureCreated(); await CreateMockUpData(context); return(new DbRepository(context)); }
public void GetAllUsersShould_ReturnEmptyCollectionIfNoUsers() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_GetAllUsers") .Options; var dbContext = new CarRentalDbContext(options); var userManager = MockUserManager <ApplicationUser>(); var usersService = new UsersService(userManager.Object, dbContext); var result = usersService.GetAllUsers().Select(x => x.Email).ToList(); Assert.Empty(result); }
public void IsAlreadyRentedShould_ReturnFalseIfCarIsNotRented() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_IsRentedCarFalse") .Options; var dbContext = new CarRentalDbContext(options); var carsService = new CarsService(dbContext, this.cloudinary, this.mapper); dbContext.Locations.Add(new Location { Id = 2, Name = LocationNameTwo }); var insertCars = new List <Car> { new Car { Id = 1, Model = CarModelTestOne, Description = CarModelDescriptionOne, GearType = Models.Enums.GearType.Automatic, LocationId = locationIdTwo, PricePerDay = locationIdOne, Image = CarImageTest, Year = DateTime.UtcNow.Year }, new Car { Id = 2, Model = CarModelTestTwo, Description = CarModelDescriptionTwo, GearType = Models.Enums.GearType.Automatic, LocationId = locationIdTwo, PricePerDay = CarPricePerDayTwo, Image = CarImageTest, Year = DateTime.UtcNow.Year }, }; insertCars.ForEach(x => carsService.AddCar(x).GetAwaiter().GetResult()); dbContext.SaveChanges(); var result = carsService.IsAlreadyRented(DateTime.UtcNow, DateTime.UtcNow.AddDays(2), insertCars[0].Id) .GetAwaiter().GetResult(); Assert.False(result); }
public void GetCarModelByIdShould_ReturnEmptyString() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_InvalidCarModel") .Options; var dbContext = new CarRentalDbContext(options); var carsService = new CarsService(dbContext, this.cloudinary, this.mapper); var expected = String.Empty; var result = carsService.GetCarModelById(1).GetAwaiter().GetResult(); Assert.Equal(expected, result); }
public BaseTestInitialization() { this.connection = new SqliteConnection("DataSource=:memory:"); this.connection.Open(); this.dbContextOptions = new DbContextOptionsBuilder <CarRentalDbContext>() .UseSqlite(connection).Options; // Create the schema in the database using (var context = new CarRentalDbContext(this.dbContextOptions)) { context.Database.EnsureCreated(); } }
public void GetCarModelByIdShould_ReturnTheRightCarModel() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_CarModel") .Options; var dbContext = new CarRentalDbContext(options); var carsService = new CarsService(dbContext, this.cloudinary, this.mapper); dbContext.Locations.Add(new Location { Id = 2, Name = LocationNameTwo }); var insertCars = new List <Car> { new Car { Id = 1, Model = CarModelTestOne, Description = CarModelDescriptionOne, GearType = Models.Enums.GearType.Automatic, LocationId = locationIdTwo, PricePerDay = locationIdOne, Image = CarImageTest, Year = DateTime.UtcNow.Year }, new Car { Id = 2, Model = CarModelTestTwo, Description = CarModelDescriptionTwo, GearType = Models.Enums.GearType.Automatic, LocationId = locationIdTwo, PricePerDay = CarPricePerDayTwo, Image = CarImageTest, Year = DateTime.UtcNow.Year }, }; insertCars.ForEach(x => carsService.AddCar(x).GetAwaiter().GetResult()); dbContext.SaveChanges(); var expected = CarModelTestTwo; var result = carsService.GetCarModelById(insertCars[1].Id).GetAwaiter().GetResult(); Assert.Equal(expected, result); }
public void FindCarForEditShould_ReturnNullIfCarNotInUse() { var options = new DbContextOptionsBuilder <CarRentalDbContext>() .UseInMemoryDatabase(databaseName: "CarRental_Database_ForEditCar_notInUse") .Options; var dbContext = new CarRentalDbContext(options); var carsService = new CarsService(dbContext, this.cloudinary, this.mapper); var insertCars = new List <Car> { new Car { Id = 1, Model = CarModelTestOne, Description = CarModelDescriptionOne, GearType = Models.Enums.GearType.Automatic, LocationId = locationIdOne, PricePerDay = locationIdOne, Image = CarImageTest, Year = DateTime.UtcNow.Year }, new Car { Id = 2, Model = CarModelTestTwo, Description = CarModelDescriptionTwo, GearType = Models.Enums.GearType.Automatic, LocationId = locationIdTwo, PricePerDay = CarPricePerDayTwo, Image = CarImageTest, Year = DateTime.UtcNow.Year, inUse = false }, }; insertCars.ForEach(x => carsService.AddCar(x).GetAwaiter().GetResult()); var actualCountOfCars = dbContext.Cars.Count(); Assert.Equal(2, actualCountOfCars); var result = carsService.FindCarForEdit(2).GetAwaiter().GetResult(); Assert.Null(result); }