Exemple #1
0
        public void CanAddCar()
        {
            var called = false;
            var expected_try_add_result = false;

            var mock_repository = new Mock <ICarsRepository>();

            mock_repository.Setup(v => v.TryAdd(It.IsAny <Guid>(), It.IsAny <Car>()))
            .Returns((Guid _g, Car _c) => { called = true; return(expected_try_add_result); });

            var cars_service = new CarsService(mock_repository.Object);

            (var car, var maybe_error) = cars_service.AddCar(Clients[0], new NewCar {
                Year = 1885
            });
            Assert.That(!called);
            Assert.That(car, Is.Null);
            Assert.That(maybe_error?.Reason, Does.Match("1886. .* is earlier than that"));
            Assert.That(maybe_error?.StatusCode, Is.EqualTo(400));

            (car, maybe_error) = cars_service.AddCar(Clients[1], new NewCar {
                Year = DateTime.Now.Year + 1
            });
            Assert.That(!called);
            Assert.That(car, Is.Null);
            Assert.That(maybe_error?.Reason, Does.Match("Your car's built year .* is in the future."));
            Assert.That(maybe_error?.StatusCode, Is.EqualTo(400));

            (car, maybe_error) = cars_service.AddCar(Clients[2], new NewCar {
                Year = DateTime.Now.Year - 1
            });
            Assert.That(called);
            Assert.That(car, Is.Null);
            Assert.That(maybe_error?.Reason, Does.Match("Unexpected error: failed to add new car"));
            Assert.That(maybe_error?.StatusCode, Is.EqualTo(500));

            called = false;
            expected_try_add_result = true;
            (car, maybe_error)      = cars_service.AddCar(Clients[3], new NewCar
            {
                Year  = DateTime.Now.Year - 1,
                Model = "Model 1",
                Make  = "Make 1",
                Stock = 20
            });
            Assert.That(called);
            Assert.That(car.Model, Is.EqualTo("Model 1"));
            Assert.That(car.Make, Is.EqualTo("Make 1"));
            Assert.That(car.Stock, Is.EqualTo(20));
            Assert.That(car.Id, Is.Not.EqualTo(Guid.Empty));
            Assert.That(Cars.Where(c => c.Id == car.Id), Is.Empty);
            Assert.That(maybe_error, Is.Null);
        }
        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 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 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_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);
        }
Exemple #7
0
        public void CarsService_VerifyTheMethodAddCar_IsCalled_WhenPassedParametersAreCorrect()
        {
            var car = new Car()
            {
                Id = 1, Manufacturer = "VW", Model = "Golf",
            };
            var mockedRepo = new Mock <IRepository <Car> >();

            mockedRepo.Setup(m => m.Add(car)).Verifiable();
            var service = new CarsService(mockedRepo.Object);

            service.AddCar(car);

            mockedRepo.Verify(m => m.Add(car), Times.Exactly(1));
        }
        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);
        }
Exemple #9
0
        public void IsCalled_WhenPassedParametersAreCorrect()
        {
            // Arrange
            var car = new Car()
            {
                Id = new Guid(), Manufacturer = "VW", Model = "Golf",
            };
            var mockedRepo = new Mock <IEfGenericRepository <Car> >();

            mockedRepo.Setup(m => m.Add(car)).Verifiable();
            var service = new CarsService(mockedRepo.Object);

            // Act
            service.AddCar(car);

            // Assert
            mockedRepo.Verify(m => m.Add(car), Times.Exactly(1));
        }
        public void RentCarShould_CreateRentDaysForCar()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_RentCarDays")
                          .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 startDate = DateTime.UtcNow.Date;
            var endDate   = startDate.AddDays(3);

            carsService.RentCar(startDate, endDate, car.Id);

            var expectedDates = new List <DateTime>();

            for (var dt = startDate; dt <= endDate; dt = dt.AddDays(1))
            {
                expectedDates.Add(dt);
            }

            var actualDatesCount =
                dbContext.CarRentDays.Where(x => x.CarId == car.Id && x.RentDate >= startDate).Count();

            Assert.Equal(expectedDates.Count, actualDatesCount);
        }
        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);
        }
        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);
        }
        public void GetAllCarsShould_OrderByLastAdded()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_OrderAdded")
                          .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,
                    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 expectedResultIds = new List <int> {
                3, 2, 1
            };
            var actualResultIds = carsService.GetAllCars(OrderCarsByLastAdded).Select(p => p.Id);

            Assert.Equal(expectedResultIds, actualResultIds);
        }