public async Task DeleteAsyncReturnsFalseIfCarDoesNotExist() { 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 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 }; await repository.AddAsync(carOne); await repository.AddAsync(carTwo); await repository.SaveChangesAsync(); var carsService = new CarsService(repository); var isDeleted = await carsService.DeleteAsync("4738-djsk-r4-3456", "Петър Петров"); var carsCount = repository.AllAsNoTracking().Count(); Assert.Equal(2, carsCount); Assert.False(isDeleted); }
public void GetByIdReturnsCar() { 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 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 carThree = new Car { Model = "Форд Фиеста 8", LicensePlate = "CO9812KA", CompanyId = "72804eudajhkhfvs-dasfa", FuelType = PatniListi.Data.Models.Enums.Fuel.Дизел, TankCapacity = 55, AverageConsumption = 5, InitialFuel = 10, StartKilometers = 234957 }; repository.AddAsync(carOne); repository.AddAsync(carTwo); repository.AddAsync(carThree); repository.SaveChangesAsync(); var carsService = new CarsService(repository); var carFromDb = repository.AllAsNoTracking().FirstOrDefault(c => c.Id == carThree.Id); var car = carsService.GetById(carThree.Id); Assert.Equal(carFromDb.Model, car.Model); Assert.Equal(carFromDb.LicensePlate, car.LicensePlate); Assert.Equal(carFromDb.AverageConsumption, car.AverageConsumption); }
public void IsGetAboutInfoRight() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(new ApplicationDbContext(options.Options)); var breedsRepository = new EfDeletableEntityRepository <Breed>(new ApplicationDbContext(options.Options)); var imagesRepository = new EfDeletableEntityRepository <Image>(new ApplicationDbContext(options.Options)); var petsRepository = new EfDeletableEntityRepository <Pet>(new ApplicationDbContext(options.Options)); var aboutService = new AboutService(userRepository, breedsRepository, imagesRepository, petsRepository); var userCount = userRepository.AllAsNoTracking().Count(); var breedsCount = breedsRepository.AllAsNoTracking().Count(); var imagesCount = imagesRepository.AllAsNoTracking().Count(); var petsCount = petsRepository.AllAsNoTracking().Count(); var expexted = new SiteAboutViewModel { UsersCount = userCount, BreedsCount = breedsCount, ImagesCount = imagesCount, PetsCount = petsCount, }; var actual = aboutService.GetAboutInfo(); Assert.Equal(expexted.UsersCount, actual.UsersCount); Assert.Equal(expexted.BreedsCount, actual.BreedsCount); Assert.Equal(expexted.ImagesCount, actual.ImagesCount); Assert.Equal(expexted.PetsCount, actual.PetsCount); }
public async Task Handle_GivenValidRequest_ShouldCreateInvite() { // Arrange var command = new InvitePlayerCommand { TeamId = 1, UserName = "******" }; var playersRepository = new EfDeletableEntityRepository <Player>(this.dbContext); var teamInvitesRepository = new EfDeletableEntityRepository <TeamInvite>(this.dbContext); var userAccessorMock = new Mock <IUserAccessor>(); userAccessorMock.Setup(x => x.UserId).Returns("Foo1"); var sut = new InvitePlayerCommandHandler(this.deletableEntityRepository, playersRepository, teamInvitesRepository, this.mediatorMock.Object, userAccessorMock.Object); // Act var rowsAffected = await sut.Handle(command, It.IsAny <CancellationToken>()); // Assert rowsAffected.ShouldBeGreaterThan(0); var createdInvite = teamInvitesRepository.AllAsNoTracking().FirstOrDefault(x => x.PlayerId == "Foo5"); createdInvite.ShouldNotBeNull(); createdInvite.TeamName.ShouldBe("FooTeam1"); }
public async Task EditAsyncUpdatesExistingRoute() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Route>(new ApplicationDbContext(options.Options)); var route = new Route { StartPoint = "Видин", EndPoint = "Добрич", Distance = 189, }; await repository.AddAsync(route); await repository.SaveChangesAsync(); var routesService = new RoutesService(repository); route.StartPoint = "Видин"; route.EndPoint = "Добрич"; route.Distance = 200; await routesService.EditAsync(route); var updatedRoute = repository.AllAsNoTracking().FirstOrDefault(r => r.Id == route.Id); Assert.Equal(route.StartPoint, updatedRoute.StartPoint); Assert.Equal(route.EndPoint, updatedRoute.EndPoint); Assert.Equal(route.Distance, updatedRoute.Distance); }
public void GetByIdReturnsRoute() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Route>(new ApplicationDbContext(options.Options)); repository.AddAsync(new Route { StartPoint = "София", EndPoint = "Пловдив", Distance = 225 }); repository.AddAsync(new Route { StartPoint = "Варна", EndPoint = "Хасково", Distance = 341 }); repository.SaveChangesAsync(); var routesService = new RoutesService(repository); var routeFromDb = repository.AllAsNoTracking().FirstOrDefault(r => r.StartPoint == "Варна" && r.EndPoint == "Хасково"); var route = routesService.GetById(routeFromDb.Id); Assert.Equal(routeFromDb.Id, route.Id); Assert.Equal(routeFromDb.StartPoint, route.StartPoint); Assert.Equal(routeFromDb.EndPoint, route.EndPoint); Assert.Equal(routeFromDb.Distance, route.Distance); }
public async Task CreateAsyncShouldWorkCorrectly() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var serviceInfoRepo = new EfDeletableEntityRepository <ServiceInfo>(dbContext); var simRepo = new EfDeletableEntityRepository <SimCard>(dbContext); var moqServiceNumberService = new Mock <IServiceNumberService>(); var service = new ServiceInfoService( serviceInfoRepo, simRepo, moqServiceNumberService.Object); var orderId = Guid.NewGuid().ToString(); var model = new ServiceInfoModel(); var serviceInfo = await service.CreateAsync <ServiceInfoModel>(orderId, model, string.Empty); var count = await serviceInfoRepo.AllAsNoTracking() .CountAsync(); var serviceInfoForComp = await serviceInfoRepo.All() .FirstOrDefaultAsync(); Assert.Equal(serviceInfo.Id, serviceInfoForComp.Id); Assert.Equal(serviceInfo.OrderId, serviceInfoForComp.OrderId); Assert.Equal(orderId, serviceInfoForComp.OrderId); Assert.Equal(1, count); }
public async Task ExistAsyncShouldReturnTrueWhenExists() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var userRepo = new EfDeletableEntityRepository <ApplicationUser>(dbContext); var service = new EmployeesService(userRepo); await userRepo.AddAsync(new ApplicationUser { FirstName = "ivan", LastName = "ivanov", MiddleName = "ivanov", EGN = "1234567890", }); await userRepo.SaveChangesAsync(); var customerId = (await userRepo.AllAsNoTracking() .FirstOrDefaultAsync()).Id; Assert.True(await service.ExistAsync(customerId)); }
public async void RemoveCommentNotificationShouldDeleteTheCorrectNotification() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var notificationRepository = new EfDeletableEntityRepository <Notification>(new ApplicationDbContext(options.Options)); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(new ApplicationDbContext(options.Options)); var notificationsService = new NotificationsService(notificationRepository, userRepository); var notification = new Notification(); var notification2 = new Notification { Content = "True", }; await notificationRepository.AddAsync(notification); await notificationRepository.AddAsync(notification2); await notificationRepository.SaveChangesAsync(); await notificationsService.RemoveCommentNotification(notification.Id); var notifFromDb = await notificationRepository.AllAsNoTracking().FirstAsync(); Assert.Equal(notification2.Content, notifFromDb.Content); }
public async Task DeleteAsyncReturnsFalseIfDriverDoesNotExist() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <ApplicationUser>(new ApplicationDbContext(options.Options)); var userOne = new ApplicationUser { UserName = "******", Email = "*****@*****.**", CompanyId = "7480-9141-3274983", FullName = "Емилия Петрова", LastLoggingDate = DateTime.UtcNow }; var userTwo = new ApplicationUser { UserName = "******", Email = "*****@*****.**", CompanyId = "7480-32141-3274983", FullName = "Петър Петров", LastLoggingDate = DateTime.UtcNow }; await repository.AddAsync(userOne); await repository.AddAsync(userTwo); await repository.SaveChangesAsync(); var usersService = new UsersService(repository); var isDeleted = await usersService.DeleteAsync("7480-9100-3274983", "Петър Петров"); var usersCount = repository.AllAsNoTracking().Count(); Assert.Equal(2, usersCount); Assert.False(isDeleted); }
public async Task GetByIdAsyncShouldWorkCorrectly() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var userRepo = new EfDeletableEntityRepository <ApplicationUser>(dbContext); var service = new EmployeesService(userRepo); await userRepo.AddAsync(new ApplicationUser { FirstName = "ivan", LastName = "ivanov", MiddleName = "ivanov", EGN = "1234567890", }); await userRepo.SaveChangesAsync(); var customerId = (await userRepo.AllAsNoTracking() .FirstOrDefaultAsync()).Id; var customer = await service.GetByIdAsync <EmployeeModel>(customerId); Assert.Equal("ivan", customer.FirstName); Assert.Equal("1234567890", customer.EGN); Assert.Equal("ivanov", customer.LastName); }
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 EditAsyncUpdatesCarInfo() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Invoice>(new ApplicationDbContext(options.Options)); var invoicesService = new InvoicesService(repository); var invoiceOne = new Invoice { Number = "43254325235", Date = DateTime.UtcNow, FuelType = "Бензин", CurrentLiters = 10, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", Location = "София", Price = 2.09m, Quantity = 25.21, TotalPrice = 2.09m * 25.21m }; var invoiceTwo = new Invoice { Number = "11254325235", Date = DateTime.UtcNow, FuelType = "Бензин", CurrentLiters = 25, UserId = "331414153", CarId = "72804eudajhkhfvs-dasfa", Location = "София", Price = 2.07m, Quantity = 45.21, TotalPrice = 2.09m * 25.21m }; await repository.AddAsync(invoiceOne); await repository.AddAsync(invoiceTwo); await repository.SaveChangesAsync(); invoiceOne.Number = "123456789"; invoiceOne.Price = 2.12m; invoiceOne.TotalPrice = invoiceOne.Price * (decimal)invoiceOne.Quantity; await invoicesService.EditAsync(invoiceOne); var invoice = repository.AllAsNoTracking().FirstOrDefault(i => i.Id == invoiceOne.Id); Assert.Equal(invoiceOne.Number, invoice.Number); Assert.Equal(invoiceOne.Price, invoice.Price); Assert.Equal(invoiceOne.TotalPrice, invoice.TotalPrice); }
public async Task CityExistAsyncShouldReturnFalseWhenCityDoesNotExist() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var addressRepo = new EfDeletableEntityRepository <Address>(dbContext); var countryRepo = new EfDeletableEntityRepository <Country>(dbContext); var cityRepo = new EfDeletableEntityRepository <City>(dbContext); var service = new AddressService(addressRepo, countryRepo, cityRepo); await countryRepo.AddAsync(new Country { Name = GlobalConstants.CountryOfUsing, }); await countryRepo.SaveChangesAsync(); var countryId = (await countryRepo.AllAsNoTracking() .FirstOrDefaultAsync(x => x.Name == GlobalConstants.CountryOfUsing)).Id; await service.AddNewCityAsync <CityModel>(new CityModel() { Name = "Varna", }); Assert.False(await service.CityExistAsync(2)); }
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 EfDeletableEntityRepoAllNoTrackingShouldWork() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "EfDeletableEntityRepoAllNoTrackingShouldWork").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Category>(dbContext); var service = new CategoriesService(repository); await service.AddAsync("test", new Image() { Url = "https://miau.bg/files/lib/600x350/agresive-cat1.jpg" }, "description"); await service.AddAsync("test2", new Image() { Url = "https://miau.bg/files/lib/600x350/agresive-cat1.jpg" }, "description2"); await service.AddAsync("test3", new Image() { Url = "https://miau.bg/files/lib/600x350/agresive-cat1.jpg" }, "description3"); var categoryToDelete = service.GetCategoryByName("test3"); repository.Delete(categoryToDelete); await repository.SaveChangesAsync(); var result = repository.AllAsNoTracking().ToList(); Assert.Equal(2, result.Count); }
public async Task CreateCaseTests() { AutoMapperConfig.RegisterMappings(typeof(CreateCaseInputViewModel).GetTypeInfo().Assembly); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "TestCreateCaseDb") .Options; using var dbContext = new ApplicationDbContext(options); dbContext.Cases.AddRange( new Case() { CaseNumber = "1", AttorneyId = "1", ClientId = "1", JudgeId = 1, TrusteeId = 1, DateFiled = DateTime.UtcNow, }); dbContext.SaveChanges(); using var casesRepository = new EfDeletableEntityRepository <Case>(dbContext); using var notesRepository = new EfDeletableEntityRepository <Note>(dbContext); var notesService = new NotesService(notesRepository); var casesService = new CasesService(casesRepository, notesService); var workingInput = new CreateCaseInputViewModel() { CaseNumber = "2", AttorneyId = "2", JudgeId = "2", TrusteeId = "2", DateFiled = DateTime.UtcNow, }; var duplicateCaseNumberInput = new CreateCaseInputViewModel() { CaseNumber = "1", AttorneyId = "2", JudgeId = "2", TrusteeId = "2", DateFiled = DateTime.UtcNow, }; var caseId = await casesService.CreateCaseAsync("newClient", workingInput); Assert.True(caseId is string); await Assert.ThrowsAsync <ArgumentException>( async() => { await casesService.CreateCaseAsync("newClient", duplicateCaseNumberInput); }); Assert.Equal(2, casesRepository.AllAsNoTracking().Count()); }
public async Task CreateAsyncAddRouteToDbIfDoesNotExists() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Route>(new ApplicationDbContext(options.Options)); var routesService = new RoutesService(repository); await routesService.CreateAsync("Видин", "Добрич", 189); var routeCount = repository.AllAsNoTracking().ToList().Count(); Assert.Equal(1, routeCount); }
public async Task CreateAsyncShouldWorkCorrectly() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var orderRepo = new EfDeletableEntityRepository <Order>(dbContext); var moqServiceInfoService = new Mock <IServiceInfoService>(); var fackeInfo = new ServiceInfo(); moqServiceInfoService.Setup(x => x.CreateAsync <ServiceInfoModel>(It.IsAny <string>(), It.IsAny <ServiceInfoModel>(), It.IsAny <string>())).Returns(Task.FromResult(fackeInfo)); var moqServiceNumberService = new Mock <IServiceNumberService>(); var service = new OrderService( orderRepo, moqServiceInfoService.Object, moqServiceNumberService.Object); var userId = Guid.NewGuid().ToString(); await service.CreateAsync <ServiceInfoModel, OrderModel>( new OrderModel { UserId = userId, }, new ServiceInfoModel()); var order = await orderRepo.AllAsNoTracking() .FirstOrDefaultAsync(); var count = await orderRepo.AllAsNoTracking() .CountAsync(); Assert.Equal(userId, order.UserId); Assert.Equal(1, count); }
public async Task CreateAsyncShouldWorkCorrectly() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var billRepo = new EfDeletableEntityRepository <Bill>(dbContext); var service = new BillsService(billRepo); var customerId = Guid.NewGuid().ToString(); var url = Guid.NewGuid().ToString(); await service.CreateAsync(customerId, url); var bill = await billRepo.AllAsNoTracking() .FirstOrDefaultAsync(x => x.Id == 1); var count = await billRepo.AllAsNoTracking().CountAsync(); Assert.Equal(1, count); Assert.Equal(customerId, bill.CusotmerId); Assert.Equal(url, bill.URL); }
public async Task CreateAsyncAddsCar() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Invoice>(new ApplicationDbContext(options.Options)); var carsRepository = 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 }; await carsRepository.AddAsync(carOne); 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 }; await carsRepository.AddAsync(carTwo); var carThree = new Car { Model = "Форд Фиеста 8", LicensePlate = "CO9812KA", CompanyId = "72804eudajhkhfvs-dasfa", FuelType = PatniListi.Data.Models.Enums.Fuel.Дизел, TankCapacity = 55, AverageConsumption = 5, InitialFuel = 10, StartKilometers = 234957 }; await carsRepository.AddAsync(carThree); await carsRepository.SaveChangesAsync(); var invoicesService = new InvoicesService(repository); var invoiceOne = new Invoice { Number = "43254325235", Date = DateTime.UtcNow, FuelType = "Бензин", CurrentLiters = 10, UserId = "341414153", CarId = "72804eudajhkhfvs-dasfa", Location = "София", Price = 2.09m, Quantity = 25.21, TotalPrice = 2.09m * 25.21m }; var invoiceTwo = new Invoice { Number = "11254325235", Date = DateTime.UtcNow, FuelType = "Бензин", CurrentLiters = 25, UserId = "331414153", CarId = "72804eudajhkhfvs-dasfa", Location = "София", Price = 2.07m, Quantity = 45.21, TotalPrice = 2.09m * 25.21m }; var invoiceThree = new Invoice { Number = "43258825235", Date = DateTime.UtcNow, FuelType = "Бензин", CurrentLiters = 20, UserId = "221414153", CarId = "72804eud-3464-hfvs-dasfa", Location = "Варна", Price = 2.06m, Quantity = 22.21, TotalPrice = 2.06m * 22.21m }; await invoicesService.CreateAsync(invoiceOne); await invoicesService.CreateAsync(invoiceTwo); await invoicesService.CreateAsync(invoiceThree); var cars = repository.AllAsNoTracking().ToList().Count(); Assert.Equal(3, cars); }
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 async Task CreateAsyncAddsCar() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Car>(new ApplicationDbContext(options.Options)); var car = new Car { Model = "Форд Фиеста", LicensePlate = "CO1212KA", CompanyId = "72804eudajhkhfvs-dasfa", FuelType = PatniListi.Data.Models.Enums.Fuel.Дизел, TankCapacity = 55, AverageConsumption = 4, InitialFuel = 10, StartKilometers = 234987 }; var carsService = new CarsService(repository); await carsService.CreateAsync(car); var cars = repository.AllAsNoTracking().ToList().Count(); Assert.Equal(1, cars); }
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 SetIsDeletedAsyncChangesUsersForCars() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <CarUser>(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 }; carOne.CarUsers.Add(new CarUser { CarId = carOne.Id, UserId = "242hds-78dsd-7823dsds", IsDeleted = false }); 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 }; carTwo.CarUsers.Add(new CarUser { CarId = carTwo.Id, UserId = "242hds-78dhgf-7823dsds", IsDeleted = false }); carTwo.CarUsers.Add(new CarUser { CarId = carTwo.Id, UserId = "242tre-78dhgf-7823dsds", IsDeleted = false }); var carThree = new Car { Model = "Форд Фиеста 8", LicensePlate = "CO9812KA", CompanyId = "72804eudajhkhfvs-dasfa", FuelType = PatniListi.Data.Models.Enums.Fuel.Дизел, TankCapacity = 55, AverageConsumption = 5, InitialFuel = 10, StartKilometers = 234957 }; carThree.CarUsers.Add(new CarUser { CarId = carThree.Id, UserId = "242tre-78dhgf-7823dsds", IsDeleted = false }); var fullName = "Мая Маринова"; var usersService = new Mock <IUsersService>(); var carUsersService = new CarUsersService(repository, usersService.Object); await carUsersService.SetIsDeletedAsync(carTwo.Id, fullName); var carsFromDb = repository.AllAsNoTracking().FirstOrDefault(c => c.Car.CompanyId == carTwo.CompanyId); Assert.Null(carsFromDb); }
public async Task Handle_GivenValidRequest_ShouldAddPlayerToTeam() { // Arrange var command = new AddPlayerCommand { TeamId = 1, PlayerId = "Foo5" }; var playerTeamsRepository = new EfDeletableEntityRepository <PlayerTeam>(this.dbContext); var sut = new AddPlayerCommandHandler(this.deletableEntityRepository, playerTeamsRepository); // Act var affectedRows = await sut.Handle(command, It.IsAny <CancellationToken>()); // Assert affectedRows.ShouldBeGreaterThan(0); var isPlayerPresent = playerTeamsRepository.AllAsNoTracking().Any(x => x.PlayerId == "Foo5" && x.TeamId == 1); isPlayerPresent.ShouldBe(true); }
public void IsExistsReturnsTrueWhenRouteExists() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Route>(new ApplicationDbContext(options.Options)); repository.AddAsync(new Route { StartPoint = "София", EndPoint = "Пловдив", Distance = 225 }); repository.AddAsync(new Route { StartPoint = "София", EndPoint = "Хасково", Distance = 267 }); repository.SaveChangesAsync(); var routesService = new RoutesService(repository); var route = repository.AllAsNoTracking().FirstOrDefault(r => r.StartPoint == "София" && r.EndPoint == "Пловдив"); var exists = routesService.IsExists(route.StartPoint, route.EndPoint); Assert.True(exists); }
public async void RemoveQuestionsAnswersFromUserAsyncShouldRemoveDataAccordingly() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(new ApplicationDbContext(options.Options)); var questionRepository = new EfDeletableEntityRepository <QuestionAnswer>(new ApplicationDbContext(options.Options)); var administartorService = new AdministartorService(userRepository, questionRepository); var user = new ApplicationUser(); var questionAnswer = new List <QuestionAnswer> { new QuestionAnswer { UserId = user.Id }, new QuestionAnswer { UserId = user.Id }, new QuestionAnswer { UserId = user.Id }, new QuestionAnswer { UserId = user.Id }, }; foreach (var qa in questionAnswer) { await questionRepository.AddAsync(qa); } await questionRepository.SaveChangesAsync(); await administartorService.RemoveQuestionsAnswersFromUserAsync(user.Id); Assert.Empty(questionRepository.AllAsNoTracking()); }
public async void RemoveCommentNotificationShouldReturnProperCount() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var notificationRepository = new EfDeletableEntityRepository <Notification>(new ApplicationDbContext(options.Options)); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(new ApplicationDbContext(options.Options)); var notificationsService = new NotificationsService(notificationRepository, userRepository); var notification = new Notification(); var notification2 = new Notification(); await notificationRepository.AddAsync(notification); await notificationRepository.AddAsync(notification2); await notificationRepository.SaveChangesAsync(); await notificationsService.RemoveCommentNotification(notification.Id); Assert.Single(notificationRepository.AllAsNoTracking()); }
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); }