public Car AddCar(Car car) { car.User = _applicationUser; _dbContext.Cars.Add(car); _dbContext.SaveChanges(); return car; }
public Car UpdateCar(Car car) { var existingCar = _dbContext.Cars.SingleOrDefault(t => t.User.Id == _applicationUser.Id && t.Id == car.Id); if (existingCar == null) { throw new ObjectNotFoundException(); } existingCar.Make = car.Make; existingCar.Model = car.Model; existingCar.NumberPlate = car.NumberPlate; existingCar.Remarks = car.Remarks; _dbContext.SaveChanges(); return existingCar; }
public void AddCar_CanAdd() { // Arrange var car = new Car { Id = 1 }; var mockCarService = new Mock<ICarService>(); mockCarService.Setup(x => x.AddCar(car)) .Returns(car); var carController = new CarsController(mockCarService.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; // Act var response = carController.AddCar(car); // Assert Car addedCar; Assert.IsTrue(response.TryGetContentValue(out addedCar)); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); }
public static Car ResolveCar(this Trip trip, IApplicationDbContext dbContext, string userId, Car car) { if (car != null && car.Id > 0) { return dbContext.Cars.SingleOrDefault(c => c.User.Id == userId && c.Id == car.Id); } return null; }
public void UpdateCar_CanUpdate() { // Arrange var car = new Car { Id = 1, User = _currentUser, Make = "DCM-12", Model = "DeLorean", NumberPlate = "OUTOFTIME" }; _fakeApplicationDbContext.Cars.Add(car); var carService = new CarService(_fakeApplicationDbContext, _mockCurrentUserService.Object); var updatedCar = new Car { Id = 1, Make = "DeLorean", Model = "DMC-12", NumberPlate = "OUTATIME" }; // Act car = carService.UpdateCar(updatedCar); // Assert Assert.IsNotNull(updatedCar); Assert.AreEqual(car.Make, "DeLorean"); Assert.AreEqual(car.Model, "DMC-12"); Assert.AreEqual(car.NumberPlate, "OUTATIME"); }
public void DeleteCar_CantDeleteIfInUse() { Exception caugthException = null; var car = new Car { Id = 4, User = _currentUser }; _fakeApplicationDbContext.Cars.Add(car); _fakeApplicationDbContext.Trips.Add(new Trip { Id = 1, User = _currentUser, Car = car }); var carService = new CarService(_fakeApplicationDbContext, _mockCurrentUserService.Object); // Act bool deleted = false; try { deleted = carService.DeleteCar(car.Id); } catch (DbEntityValidationException ex) { caugthException = ex; } // Assert Assert.IsFalse(deleted); Assert.IsNotNull(caugthException); }
public void AddCar_ShouldThrowValidationError() { // Arrange Exception caugthException = null; var car = new Car { Make = "DeLorean", Model = "DMC-12", //NumberPlate = "OUTATIME" }; var carService = new CarService(_fakeApplicationDbContext, _mockCurrentUserService.Object); // Act try { car = carService.AddCar(car); } catch (DbEntityValidationException ex) { caugthException = ex; } // Assert Assert.IsNull(car); Assert.IsNotNull(caugthException); }
public void AddCar_CanAdd() { // Arrange var car = new Car { Make = "DeLorean", Model = "DMC-12", NumberPlate = "OUTATIME" }; var carService = new CarService(_fakeApplicationDbContext, _mockCurrentUserService.Object); // Act car = carService.AddCar(car); // Assert Assert.IsNotNull(car); Assert.AreEqual(car.Make, "DeLorean"); Assert.AreEqual(car.Model, "DMC-12"); Assert.AreEqual(car.NumberPlate, "OUTATIME"); }
public void UpdateTrip_CanUpdate() { // Arrange var addressOrigin = new Address { Id = 1, User = _currentUser }; var addressDestination = new Address { Id = 2, User = _currentUser }; var car = new Car { Id = 1, User = _currentUser }; var otherCar = new Car { Id = 2, User = _currentUser }; var otherAddress = new Address { Id = 3, User = _currentUser }; var trip = new Trip { Id = 1, User = _currentUser, Date = DateTime.SpecifyKind(new DateTime(2014, 08, 01), DateTimeKind.Utc), AddressOrigin = addressOrigin, AddressDestination = addressDestination, Car = car }; _fakeApplicationDbContext.Addresses.Add(addressOrigin); _fakeApplicationDbContext.Addresses.Add(addressDestination); _fakeApplicationDbContext.Addresses.Add(otherAddress); _fakeApplicationDbContext.Cars.Add(car); _fakeApplicationDbContext.Cars.Add(otherCar); _fakeApplicationDbContext.Trips.Add(trip); var tripService = new TripService(_fakeApplicationDbContext, _mockCurrentUserService.Object, _mockIDistanceCalculatorService.Object); var updatedTrip = new Trip { Id = 1, User = _currentUser, Date = DateTime.SpecifyKind(new DateTime(2014, 08, 02), DateTimeKind.Utc), AddressOrigin = addressOrigin, AddressDestination = otherAddress, Car = otherCar, Remarks = "" }; // Act updatedTrip = tripService.UpdateTrip(updatedTrip); // Assert Assert.IsNotNull(updatedTrip); Assert.IsNotNull(updatedTrip.AddressOrigin); Assert.IsNotNull(updatedTrip.AddressDestination); Assert.IsNotNull(updatedTrip.Car); Assert.AreEqual(new DateTime(2014, 08, 02), updatedTrip.Date); Assert.AreEqual(addressOrigin.Id, updatedTrip.AddressOrigin.Id); Assert.AreEqual(otherAddress.Id, updatedTrip.AddressDestination.Id); Assert.AreEqual(otherCar.Id, updatedTrip.Car.Id); Assert.AreEqual(updatedTrip.DistanceInKm, 42); Assert.AreEqual(updatedTrip.Id, 1); }
public void AddTrip_CanAdd() { // Arrange var addressOrigin = new Address { Id = 1, User = _currentUser}; var addressDestination = new Address { Id = 2, User = _currentUser }; var car = new Car { Id = 1, User = _currentUser }; _fakeApplicationDbContext.Addresses.Add(addressOrigin); _fakeApplicationDbContext.Addresses.Add(addressDestination); _fakeApplicationDbContext.Cars.Add(car); var trip = new Trip { User = _currentUser, Date = new DateTime(2014, 08, 01), AddressOrigin = addressOrigin, AddressDestination = addressDestination, Car = car }; var tripService = new TripService(_fakeApplicationDbContext, _mockCurrentUserService.Object, _mockIDistanceCalculatorService.Object); // Act trip = tripService.AddTrip(trip); // Assert Assert.IsNotNull(trip); Assert.IsNotNull(trip.AddressOrigin); Assert.IsNotNull(trip.AddressDestination); Assert.IsNotNull(trip.Car); Assert.AreEqual(addressOrigin.Id, trip.AddressOrigin.Id); Assert.AreEqual(addressDestination.Id, trip.AddressDestination.Id); Assert.AreEqual(car.Id, trip.Car.Id); Assert.AreEqual(trip.DistanceInKm, 42); }
public void GetNewTripTemplate_WithLatestUsedCarAndAddress() { // Arrange var latestUsedAddress = new Address { Id = 1 }; var otherAddress = new Address { Id = 2 }; var latestUsedCar = new Car { Id = 1 }; var otherCar = new Car { Id = 1 }; _fakeApplicationDbContext.Trips.Add(new Trip { Id = 1, User = _currentUser, Date = new DateTime(2014, 08, 01), AddressDestination = otherAddress, Car = otherCar }); _fakeApplicationDbContext.Trips.Add(new Trip { Id = 2, User = _currentUser, Date = new DateTime(2014, 07, 01), AddressDestination = otherAddress, Car = otherCar }); _fakeApplicationDbContext.Trips.Add(new Trip { Id = 3, User = _currentUser, Date = new DateTime(2014, 10, 01), AddressDestination = latestUsedAddress, Car = latestUsedCar }); _fakeApplicationDbContext.Trips.Add(new Trip { Id = 4, User = _otherUser, Date = new DateTime(2014, 11, 01), AddressDestination = otherAddress, Car = otherCar }); _fakeApplicationDbContext.Trips.Add(new Trip { Id = 5, User = _currentUser, Date = new DateTime(2014, 05, 01), AddressDestination = otherAddress, Car = otherCar }); var tripService = new TripService(_fakeApplicationDbContext, _mockCurrentUserService.Object, _mockIDistanceCalculatorService.Object); // Act var trip = tripService.GetNewTripTemplate(); // Assert Assert.IsNotNull(trip); Assert.IsNotNull(trip.AddressOrigin); Assert.IsNotNull(trip.Car); Assert.AreEqual(latestUsedAddress.Id, trip.AddressOrigin.Id); Assert.AreEqual(latestUsedCar.Id, trip.Car.Id); }