Esempio n. 1
0
        public ActionResult Create([Bind(Include = "Id,RentedAt,ReturnedAt,VehicleId,ClientId")] RentalModel rental)
        {
            if (ModelState.IsValid)
            {
                rental.Id = Guid.NewGuid();
                db.Rentals.Add(rental);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ClientId  = new SelectList(db.Clients, "Id", "FirstName", rental.ClientId);
            ViewBag.VehicleId = new SelectList(db.Vehicles, "Id", "LicensePlate", rental.VehicleId);
            return(View(rental));
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        /// <summary>
        /// AddCustomer variable decleration
        /// </summary>
        /// <param name="customerModel"></param>
        public void AddCustomer(CustomerModel customerModel)
        {
            try
            {
                // NewCustomerEntity entry
                newCustomerEntity = new Customer
                {
                    City           = customerModel.City,
                    CustomerType   = customerModel.CustomerType,
                    Street         = customerModel.Street,
                    Postcode       = customerModel.Postcode,
                    CustomerId     = customerModel.CustomerId,
                    CustomerNumber = customerModel.CustomerNumber,
                    DateOfBirth    = customerModel.DateOfBirth,
                    FirstName      = customerModel.FirstName,
                    LastName       = customerModel.LastName
                };

                //using (var carRentalDbContext = new CarRentalDbContext())
                //{
                // Add the newCustomerEntity to the Customers table in database
                carRentalDbContext.Customers.Add(newCustomerEntity);
                // Savechanges to database
                carRentalDbContext.SaveChanges();
                //}
            }
            catch { throw new ArgumentNullException(); }
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        public void AddCustomer(Customer customerModel)
        {
            try
            {
                newCustomerEntity = new Customer
                {
                    City           = customerModel.City,
                    CustomerType   = customerModel.CustomerType,
                    Street         = customerModel.Street,
                    Postcode       = customerModel.Postcode,
                    CustomerId     = customerModel.CustomerId,
                    CustomerNumber = customerModel.CustomerNumber,
                    DateOfBirth    = customerModel.DateOfBirth,
                    FirstName      = customerModel.FirstName,
                    LastName       = customerModel.LastName
                };

                carRentalDbContext.Customers.Add(newCustomerEntity);
                carRentalDbContext.SaveChanges();
            }
            catch
            {
                throw new Exception();
            }
        }
 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();
         }
     }
 }
Esempio n. 9
0
        public ActionResult Create([Bind(Include = "Id,Name")] VehicleBrand vehicleBrand)
        {
            var error = string.Empty;

            if (ModelState.IsValid)
            {
                try
                {
                    vehicleBrand.Id = Guid.NewGuid();
                    db.VehicleBrands.Add(vehicleBrand);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (DbUpdateException ex)
                {
                    error = ex.GetDeepestMessage();
                }
            }

            ViewBag.Error = error;

            return(View(vehicleBrand));
        }
        public ActionResult Create([Bind(Include = "Id,IdentificationNumber,ValidUntil,Issuer")] DriverLicense driverLicense)
        {
            var error = string.Empty;

            if (ModelState.IsValid)
            {
                try
                {
                    driverLicense.Id = Guid.NewGuid();
                    db.DriverLicenses.Add(driverLicense);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (DbUpdateException ex)
                {
                    error = ex.GetDeepestMessage();
                }
            }

            ViewBag.Error = error;

            return(View(driverLicense));
        }
Esempio n. 11
0
        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);
        }
Esempio n. 12
0
        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);
        }
Esempio n. 13
0
        public ActionResult Create([Bind(Include = "Id,FirstName,LastName,IdentificationNumber,Address,DriverLicenseId")] Client client)
        {
            var error = string.Empty;

            if (ModelState.IsValid)
            {
                try
                {
                    client.Id = Guid.NewGuid();
                    db.Clients.Add(client);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (DbUpdateException ex)
                {
                    error = ex.GetDeepestMessage();
                }
            }

            ViewBag.DriverLicenseId = new SelectList(db.DriverLicenses, "Id", "IdentificationNumber", client.DriverLicenseId);
            ViewBag.Error           = error;

            return(View(client));
        }
        public ActionResult Create([Bind(Include = "Id,VehicleBrandId,Name,MaxPassengers,BigLuggage")] VehicleModel vehicleModel)
        {
            var error = string.Empty;

            if (ModelState.IsValid)
            {
                try
                {
                    vehicleModel.Id = Guid.NewGuid();
                    db.VehicleModels.Add(vehicleModel);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (DbUpdateException ex)
                {
                    error = ex.GetDeepestMessage();
                }
            }

            ViewBag.VehicleBrandId = new SelectList(db.VehicleBrands, "Id", "Name", vehicleModel.VehicleBrandId);
            ViewBag.Error          = error;

            return(View(vehicleModel));
        }
Esempio n. 15
0
        public ActionResult Create([Bind(Include = "Id,LicensePlate,VehicleModelId,TechnicalInspectionDoneAt,PricePerDay,Type")] Vehicle vehicle)
        {
            var error = string.Empty;

            if (ModelState.IsValid)
            {
                try
                {
                    vehicle.Id = Guid.NewGuid();
                    db.Vehicles.Add(vehicle);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (DbUpdateException ex)
                {
                    error = ex.GetDeepestMessage();
                }
            }

            ViewBag.VehicleModelId = new SelectList(db.VehicleModels, "Id", "Name", vehicle.VehicleModelId);
            ViewBag.Error          = error;

            return(View(vehicle));
        }
Esempio n. 16
0
        public void UserVoucherShould_MakeTheVoucherUsed()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_UseVoucher")
                          .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 random   = new Random();
            var discount = random.Next(1, 5);

            var voucher = new Voucher
            {
                ApplicationUserId = user.Id,
                Discount          = discount,
                VoucherCode       = Guid.NewGuid().ToString(),
                Status            = VoucherStatus.Active
            };

            dbContext.Vouchers.Add(voucher);
            dbContext.SaveChanges();

            var vouchersService = new VouchersService(dbContext, usersServiceMock.Object, this.mapper);

            vouchersService.UseVoucher(voucher.VoucherCode);

            var expected = VoucherStatus.Used;
            var result   = dbContext.Vouchers.FirstOrDefault(x => x.Id == voucher.Id).Status;

            Assert.Equal(expected, result);
        }
Esempio n. 17
0
        public void GetDiscountForCodeShould_ReturnRightDiscountPercent()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_DiscountValid")
                          .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(It.IsAny <string>())).
            Returns((string res) => res);

            var vouchersService = new VouchersService(dbContext, usersServiceMock.Object, this.mapper);

            var random   = new Random();
            var discount = random.Next(1, 5);

            var voucher = new Voucher
            {
                ApplicationUserId = user.Id,
                Discount          = discount,
                VoucherCode       = Guid.NewGuid().ToString(),
                Status            = VoucherStatus.Active
            };

            dbContext.Vouchers.Add(voucher);
            dbContext.SaveChanges();

            var expected = discount;
            var result   = vouchersService.GetDiscountForCode(voucher.VoucherCode).GetAwaiter().GetResult();

            Assert.Equal(expected, result);
        }
        public void SetupClientService()
        {
            // Add data to the database.
            using (var context = new CarRentalDbContext(this.dbContextOptions))
            {
                context.ClientAccounts.Add(new ClientAccount
                {
                    Email    = "*****@*****.**",
                    FullName = "Client Name 1",
                    Phone    = "+11111",
                });

                context.ClientAccounts.Add(new ClientAccount
                {
                    Email    = "*****@*****.**",
                    FullName = "Client Name 2",
                    Phone    = "+22222",
                });
                context.SaveChanges();
            }
        }
Esempio n. 19
0
        public void AddCustomer(CustomerModel customerModel)
        {
            var customerEntity = new Customer
            {
                City           = customerModel.City,
                CustomerType   = customerModel.CustomerType,
                Street         = customerModel.Street,
                Postcode       = customerModel.Postcode,
                CustomerId     = customerModel.CustomerId,
                CustomerNumber = customerModel.CustomerNumber,
                DateOfBirth    = customerModel.DateOfBirth,
                FirstName      = customerModel.FirstName,
                LastName       = customerModel.LastName
            };


            using (var carRentalDbContext = new CarRentalDbContext())
            {
                carRentalDbContext.Customers.Add(customerEntity);
                carRentalDbContext.SaveChanges();
            }
        }
Esempio n. 20
0
        public void GetAllUsersShould_ReturnUsersInDatabase()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_GetAllUsers")
                          .Options;
            var dbContext = new CarRentalDbContext(options);

            var user1 = new ApplicationUser()
            {
                Id        = Guid.NewGuid().ToString(),
                Email     = "*****@*****.**",
                FirstName = "Admin",
                LastName  = "LastAdmin"
            };

            var user2 = new ApplicationUser()
            {
                Id        = Guid.NewGuid().ToString(),
                Email     = "*****@*****.**",
                FirstName = "Admin2",
                LastName  = "LastAdmin2"
            };

            dbContext.Users.Add(user1);
            dbContext.Users.Add(user2);
            dbContext.SaveChanges();

            var userManager  = MockUserManager <ApplicationUser>();
            var usersService = new UsersService(userManager.Object, dbContext);

            var exptected = new List <string> {
                user1.Email, user2.Email
            };
            var result = usersService.GetAllUsers().Select(x => x.Email).ToList();

            Assert.Equal(exptected, result);
        }
        public void SetupRezervationService()
        {
            this.SetupClientService();

            using (var context = new CarRentalDbContext(this.dbContextOptions))
            {
                var client1 = context.ClientAccounts.First();
                var client2 = context.ClientAccounts.Last();

                context.Rezervations.Add(new Rezervation
                {
                    PickUpDate     = DateTime.Today.AddDays(1),                 // set to Today for simplicity.
                    ReturnDate     = DateTime.Today.AddDays(5),
                    CarPlateNumber = "CA1234AC",
                    CarType        = 2,
                    RentaltFee     = 1152.00m,
                    DepositFee     = 138.24m,
                    ClientId       = client1.ClientId,
                });

                context.Rezervations.Add(new Rezervation
                {
                    PickUpDate     = DateTime.Today.AddDays(1),                 // set to Today for simplicity.
                    ReturnDate     = DateTime.Today.AddDays(5),
                    CarPlateNumber = "CA1234AC",
                    CarType        = 2,
                    RentaltFee     = 1152.00m,
                    DepositFee     = 138.24m,
                    IsPickedUp     = true,
                    IsReturned     = true,
                    ClientId       = client1.ClientId,
                });

                context.Rezervations.Add(new Rezervation
                {
                    PickUpDate     = DateTime.Today.AddDays(1),                 // set to Today for simplicity.
                    ReturnDate     = DateTime.Today.AddDays(5),
                    CarPlateNumber = "BT1234UC",
                    CarType        = 1,
                    RentaltFee     = 50.00m,
                    DepositFee     = 2.5m,
                    IsPickedUp     = true,
                    ClientId       = client2.ClientId,
                });

                context.Rezervations.Add(new Rezervation
                {
                    PickUpDate         = DateTime.Today.AddDays(1),             // set to Today for simplicity.
                    ReturnDate         = DateTime.Today.AddDays(2),
                    CarPlateNumber     = "SD1234T",
                    CarType            = 3,
                    RentaltFee         = 50.00m,
                    DepositFee         = 3.5m,
                    IsCancelled        = true,
                    CancelationFeeRate = 2,
                    CancellationFee    = 50.0m,
                    ClientId           = client2.ClientId,
                });

                context.Rezervations.Add(new Rezervation
                {
                    PickUpDate     = DateTime.Today.AddDays(1),                 // set to Today for simplicity.
                    ReturnDate     = DateTime.Today.AddDays(5),
                    CarPlateNumber = "CA1234AC",
                    CarType        = 2,
                    RentaltFee     = 1152.00m,
                    DepositFee     = 138.24m,
                    IsPickedUp     = true,
                    IsReturned     = true,
                    ClientId       = client1.ClientId,
                });

                context.SaveChanges();
            }
        }
Esempio n. 22
0
        public void GetAllReviewsShould_ReturnAllReviewsFromCustomers()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_AllReviews")
                          .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 ordersServiceMock = new Mock <IOrdersService>();

            ordersServiceMock.Setup(x => x.DeleteReviewFromOrder(It.IsAny <int>())).
            ReturnsAsync(true);

            var random         = new Random();
            var reviewsService = new ReviewsService(dbContext, vouchersServiceMock.Object, this.mapper, ordersServiceMock.Object);

            var ordersToInsert = 10;

            for (int i = 1; i <= ordersToInsert; i++)
            {
                var order = new Order
                {
                    Id = Guid.NewGuid().ToString(),
                    ApplicationUserId = user.Id,
                    CarId             = 1,
                    PickUpLocationId  = 1,
                    ReturnLocationId  = 1,
                    Price             = 1,
                };

                var reviewRating  = random.Next(1, 5);
                var reviewComment = Guid.NewGuid().ToString();

                order.Review = new Review
                {
                    Comment = reviewComment,
                    Rating  = reviewRating,
                    Id      = i,
                    CarId   = 1
                };

                dbContext.Orders.Add(order);
                dbContext.SaveChanges();
            }

            var expected = ordersToInsert;
            var result   = reviewsService.GetAllReviews().Count();

            Assert.Equal(expected, result);
        }
Esempio n. 23
0
        public void GetAvailableCarsShould_ReturnEmptyCollectionIfAllCarsAreRented()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_NoAvailableCars")
                          .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
                },
                new Car
                {
                    Id          = 3,
                    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());

            var insertRents = new List <CarRentDays>
            {
                new CarRentDays
                {
                    CarId    = 1,
                    RentDate = DateTime.UtcNow.Date.AddDays(1)
                },
                new CarRentDays
                {
                    CarId    = 1,
                    RentDate = DateTime.UtcNow.Date.AddDays(2)
                },
                new CarRentDays
                {
                    CarId    = 2,
                    RentDate = DateTime.UtcNow.Date
                },
                new CarRentDays
                {
                    CarId    = 2,
                    RentDate = DateTime.UtcNow.Date.AddDays(1)
                },
                new CarRentDays
                {
                    CarId    = 2,
                    RentDate = DateTime.UtcNow.Date.AddDays(2)
                },
                new CarRentDays
                {
                    CarId    = 3,
                    RentDate = DateTime.UtcNow.Date.AddDays(1)
                },
            };

            dbContext.CarRentDays.AddRange(insertRents);
            dbContext.SaveChanges();

            var actualResultIds = carsService
                                  .GetAvailableCars(DateTime.UtcNow.Date, DateTime.UtcNow.Date.AddDays(1), LocationNameTwo)
                                  .Select(p => p.Id);

            Assert.Empty(actualResultIds);
        }
Esempio n. 24
0
        public void GetAllCarsShould_OrderByRatingDescending()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_OrderRatingDSC")
                          .Options;
            var dbContext = new CarRentalDbContext(options);

            var carsService = new CarsService(dbContext, this.cloudinary, this.mapper);

            dbContext.Locations.Add(new Location
            {
                Id   = 1,
                Name = LocationNameOne
            });

            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  = locationIdOne,
                    PricePerDay = locationIdOne + 100,
                    Image       = CarImageTest,
                    Year        = DateTime.UtcNow.Year
                },
                new Car
                {
                    Id          = 2,
                    Model       = CarModelTestTwo,
                    Description = CarModelDescriptionTwo,
                    GearType    = Models.Enums.GearType.Automatic,
                    LocationId  = locationIdTwo,
                    PricePerDay = CarPricePerDayTwo + 150,
                    Image       = CarImageTest,
                    Year        = DateTime.UtcNow.Year
                },
                new Car
                {
                    Id          = 3,
                    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());

            var insertReviews = new List <Review>
            {
                new Review
                {
                    CarId   = 1,
                    Comment = ReviewComment,
                    Rating  = 4
                },
                new Review
                {
                    CarId   = 1,
                    Comment = ReviewComment,
                    Rating  = 3
                },
                new Review
                {
                    CarId   = 2,
                    Comment = ReviewComment,
                    Rating  = 5
                },
                new Review
                {
                    CarId   = 3,
                    Comment = ReviewComment,
                    Rating  = 5
                },
                new Review
                {
                    CarId   = 3,
                    Comment = ReviewComment,
                    Rating  = 4
                },
            };

            dbContext.Reviews.AddRange(insertReviews);
            dbContext.SaveChanges();

            var expectedResultIds = new List <int> {
                2, 3, 1
            };
            var actualResultIds = carsService.GetAllCars(OrderCarsByRatingDescending).Select(p => p.Id);

            Assert.Equal(expectedResultIds, actualResultIds);
        }
Esempio n. 25
0
 public Car Add(Car car)
 {
     context.Cars.Add(car);
     context.SaveChanges();
     return(car);
 }
Esempio n. 26
0
 public Reservation Add(Reservation reservation)
 {
     context.Reservations.Add(reservation);
     context.SaveChanges();
     return(reservation);
 }