public void A010_SaveNewTest() { carsRepository.SaveAndFlush(Car1); Car carCheck = carsRepository.GetById(Car1.PrimaryKey); Assert.IsNotNull(carCheck); Assert.AreEqual(Car1.PrimaryKey, carCheck.PrimaryKey); Assert.AreEqual(Car1.Name, carCheck.Name); Assert.AreEqual(Car1.ManufacturerKey, carCheck.ManufacturerKey); Assert.AreEqual(Car1.Year, carCheck.Year); Assert.AreEqual(Car1.Category, carCheck.Category); Assert.AreEqual(Car1.Price, carCheck.Price); Assert.AreEqual(Car1.DisplacementCC, carCheck.DisplacementCC); Assert.AreEqual(Car1.MaxPower, carCheck.MaxPower); Assert.AreEqual(Car1.PowerRPM, carCheck.PowerRPM); Assert.AreEqual(Car1.TorqueFTLB, carCheck.TorqueFTLB); Assert.AreEqual(Car1.TorqueRPM, carCheck.TorqueRPM); Assert.AreEqual(Car1.DriveTrain, carCheck.DriveTrain); Assert.AreEqual(Car1.Aspiration, carCheck.Aspiration); Assert.AreEqual(Car1.Length, carCheck.Length); Assert.AreEqual(Car1.Width, carCheck.Width); Assert.AreEqual(Car1.Height, carCheck.Height); Assert.AreEqual(Car1.Weight, carCheck.Weight); Assert.AreEqual(Car1.MaxSpeed, carCheck.MaxSpeed); Assert.AreEqual(Car1.Acceleration, carCheck.Acceleration); Assert.AreEqual(Car1.Braking, carCheck.Braking); Assert.AreEqual(Car1.Cornering, carCheck.Cornering); Assert.AreEqual(Car1.Stability, carCheck.Stability); }
/// <summary>Validations done to an entity before it is saved.</summary> /// <param name="validateEntity">The entity to validate.</param> /// <exception cref="GTSport_DT.OwnerCars.OwnerCarIDNotSetException"> /// Thrown when the car ID is not filled. /// </exception> /// <exception cref="GTSport_DT.OwnerCars.OwnerCarOwnerKeyNotSetException"> /// Thrown when the owner key is not filled. /// </exception> /// <exception cref="GTSport_DT.OwnerCars.OwnerCarCarKeyNotSetException"> /// Thrown when the car key is not filled. /// </exception> /// <exception cref="OwnerNotFoundException"> /// Thrown when the owner key can not be found in the owners table. /// </exception> /// <exception cref="CarNotFoundException"> /// Thrown when the car key can not be found in the cars table. /// </exception> public override void ValidateSave(OwnerCar validateEntity) { if (String.IsNullOrWhiteSpace(validateEntity.CarID)) { throw new OwnerCarIDNotSetException(OwnerCarIDNotSetException.OwnerCarIDNotSetMsg); } if (String.IsNullOrWhiteSpace(validateEntity.OwnerKey)) { throw new OwnerCarOwnerKeyNotSetException(OwnerCarOwnerKeyNotSetException.OwnerCarOwnerKeyNotSetMsg); } if (String.IsNullOrWhiteSpace(validateEntity.CarKey)) { throw new OwnerCarCarKeyNotSetException(OwnerCarCarKeyNotSetException.OwnerCarCarKeyNotSetMsg); } Owner owner = ownersRepository.GetById(validateEntity.OwnerKey); if (owner == null) { throw new OwnerNotFoundException(OwnerNotFoundException.OwnerKeyNotFoundMsg, validateEntity.OwnerKey); } Car car = carsRepository.GetById(validateEntity.CarKey); if (car == null) { throw new CarNotFoundException(CarNotFoundException.CarKeyNotFoundMsg, validateEntity.CarKey); } }
// Get CarDelete public ActionResult CarDelete(int id) { CarsRepository carRepository = new CarsRepository(); carRepository.DeleteById(id); return(View(carRepository.GetById(id))); }
public void CarsRepositoryGetById_ShouldProperlyReturnResult() { var numberOfCars = 5; var rng = new Random(); var collection = new List <ICar>(); for (int i = 0; i < numberOfCars; i++) { var car = Mock.Create <ICar>(); Mock.Arrange(() => car.Id).Returns(i); collection.Add(car); } var mockDb = Mock.Create <IDatabase>(); Mock.Arrange(() => mockDb.Cars).Returns(collection); var repo = new CarsRepository(mockDb); var expected = rng.Next(numberOfCars); Assert.AreEqual(expected, repo.GetById(expected).Id); }
internal Car GetById(int id) { Car car = _repo.GetById(id); if (car == null) { throw new Exception("Invalid Car Id"); } return(car); }
public void CarsRepositoryGetById_SchhouldThrowAnException_IfIdIsNotFound() { var mockDb = new Mock <IDatabase>(); mockDb.Setup(x => x.Cars).Returns(new List <Car>()); var repo = new CarsRepository(mockDb.Object); Assert.Throws <ArgumentException>(() => repo.GetById(8)); }
public Car GetById(int id) { Car foundCar = _repo.GetById(id); if (foundCar == null) { throw new Exception("Invalid car Id"); } return(foundCar); }
public void TestCarsRepository_GetByIdValidParameter_ShouldReturnCar() { var carRepo = new CarsRepository(); var car = new Car { Id = 1, Make = "Audi", Model = "A5", Year = 2005 }; carRepo.Add(car); Assert.AreEqual(car, carRepo.GetById(car.Id)); }
public async Task <IHttpActionResult> GetCar(string id) { Car car = await repo.GetById(id); if (car == null) { return(NotFound()); } return(Ok(Mapper.Map <CarDTO>(car))); }
// GET: CarDetail public ActionResult CarDetail(int id) { CarsRepository carsRepository = new CarsRepository(); var result = carsRepository.GetById(id); if (result != null) { return(View(result)); } else { return(View()); } }
public void GetById_ShouldThrowException_IfIdCannotBeFound() { var fakeDb = new Mock <IDatabase>(); fakeDb.SetupGet(cp => cp.Cars).Returns(new List <Car>() { }); var carRepo = new CarsRepository(fakeDb.Object); var ex = Assert.Throws <ArgumentException>(() => carRepo.GetById(10)); Assert.IsTrue(ex.Message.Contains("Car with such Id could not be found")); }
public async Task <ActionResult> EditCar(EditCarViewModel editCarViewModel) { if (ModelState.IsValid) { bool changed = false; CarsRepository <Car, CarDTO> carsRepo = new CarsRepository <Car, CarDTO>(); Car car = await carsRepo.GetById(editCarViewModel.SelectedCarToEditId); // Add image if selected if (Request.Files.Count > 0) { var postedFile = Request.Files[0]; if (postedFile != null && postedFile.ContentLength > 0) { string carsImagesPath = HttpContext.Server.MapPath("~/Content/Images/Cars"); string extension = Path.GetExtension(postedFile.FileName); string carFileName = $"{editCarViewModel.SelectedCarToEditId}{extension}"; string saveToPath = Path.Combine(carsImagesPath, carFileName); try { postedFile.SaveAs(saveToPath); car.ImageName = carFileName; changed = true; } catch (Exception) { ModelState.AddModelError(string.Empty, "Failed to save new car image."); } } } if (editCarViewModel.Name != car.Name) { car.Name = editCarViewModel.Name; changed = true; } if (changed) { await carsRepo.Update(car.Id, car); TempData["CarId"] = car.Id; } } return(RedirectToAction("Index")); }
public ActionResult Edit(int?id) { CarsRepository repo = new CarsRepository(); Car item = id == null ? new Car() : repo.GetById(id.Value); EditVM model = new EditVM(); model.Id = item.Id; model.Make = item.Make; model.Marka = item.Model; model.Year = item.Year; model.Price = item.Price; model.Color = item.Color; return(View(model)); }
public void GetById_ShouldThrowException_IfThereIsNoCarWithThatId() { // Arrange var mock = new Mock <IDatabase>(); mock.Setup(m => m.Cars).Returns(new List <Car>() { });; var fakeDB = mock.Object; var carsRepository = new CarsRepository(fakeDB); // Act and Assert var ex = Assert.Throws <ArgumentException>(() => carsRepository.GetById(7)); Assert.IsTrue(ex.Message.Contains("Car with such Id could not be found")); }
public void CarsRepositoryGetById_ShouldReturnCarProperly(int needed) { int magicNumberThatIncludeValuesAbove = 10; var collection = new List <Car>(); for (int i = 0; i < magicNumberThatIncludeValuesAbove; i++) { collection.Add(new CarChild(id: i)); } var mockDb = new Mock <IDatabase>(); mockDb.Setup(x => x.Cars).Returns(collection); var repo = new CarsRepository(mockDb.Object); Assert.AreEqual(needed, repo.GetById(needed).Id); }
public void CarsRepositoryGetById_ShouldThrowArgumentException_IfCarWithThatIdDoNotExsist() { var numberOfCars = 5; var collection = new List <ICar>(); for (int i = 0; i < numberOfCars; i++) { var mockCar = Mock.Create <ICar>(); Mock.Arrange(() => mockCar.Id).Returns(i); collection.Add(mockCar); } var mockDb = Mock.Create <IDatabase>(); Mock.Arrange(() => mockDb.Cars).Returns(collection); var repo = new CarsRepository(mockDb); Assert.ThrowsException <ArgumentException>(() => repo.GetById(numberOfCars + 5)); }
private static void Module1IOC() { var builder = new ContainerBuilder(); builder.RegisterType <ProjectContext>() .As <DbContext>(); builder.RegisterGeneric(typeof(BaseRepository <>)) .As(typeof(IRepository <>)) .InstancePerLifetimeScope(); using (var container = builder.Build()) { //ICarsRepository<Cars> carsRepo = container.Resolve<ICarsRepository<Cars>>(); var carsRepo = new CarsRepository(new ProjectContext()); var cars = carsRepo.All().ToList(); var car = carsRepo.GetById(1001); //一直只能点出ToListAsync方法是因为没有引入System.Linq //ToListAsync是EF中的扩展方法 //var cars = carsRepo.All().ToListAsync(); } }
public void GetById_ShouldFindCarById() { // Arrange var car = new Car { Id = 1, Make = "Audi", Model = "A5", Year = 2005 }; var mock = new Mock <IDatabase>(); mock.Setup(m => m.Cars).Returns(new List <Car>() { }); var fakeDB = mock.Object; fakeDB.Cars.Add(car); var carsRepository = new CarsRepository(fakeDB); // Act var result = carsRepository.GetById(1); // Assert mock.Verify(m => m.Cars, Times.Exactly(2)); }
public async Task <HttpResponseMessage> PostLapTime(LapTimeDTO[] lapTimeDTOs) { if (!ModelState.IsValid) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } LapTimeDTO first = lapTimeDTOs.FirstOrDefault(); if (first != null) { DriverResultsRepository <DriverResult> driverResultsRepo = new DriverResultsRepository <DriverResult>(); CarsRepository <Car, CarDTO> carsRepo = new CarsRepository <Car, CarDTO>(); TracksRepository <Track> tracksRepo = new TracksRepository <Track>(); BestLapTimesRepository <BestLapTime> bestLapsRepo = new BestLapTimesRepository <BestLapTime>(); DriverResult driverResult = await driverResultsRepo.GetById(first.DriverResultId); BestLapTime usersBestLapInCar = bestLapsRepo.GetForUserId(driverResult.ApplicationUserId).Where(bl => bl.CarId == driverResult.CarId).FirstOrDefault(); TimeSpan fastestLap = driverResult.BestLapTime; BestLapTime bestLapTime = new BestLapTime(); foreach (LapTimeDTO lapTimeDTO in lapTimeDTOs) { LapTime lapTime = new LapTime() { Id = Guid.NewGuid().ToString(), DriverResultId = lapTimeDTO.DriverResultId, LapNumber = lapTimeDTO.LapNumber, Time = lapTimeDTO.Time }; lapTime = await repo.Insert(lapTime); if (lapTime.Time == fastestLap) { bestLapTime = new BestLapTime() { Id = Guid.NewGuid().ToString(), ApplicationUserId = driverResult.ApplicationUserId, CarId = driverResult.CarId, LapTimeId = lapTime.Id, }; if (usersBestLapInCar == null) { bestLapTime = await bestLapsRepo.Insert(bestLapTime); } else if (fastestLap < usersBestLapInCar.LapTime.Time) { EntityState response = await bestLapsRepo.Update(usersBestLapInCar.Id, bestLapTime); } Car car = await carsRepo.GetById(driverResult.CarId); var carsBestLapTime = car?.BestLapTime?.LapTime?.Time; if (car?.BestLapTimeId == null || fastestLap < carsBestLapTime) { car.BestLapTimeId = bestLapTime.Id; await carsRepo.Update(car.Id, car); } Track track = await tracksRepo.GetById(car?.TrackId); var trackBestLap = track?.BestLapTime?.LapTime?.Time; if (track?.BestLapTimeId == null || fastestLap < trackBestLap) { track.BestLapTimeId = bestLapTime.Id; await tracksRepo.Update(track.Id, track); } } } } return(new HttpResponseMessage(HttpStatusCode.OK)); }
public void TestCarsRepository_GetByIdInvalidParameter_ShouldThrowArgumentException() { var carRepo = new CarsRepository(); Assert.Throws <ArgumentException>(() => carRepo.GetById(5)); }