/// <summary> /// Creates the fillups. /// </summary> /// <param name="odometer">The initial odometer reading</param> /// <param name="date">The first date to create a fill up for</param> /// <param name="vehicle">The vehicle object to create the fill ups for</param> /// <param name="unitsModifier">The units modifier is applied to the total gallons calculation. /// By supplying a different value for each vehicle, the data will be different for each vehicle. /// </param> /// <param name="distanceModifier">The distance modifier is applied to the distance calculation. /// By supplying a different value for each vehicle, the data will be different for each vehicle. /// </param> /// <remarks> /// Creates random fill up sample data for the vehicle. /// Consumes the data arrays at the top of this class. /// Randomizes the index used to access data arrays by creating an array then randomly sorting the array elements. /// The "while" loop runs while calculated date is less than the current date. /// The date is recalculated each cycle of the while loop, adding a random number of days between 3 and 18 days to the previous value. /// </remarks> void CreateFillups(Int32 odometer, DateTime date, Vehicle vehicle, Double unitsModifier, Double distanceModifier) { var randomArray = RandomizeArray( new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }); var currentIndex = 0; var random = new Random(); var isFirst = true; while(date < DateTime.Now) { var dataIndex = randomArray[currentIndex]; var distance = (Int32)(_distance[dataIndex] * distanceModifier); var fillup = new FillupEntry(); fillup.Date = date; if(isFirst) { isFirst = false; fillup.Distance = null; } else { fillup.Distance = distance; } fillup.Odometer = odometer; fillup.PricePerUnit = _price[dataIndex]; fillup.TotalUnits = _units[dataIndex] * unitsModifier; fillup.TransactionFee = _fee[dataIndex]; fillup.VehicleId = vehicle.VehicleId; fillup.Vendor = _vendor[dataIndex]; odometer += distance; vehicle.Fillups.Add(fillup); currentIndex += 1; if(currentIndex > 11) { currentIndex = 0; } date = date.AddDays(random.Next(3, 14)); } SaveChanges(); }
public void WhenGettingOtherUsersVehicle_ThenThrowsInvalidOperationException() { int userId = this.defaultTestUser.UserId; var repository = new VehicleRepository(new MileageStatsDbContext()); var vehicle = new Vehicle {Name = "Vehicle"}; repository.Create(userId, vehicle); var repositoryForVerify = new VehicleRepository(new MileageStatsDbContext()); Assert.Throws<InvalidOperationException>(() => repositoryForVerify.GetVehicle(42, vehicle.VehicleId)); }
public void WhenVehicleAdded_ThenUpdatesRepository() { var repository = new VehicleRepository(new MileageStatsDbContext()); var vehicle = new Vehicle {Name = "Vehicle"}; repository.Create(this.defaultTestUser.UserId, vehicle); List<Vehicle> actualList = new MileageStatsDbContext().Vehicles.ToList(); Assert.Equal(1, actualList.Count); Assert.Equal(vehicle.Name, actualList[0].Name); }
public void ForOtherUser_ThenNullReturned() { const int notTheCurrentUserId = UserId + 1; var vehicle = new Vehicle { VehicleId = DefaultVehicleId, Name = "vehicle" }; _vehicleRepo .Setup(vr => vr.GetVehicle(notTheCurrentUserId, DefaultVehicleId)) .Throws(new InvalidOperationException()); var handler = new GetVehicleById(_vehicleRepo.Object); var retrievedVehicle = handler.Execute(notTheCurrentUserId, vehicle.VehicleId); Assert.Null(retrievedVehicle); }
public void ThenVehicleReturned() { var vehicle = new Vehicle { VehicleId = 1, Name = "vehicle" }; _vehicleRepo .Setup(vr => vr.GetVehicle(UserId, vehicle.VehicleId)) .Returns(vehicle); var handler = new GetVehicleById(_vehicleRepo.Object); var retrievedVehicle = handler.Execute(UserId, vehicle.VehicleId); Assert.NotNull(retrievedVehicle); Assert.Equal("vehicle", retrievedVehicle.Name); }
public void ThenVehiclePhotoIdSet() { const int photoId = 99; var vehicle = new Vehicle { VehicleId = DefaultVehicleId, Name = "vehicle", PhotoId = photoId }; _vehicleRepo .Setup(vr => vr.GetVehicle(UserId, DefaultVehicleId)) .Returns(vehicle); var handler = new GetVehicleById(_vehicleRepo.Object); var retrievedVehicle = handler.Execute(UserId, vehicle.VehicleId); Assert.Equal(photoId, retrievedVehicle.PhotoId); }
public void ThenDelegatesToVehicleRepositoryForEachVehicle() { var vehicleOne = new Vehicle {VehicleId = 1, Name = "oldName", SortOrder = 0}; var vehicleTwo = new Vehicle {VehicleId = 2, Name = "oldName", SortOrder = 1}; var vehicleThree = new Vehicle {VehicleId = 3, Name = "oldName", SortOrder = 2}; _vehicleRepo.Setup(r => r.GetVehicle(It.IsAny<int>(), 1)).Returns(vehicleOne); _vehicleRepo.Setup(r => r.GetVehicle(It.IsAny<int>(), 2)).Returns(vehicleTwo); _vehicleRepo.Setup(r => r.GetVehicle(It.IsAny<int>(), 3)).Returns(vehicleThree); var newOrder = new[] {3, 2, 1}; var handler = new UpdateVehicleSortOrder(_vehicleRepo.Object); handler.Execute(UserId, newOrder); _vehicleRepo.Verify(r => r.Update(vehicleOne), Times.Once()); _vehicleRepo.Verify(r => r.Update(vehicleTwo), Times.Once()); _vehicleRepo.Verify(r => r.Update(vehicleThree), Times.Once()); }
public void Update(Vehicle updatedVehicle) { Vehicle vehicleToUpdate = this.GetDbSet<Vehicle>() .Where(v => v.VehicleId == updatedVehicle.VehicleId) .First(); vehicleToUpdate.Name = updatedVehicle.Name; vehicleToUpdate.Year = updatedVehicle.Year; vehicleToUpdate.MakeName = updatedVehicle.MakeName; vehicleToUpdate.ModelName = updatedVehicle.ModelName; vehicleToUpdate.SortOrder = updatedVehicle.SortOrder; vehicleToUpdate.PhotoId = updatedVehicle.PhotoId; this.SetEntityState(vehicleToUpdate, vehicleToUpdate.VehicleId == 0 ? EntityState.Added : EntityState.Modified); this.UnitOfWork.SaveChanges(); }
private void InitializeFixture() { DatabaseTestUtility.DropCreateMileageStatsDatabase(); this.defaultTestUser = new User() { AuthorizationId = "TestAuthorizationId", DisplayName = "DefaultTestUser" }; var userRepository = new UserRepository(this.GetUnitOfWork()); userRepository.Create(this.defaultTestUser); int userId = this.defaultTestUser.UserId; var vehicleRepository = new VehicleRepository(this.GetUnitOfWork()); this.defaultVehicle = new Vehicle() { Name = "Test Vehicle" }; vehicleRepository.Create(this.defaultTestUser.UserId, this.defaultVehicle); }
public void WhenGetFleetSummaryStatistics_CalculatesAcrossVehicles() { var fillups1 = new[] { new FillupEntry {Date = DateTime.UtcNow.AddDays(-1), PricePerUnit = 1.0, TotalUnits = 10}, new FillupEntry {Date = DateTime.UtcNow, PricePerUnit = 1.0, TotalUnits = 10} }; var fillups2 = new[] { new FillupEntry {Date = DateTime.UtcNow.AddDays(-1), PricePerUnit = 1.0, TotalUnits = 10}, new FillupEntry {Date = DateTime.UtcNow, PricePerUnit = 1.0, TotalUnits = 10}, new FillupEntry { Date = DateTime.UtcNow, PricePerUnit = 1.0, TotalUnits = 10, TransactionFee = 5.0 } }; var vehicle1 = new Vehicle { VehicleId = 1, Fillups = fillups1 }; var vehicle2 = new Vehicle { VehicleId = 2, Fillups = fillups2 }; var mockVehicleRepository = new Mock<IVehicleRepository>(); mockVehicleRepository .Setup(x => x.GetVehicles(DefaultUserId)) .Returns(new[] { vehicle1, vehicle2 }); var handler = new GetFleetSummaryStatistics(mockVehicleRepository.Object); var actual = handler.Execute(DefaultUserId); Assert.NotNull(actual); Assert.Equal(30, actual.TotalFuelCost); Assert.Equal(35, actual.TotalCost); }
public void WhenVehicleAdded_ThenPersists() { int userId = this.defaultTestUser.UserId; var repository = new VehicleRepository(new MileageStatsDbContext()); Vehicle vehicle1 = new Vehicle {Name = "Vehicle1"}; Vehicle vehicle2 = new Vehicle {Name = "Vehicle2"}; repository.Create(userId, vehicle1); repository.Create(userId, vehicle2); // I use a new context and repository to verify the data was stored var repository2 = new VehicleRepository(new MileageStatsDbContext()); var retrievedVehicles = repository2.GetVehicles(userId); Assert.NotNull(retrievedVehicles); List<Vehicle> actualList = new List<Vehicle>(retrievedVehicles); Assert.Equal(2, actualList.Count); Assert.Equal(vehicle1.Name, actualList[0].Name); Assert.Equal(vehicle2.Name, actualList[1].Name); }
public void WhenVehicleDeleted_ThenPersists() { int userId = this.defaultTestUser.UserId; var repository = new VehicleRepository(new MileageStatsDbContext()); Vehicle vehicle1 = new Vehicle {Name = "Vehicle1"}; repository.Create(userId, vehicle1); var retrievedVehicles = repository.GetVehicles(userId); Assert.Equal(1, retrievedVehicles.Count()); repository.Delete(retrievedVehicles.First().VehicleId); // I use a new context and repository to verify the vehicle was deleted var repository2 = new VehicleRepository(new MileageStatsDbContext()); var verifyVehicles = repository2.GetVehicles(userId); Assert.NotNull(retrievedVehicles); Assert.Equal(0, verifyVehicles.Count()); }
public void WhenGettingVehicle_ThenVehicleStatisticsReturned() { var fillups = new List<FillupEntry> { new FillupEntry { Date = DateTime.UtcNow.AddDays(-10), Odometer = 500, PricePerUnit = 10.0, TotalUnits = 10.0 }, new FillupEntry { Date = DateTime.UtcNow.AddDays(-5), Odometer = 1000, PricePerUnit = 10.0, TotalUnits = 10.0 } }; var vehicle = new Vehicle { VehicleId = DefaultVehicleId, Fillups = fillups }; _vehicleRepo .Setup(vr => vr.GetVehicle(UserId, DefaultVehicleId)) .Returns(vehicle); var handler = new GetVehicleById(_vehicleRepo.Object); var retrievedVehicle = handler.Execute(UserId, vehicle.VehicleId); Assert.NotNull(retrievedVehicle); Assert.Equal(1000, retrievedVehicle.Odometer); }
private void SeedVehicles(Int32 userId) { var vehicle = new Vehicle {UserId = userId, Name = "Hot Rod", SortOrder = 1, Year = 2003, MakeName = "BMW", ModelName = "330xi"}; Vehicles.Add(vehicle); SaveChanges(); vehicle.Photo = CreateVehiclePhoto(Resources.bmw, vehicle.VehicleId); vehicle.PhotoId = vehicle.Photo.VehiclePhotoId; SaveChanges(); CreateFillups(1000, DateTime.Now.AddDays(-365), vehicle, 1, 1); CreateReminders(vehicle); vehicle = new Vehicle {UserId = userId, Name = "Soccer Mom's Ride", SortOrder = 2, Year = 1997, MakeName = "Honda", ModelName = "Accord LX"}; Vehicles.Add(vehicle); SaveChanges(); vehicle.Photo = CreateVehiclePhoto(Resources.soccermomcar, vehicle.VehicleId); vehicle.PhotoId = vehicle.Photo.VehiclePhotoId; SaveChanges(); CreateFillups(500, DateTime.Now.AddDays(-370), vehicle, .9, 1.2); CreateReminders(vehicle); vehicle = new Vehicle {UserId = userId, Name = "Mud Lover", SortOrder = 3, Year = 2011, MakeName = "Jeep", ModelName = "Wrangler"}; Vehicles.Add(vehicle); SaveChanges(); vehicle.Photo = CreateVehiclePhoto(Resources.jeep, vehicle.VehicleId); vehicle.PhotoId = vehicle.Photo.VehiclePhotoId; SaveChanges(); CreateFillups(750, DateTime.Now.AddDays(-373), vehicle, 1.2, .8); CreateReminders(vehicle); }
public void Create(int userId, Vehicle vehicle) { vehicle.UserId = userId; this.GetDbSet<Vehicle>().Add(vehicle); this.UnitOfWork.SaveChanges(); }
public void ThenUpdatesSortOrderForEachVehicle() { var vehicleOne = new Vehicle {VehicleId = 1, Name = "oldName", SortOrder = 0}; var vehicleTwo = new Vehicle {VehicleId = 2, Name = "oldName", SortOrder = 1}; var vehicleThree = new Vehicle {VehicleId = 3, Name = "oldName", SortOrder = 2}; _vehicleRepo.Setup(vr => vr.GetVehicle(It.IsAny<int>(), 1)).Returns(vehicleOne); _vehicleRepo.Setup(vr => vr.GetVehicle(It.IsAny<int>(), 2)).Returns(vehicleTwo); _vehicleRepo.Setup(vr => vr.GetVehicle(It.IsAny<int>(), 3)).Returns(vehicleThree); var newOrder = new[] {3, 2, 1}; var handler = new UpdateVehicleSortOrder(_vehicleRepo.Object); handler.Execute(UserId, newOrder); Assert.Equal(2, vehicleOne.SortOrder); Assert.Equal(1, vehicleTwo.SortOrder); Assert.Equal(0, vehicleThree.SortOrder); }
public void ByUserIdForUser_ThenReturnsVehiclesInSortedOrder() { var vehicle01 = new Vehicle { Name = "first", VehicleId = 4, SortOrder = 1 }; var vehicle02 = new Vehicle { Name = "second", VehicleId = 1, SortOrder = 3 }; var vehicle03 = new Vehicle { Name = "third", VehicleId = 2, SortOrder = 2 }; var vehicles = new List<Vehicle> { vehicle03, vehicle02, vehicle01 }; _vehicleRepo .Setup(vr => vr.GetVehicles(UserId)) .Returns(vehicles); var handler = new GetVehicleListForUser(_vehicleRepo.Object); var result = handler.Execute(UserId); var acutal = result.ToArray(); Assert.Equal(vehicle01.Name, acutal[0].Name); Assert.Equal(vehicle03.Name, acutal[1].Name); Assert.Equal(vehicle02.Name, acutal[2].Name); }
public void WhenVehicleAdded_ThenUpdatesVehicleId() { int userId = this.defaultTestUser.UserId; var repository = new VehicleRepository(new MileageStatsDbContext()); Vehicle vehicle = new Vehicle {Name = "Vehicle"}; repository.Create(userId, vehicle); IEnumerable<Vehicle> retrievedVehicles = repository.GetVehicles(userId); Assert.NotNull(retrievedVehicles); List<Vehicle> actualList = new List<Vehicle>(retrievedVehicles); Assert.Equal(1, actualList.Count); Assert.Equal(1, actualList[0].VehicleId); }
private void CreateReminders(Vehicle vehicle) { FillupEntry lastFillup = vehicle.Fillups.OrderByDescending(f => f.Date).FirstOrDefault(); if (lastFillup == null) { return; } Reminder reminder; // create overdue by mileage reminder reminder = new Reminder { DueDate = null, DueDistance = lastFillup.Odometer - 10, IsFulfilled = false, Remarks = "Check air filter when oil is changed", Title = "Oil Change", VehicleId = vehicle.VehicleId }; vehicle.Reminders.Add(reminder); // create overdue by date reminder reminder = new Reminder { DueDate = lastFillup.Date.AddDays(-10), DueDistance = null, IsFulfilled = false, Remarks = "Check condition of the wipers", Title = "Check Wiper Fluid", VehicleId = vehicle.VehicleId }; vehicle.Reminders.Add(reminder); // create to be done soon by mileage reminder reminder = new Reminder {DueDate = null, DueDistance = lastFillup.Odometer + 400, IsFulfilled = false, Remarks = "Check air pressure", Title = "Rotate Tires", VehicleId = vehicle.VehicleId}; vehicle.Reminders.Add(reminder); // create to be done soon by date reminder reminder = new Reminder {DueDate = DateTime.Now.AddDays(+10), DueDistance = null, IsFulfilled = false, Remarks = "Check air freshener", Title = "Vacuum Car", VehicleId = vehicle.VehicleId}; vehicle.Reminders.Add(reminder); }
public void WhenVehicleModifiedInSameContext_ThenPersistsChange() { IUnitOfWork uow = new MileageStatsDbContext(); int userId = this.defaultTestUser.UserId; var repository = new VehicleRepository(uow); Vehicle vehicle = new Vehicle {Name = "Vehicle", UserId = userId}; repository.Create(userId, vehicle); // I use a new context and repository to verify the data was stored var repositoryForUpdate = new VehicleRepository(uow); var retrievedVehicle = repositoryForUpdate.GetVehicles(userId).First(); retrievedVehicle.Name = "Updated Vehicle Name"; repositoryForUpdate.Update(retrievedVehicle); int updatedVehicleId = retrievedVehicle.VehicleId; var repositoryForVerify = new VehicleRepository(uow); var updatedVehicle = repositoryForVerify.GetVehicle(userId, updatedVehicleId); Assert.Equal("Updated Vehicle Name", updatedVehicle.Name); }