public void GetAllTransportWorkTicketsForPeriodAsQueryable() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <TransportWorkTicket>(new ApplicationDbContext(options.Options)); var workTicketOne = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200100, TravelledDistance = 441, EndKilometers = 200541, FuelAvailability = 55, FuelConsumption = 441 * (5 / 100), Residue = 55 - (441 * (5 / 100)) }; var workTicketTwo = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200541, TravelledDistance = 100, EndKilometers = 200641, FuelAvailability = 55, FuelConsumption = 100 * (5 / 100), Residue = 55 - (100 * (5 / 100)) }; repository.AddAsync(workTicketOne); repository.AddAsync(workTicketTwo); repository.SaveChangesAsync(); var transportWorkTicketsService = new TransportWorkTicketsService(repository); AutoMapperConfig.RegisterMappings(typeof(TransportWorkTicketViewModel).Assembly); var workTickets = transportWorkTicketsService.GetAllTransportWorkTicketsForPeriod <TransportWorkTicketViewModel>(workTicketTwo.CarId, DateTime.UtcNow.AddDays(-10), DateTime.UtcNow); Assert.Equal(2, workTickets.Count()); }
public async Task DeleteAsyncReturnsFalseIfTransportWorkTicketDoesNotExist() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <TransportWorkTicket>(new ApplicationDbContext(options.Options)); var workTicketOne = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200100, TravelledDistance = 441, EndKilometers = 200541, FuelAvailability = 55, FuelConsumption = 441 * (5 / 100), Residue = 55 - (441 * (5 / 100)) }; var workTicketTwo = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200541, TravelledDistance = 100, EndKilometers = 200641, FuelAvailability = 55, FuelConsumption = 100 * (5 / 100), Residue = 55 - (100 * (5 / 100)) }; await repository.AddAsync(workTicketOne); await repository.AddAsync(workTicketTwo); await repository.SaveChangesAsync(); var transportWorkTicketsService = new TransportWorkTicketsService(repository); var isDeleted = await transportWorkTicketsService.DeleteAsync("4738-djsk-r4-3456", "Силвия Петрова"); var carsCount = repository.AllAsNoTracking().Count(); Assert.Equal(2, carsCount); Assert.False(isDeleted); }
public async Task EditAsyncUpdatesTransportWorkTicketInfo() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <TransportWorkTicket>(new ApplicationDbContext(options.Options)); var workTicketOne = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200100, TravelledDistance = 441, EndKilometers = 200541, FuelAvailability = 55, FuelConsumption = 441 * (5 / 100), Residue = 55 - (441 * (5 / 100)) }; var workTicketTwo = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200541, TravelledDistance = 100, EndKilometers = 200641, FuelAvailability = 55, FuelConsumption = 100 * (5 / 100), Residue = 55 - (100 * (5 / 100)) }; await repository.AddAsync(workTicketOne); await repository.AddAsync(workTicketTwo); await repository.SaveChangesAsync(); var transportWorkTicketsService = new TransportWorkTicketsService(repository); workTicketTwo.StartKilometers = 200700; workTicketTwo.TravelledDistance = 200; await transportWorkTicketsService.EditAsync(workTicketTwo); var workTicket = repository.AllAsNoTracking().FirstOrDefault(tr => tr.Id == workTicketTwo.Id); Assert.Equal(workTicketTwo.Date, workTicket.Date); Assert.Equal(workTicketTwo.StartKilometers, workTicket.StartKilometers); Assert.Equal(workTicketTwo.TravelledDistance, workTicket.TravelledDistance); }
public async Task SetIsDeletedAsyncChangesUsersForCars() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <RouteTransportWorkTicket>(new ApplicationDbContext(options.Options)); var workTicketOne = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200100, TravelledDistance = 441, EndKilometers = 200541, FuelAvailability = 55, FuelConsumption = 441 * (5 / 100), Residue = 55 - (441 * (5 / 100)) }; workTicketOne.RouteTransportWorkTickets.Add(new RouteTransportWorkTicket { TransportWorkTicketId = workTicketOne.Id, RouteId = "242hds-78dsd-7823dsds", IsDeleted = false }); var workTicketTwo = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200541, TravelledDistance = 100, EndKilometers = 200641, FuelAvailability = 55, FuelConsumption = 100 * (5 / 100), Residue = 55 - (100 * (5 / 100)) }; workTicketTwo.RouteTransportWorkTickets.Add(new RouteTransportWorkTicket { TransportWorkTicketId = workTicketTwo.Id, RouteId = "242hds-78dhgf-7823dsds", IsDeleted = false }); workTicketTwo.RouteTransportWorkTickets.Add(new RouteTransportWorkTicket { TransportWorkTicketId = workTicketTwo.Id, RouteId = "242tre-78dh00-7823dsds", IsDeleted = false }); var workTicketThree = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "3414141890", CarId = "72804eu-jhkhfvs-dasfa", StartKilometers = 200800, TravelledDistance = 100, EndKilometers = 200900, FuelAvailability = 55, FuelConsumption = 100 * (5 / 100), Residue = 55 - (100 * (5 / 100)) }; workTicketThree.RouteTransportWorkTickets.Add(new RouteTransportWorkTicket { TransportWorkTicketId = workTicketThree.Id, RouteId = "242tre-78dhgf-7823dsds", IsDeleted = false }); var fullName = "Мая Малинова"; var routesService = new Mock <IRoutesService>(); var routeTransportWorkTicketsService = new RouteTransportWorkTisketsService(repository, routesService.Object); AutoMapperConfig.RegisterMappings(typeof(RouteTransportViewModel).Assembly); await routeTransportWorkTicketsService.SetIsDeletedAsync(workTicketThree.Id, fullName); var workTickets = repository.AllAsNoTracking().FirstOrDefault(tr => tr.Id == workTicketThree.Id); Assert.Null(workTickets); }
public void GetCurrentFuelConsumptionByCarIdExceptForTheCurrentTransportWorkTicketId() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Car>(new ApplicationDbContext(options.Options)); var carOne = new Car { Model = "Форд Фиеста", LicensePlate = "CO1212KA", CompanyId = "72804eudajhkhfvs-dasfa", FuelType = PatniListi.Data.Models.Enums.Fuel.Дизел, TankCapacity = 55, AverageConsumption = 4, InitialFuel = 10, StartKilometers = 234987 }; var workTicket = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341455153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200100, TravelledDistance = 441, EndKilometers = 200541, FuelAvailability = 55, FuelConsumption = 441 * (5 / 100), Residue = 55 - (441 * (5 / 100)) }; carOne.TransportWorkTickets.Add(workTicket); var workTicketOne = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200541, TravelledDistance = 100, EndKilometers = 200641, FuelAvailability = 55, FuelConsumption = 100 * (5 / 100), Residue = 55 - (100 * (5 / 100)) }; carOne.TransportWorkTickets.Add(workTicketOne); var carTwo = new Car { Model = "Форд Фиеста", LicensePlate = "CO4312KA", CompanyId = "72804eud-3464-hfvs-dasfa", FuelType = PatniListi.Data.Models.Enums.Fuel.Бензин, TankCapacity = 55, AverageConsumption = 6, InitialFuel = 10, StartKilometers = 230444 }; var workTicketTwo = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414188", CarId = "72804eud-3464-hfvs-dasfa", StartKilometers = 200541, TravelledDistance = 200, EndKilometers = 200741, FuelAvailability = 55, FuelConsumption = 200 * (5 / 100), Residue = 55 - (200 * (5 / 100)) }; carTwo.TransportWorkTickets.Add(workTicketTwo); repository.AddAsync(carOne); repository.AddAsync(carTwo); repository.SaveChangesAsync(); var carsService = new CarsService(repository); var fuelConsumptionFromDb = repository .AllAsNoTracking() .Where(c => c.Id == carOne.Id) .Select(i => i.TransportWorkTickets.Where(tr => tr.Id != workTicket.Id).Sum(i => i.FuelConsumption)) .SingleOrDefault(); var travelledDistance = carsService.GetCurrentFuelConsumptionByCarId(carOne.Id, workTicket.Id); Assert.Equal(fuelConsumptionFromDb, travelledDistance); }
public async Task CreateAsyncAddsCar() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <TransportWorkTicket>(new ApplicationDbContext(options.Options)); var transportWorkTicket = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200100, TravelledDistance = 441, EndKilometers = 200541, FuelAvailability = 55, FuelConsumption = 441 * (5 / 100), Residue = 55 - (441 * (5 / 100)), }; var transportWorkTicketsService = new TransportWorkTicketsService(repository); await transportWorkTicketsService.CreateAsync(transportWorkTicket); var workTickets = repository.AllAsNoTracking().ToList(); Assert.Single(workTickets); }
public async Task <TransportWorkTicket> CreateAsync(DateTime date, string userId, string carId, string createdBy, double startKilometers, double endKilometers, double fuelConsumption, double residue, double fuelAvailability, double travelledDistance) { var transportWorkTicket = new TransportWorkTicket { Date = date, UserId = userId, CarId = carId, CreatedBy = createdBy, StartKilometers = startKilometers, EndKilometers = endKilometers, FuelConsumption = fuelConsumption, Residue = residue, FuelAvailability = fuelAvailability, TravelledDistance = travelledDistance, }; await this.transportWorkTicketsRepository.AddAsync(transportWorkTicket); await this.transportWorkTicketsRepository.SaveChangesAsync(); return(transportWorkTicket); }
public async Task GetByIdReturnsTransportWorkTicket() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <TransportWorkTicket>(new ApplicationDbContext(options.Options)); var workTicketOne = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200100, TravelledDistance = 441, EndKilometers = 200541, FuelAvailability = 55, FuelConsumption = 441 * (5 / 100), Residue = 55 - (441 * (5 / 100)) }; var workTicketTwo = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", StartKilometers = 200541, TravelledDistance = 100, EndKilometers = 200641, FuelAvailability = 55, FuelConsumption = 100 * (5 / 100), Residue = 55 - (100 * (5 / 100)) }; var workTicketThree = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = "3414141890", CarId = "72804eu-jhkhfvs-dasfa", StartKilometers = 200800, TravelledDistance = 100, EndKilometers = 200900, FuelAvailability = 55, FuelConsumption = 100 * (5 / 100), Residue = 55 - (100 * (5 / 100)) }; await repository.AddAsync(workTicketOne); await repository.AddAsync(workTicketTwo); await repository.SaveChangesAsync(); var transportWorkTicketsService = new TransportWorkTicketsService(repository); AutoMapperConfig.RegisterMappings(typeof(TransportWorkTicketViewModel).Assembly); var workTicket = transportWorkTicketsService.GetById(workTicketOne.Id); Assert.Equal(workTicketOne.StartKilometers, workTicket.StartKilometers); Assert.Equal(workTicketOne.EndKilometers, workTicket.EndKilometers); Assert.Equal(workTicketOne.TravelledDistance, workTicket.TravelledDistance); Assert.Equal(workTicketOne.Date, workTicket.Date); Assert.Equal(workTicketOne.FuelAvailability, workTicket.FuelAvailability); Assert.Equal(workTicketOne.FuelConsumption, workTicket.FuelConsumption); }
public async Task SeedAsync(ApplicationDbContext dbContext, IServiceProvider serviceProvider) { if (dbContext.TransportWorkTickets.Any()) { return; } var companyIdOne = dbContext.Companies .Where(c => c.Name == "Авангард ЕООД") .Select(c => c.Id) .FirstOrDefault(); var cars = dbContext.Cars .Where(c => c.CompanyId == companyIdOne) .Include(c => c.Invoices) .Include(c => c.CarUsers) .ToList(); var routes = dbContext.Routes .ToList(); for (int i = 0; i < cars.Count; i++) { var travelledDistance = routes[i].Distance; var invoiceQuantity = cars[i].Invoices.Select(i => i.Quantity).FirstOrDefault(); var fuelAvailability = cars[i].InitialFuel + invoiceQuantity; var fuelConsumption = Math.Round(travelledDistance * (cars[i].AverageConsumption / 100.00), 2); var transportWorkTicket = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = cars[i].CarUsers.Select(cu => cu.UserId).FirstOrDefault(), CarId = cars[i].Id, CreatedBy = cars[i].CarUsers.Select(cu => cu.ApplicationUser.FullName).FirstOrDefault(), StartKilometers = cars[i].StartKilometers, TravelledDistance = travelledDistance, EndKilometers = cars[i].StartKilometers + routes[i].Distance, FuelAvailability = fuelAvailability, FuelConsumption = fuelConsumption, Residue = fuelAvailability - fuelConsumption, }; await dbContext.TransportWorkTickets.AddAsync(transportWorkTicket); await dbContext.RouteTransportWorkTickets.AddAsync(new RouteTransportWorkTicket { RouteId = routes[i].Id, TransportWorkTicketId = transportWorkTicket.Id }); } var companyIdTwo = dbContext.Companies .Where(c => c.Name == "ЕT Саламандър") .Select(c => c.Id) .FirstOrDefault(); var carsTwo = dbContext.Cars .Where(c => c.CompanyId == companyIdTwo) .Include(c => c.Invoices) .Include(c => c.CarUsers) .ToList(); for (int i = 0; i < carsTwo.Count; i++) { var travelledDistance = routes[i].Distance; var invoiceQuantity = carsTwo[i].Invoices.Select(i => i.Quantity).FirstOrDefault(); var fuelAvailability = carsTwo[i].InitialFuel + invoiceQuantity; var fuelConsumption = Math.Round(travelledDistance * (carsTwo[i].AverageConsumption / 100.00), 2); var transportWorkTicket = new TransportWorkTicket { Date = DateTime.UtcNow, UserId = carsTwo[i].CarUsers.Select(cu => cu.UserId).FirstOrDefault(), CarId = carsTwo[i].Id, CreatedBy = carsTwo[i].CarUsers.Select(cu => cu.ApplicationUser.FullName).FirstOrDefault(), StartKilometers = carsTwo[i].StartKilometers, TravelledDistance = travelledDistance, EndKilometers = carsTwo[i].StartKilometers + routes[i].Distance, FuelAvailability = fuelAvailability, FuelConsumption = fuelConsumption, Residue = fuelAvailability - fuelConsumption, }; await dbContext.TransportWorkTickets.AddAsync(transportWorkTicket); await dbContext.RouteTransportWorkTickets.AddAsync(new RouteTransportWorkTicket { RouteId = routes[i].Id, TransportWorkTicketId = transportWorkTicket.Id }); } }