public void GetUsersCountRetunsUsersCountByCompanyName() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var usersRepository = new EfDeletableEntityRepository <ApplicationUser>(new ApplicationDbContext(options.Options)); var repository = new EfRepository <Company>(new ApplicationDbContext(options.Options)); repository.AddAsync(new Company { Name = "Авангард ЕООД" }); repository.AddAsync(new Company { Name = "ЕТ Саламандър" }); repository.SaveChangesAsync(); var companies = repository.AllAsNoTracking().ToList(); for (int i = 0; i < companies.Count(); i++) { usersRepository.AddAsync(new ApplicationUser { UserName = $"mars{i}", Email = "*****@*****.**", CompanyId = companies[i].Id, FullName = "Емилия Петрова", LastLoggingDate = DateTime.UtcNow }); usersRepository.SaveChangesAsync(); } var companiesService = new CompaniesService(repository); var companyCount = companiesService.GetUsersCount("Авангард ЕООД"); Assert.Equal(1, companyCount); }
public async Task LogicalDelete() { using (EfRepository <LogicalDeleteEntity> localRepository = new EfRepository <LogicalDeleteEntity>(_db)) { await localRepository.AddAsync(_identity, Entity2); await localRepository.AddAsync(_identity, Entity3); await localRepository.SaveChangesAsync(_identity); Assert.AreEqual(2, EfChangeListener.CreatedEntities.Count); Assert.AreEqual(2, (await localRepository.EntitiesAsync(_identity)).Count()); await localRepository.RemoveAsync(_identity, Entity2); await localRepository.SaveChangesAsync(_identity); Assert.AreEqual(2, (await localRepository.EntitiesAsync(_identity)).Count()); var entities = (await localRepository.EntitiesAsync(_identity)).ToArray(); Assert.AreEqual(1, (await localRepository.EntitiesAsync(_identity)).Count(entity => entity.IsDeleted && entity.Id == 1)); Assert.AreEqual(1, (await localRepository.EntitiesAsync(_identity)).Count(entity => !entity.IsDeleted && entity.Id == 2)); Assert.AreEqual(1, EfChangeListener.RemovedEntities.Count); Assert.AreEqual(0, EfChangeListener.ModifiedNewEntities.Count); Assert.AreEqual(0, EfChangeListener.ModifiedOriginalEntities.Count); // The LogicalDeleting mock entity should just modified Assert.AreEqual(false, EfChangeListener.WasOnModifiedCalled); } }
public void GetByIdReturnsCompany() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfRepository <Company>(new ApplicationDbContext(options.Options)); repository.AddAsync(new Company { Name = "Авангард ЕООД", Bulstat = "041835487509", VatNumber = $"BG{041835487509}", PhoneNumber = "+359 889 121 212", Address = "гр.Пловдив, ул.Марашев 5" }); repository.AddAsync(new Company { Name = "ЕТ Саламандър", Bulstat = "63835487509", VatNumber = $"BG{041835487509}", PhoneNumber = "+359 889 211 212", Address = "гр.Пловдив, ул.Марашев 6" }); repository.AddAsync(new Company { Name = "Тиесто Гранд", Bulstat = "041115487509", VatNumber = $"BG{041835487509}", PhoneNumber = "+359 889 121 211", Address = "гр.Пловдив, ул.Марашев 7" }); repository.SaveChangesAsync(); var routesService = new CompaniesService(repository); var routeFromDb = repository.AllAsNoTracking().FirstOrDefault(c => c.Name == "Тиесто Гранд"); var route = routesService.GetById(routeFromDb.Id); Assert.Equal(routeFromDb.Id, route.Id); Assert.Equal(routeFromDb.Name, route.Name); Assert.Equal(routeFromDb.Bulstat, route.Bulstat); Assert.Equal(routeFromDb.Address, route.Address); }
public async Task GetAllAsKeyValuePairsShouldWorkCorrect() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; using var db = new ApplicationDbContext(options); var repository = new EfRepository <Country>(db); await repository.AddAsync(new Country { Id = 1, Name = "Bulgaria" }); await repository.AddAsync(new Country { Id = 2, Name = "Nigeria" }); await repository.AddAsync(new Country { Id = 3, Name = "Germany" }); await repository.SaveChangesAsync(); var service = new CountriesService(repository); var result = service.GetAllAsKeyValuePairs() .Select(x => x.Key + "=>" + x.Value) .ToList(); Assert.Equal(3, result.Count()); // order should be by name Assert.Equal("1=>Bulgaria", result[0]); Assert.Equal("2=>Nigeria", result[2]); Assert.Equal("3=>Germany", result[1]); }
public async Task GetDetailsAsyncReturnsCompanyDetails() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfRepository <Company>(new ApplicationDbContext(options.Options)); await repository.AddAsync(new Company { Name = "Авангард ЕООД" }); await repository.AddAsync(new Company { Name = "ЕТ Саламандър" }); await repository.AddAsync(new Company { Name = "Тиесто Гранд" }); await repository.SaveChangesAsync(); var companiesService = new CompaniesService(repository); var companyFromDb = repository.AllAsNoTracking().FirstOrDefault(c => c.Name == "Тиесто Гранд"); AutoMapperConfig.RegisterMappings(typeof(DetailsCompanyViewModel).Assembly); var company = await companiesService.GetDetailsAsync <DetailsCompanyViewModel>(companyFromDb.Id); Assert.Equal(company.Name, companyFromDb.Name); Assert.Equal(company.Bulstat, companyFromDb.Bulstat); Assert.Equal(company.PhoneNumber, companyFromDb.PhoneNumber); Assert.Equal(company.Address, companyFromDb.Address); }
public async Task TestItemQuantityPerMenu() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var itemRepository = new EfRepository <Item>(new ApplicationDbContext(options.Options)); var orderRepository = new EfDeletableEntityRepository <Order>(new ApplicationDbContext(options.Options)); var dashboardService = new DashboardService(itemRepository, orderRepository); await itemRepository.AddAsync(new Item { Name = "Eggs", Quantity = 0, MenuId = 1 }); itemRepository.SaveChangesAsync().GetAwaiter().GetResult(); await itemRepository.AddAsync(new Item { Name = "Eggs", Quantity = 0, MenuId = 2 }); itemRepository.SaveChangesAsync().GetAwaiter().GetResult(); await itemRepository.AddAsync(new Item { Name = "Pizza", Quantity = 0, MenuId = 2 }); itemRepository.SaveChangesAsync().GetAwaiter().GetResult(); int countMenu1 = dashboardService.ItemQuantityPerMenu(1); int countMenu2 = dashboardService.ItemQuantityPerMenu(2); int countMenu3 = dashboardService.ItemQuantityPerMenu(3); Assert.Equal(1, countMenu1); Assert.Equal(2, countMenu2); Assert.Equal(0, countMenu3); }
public async Task <Author> AddAuthor() { var nationality = await GetNationality(); var tenant = await GetTenant(); var author = new Author { Name = "Mohamed", NationalityId = nationality.Id }; var addedAuthor = await _repo.AddAsync <Author>(author); return(addedAuthor); }
public async Task AddFinance(FinanceDto financeDto) { financeDto.Commission = CalculateCommission(financeDto.Amount); financeDto.Service = "finance"; var finance = _mapper.Map <Finance>(financeDto); await _financeRepository.AddAsync(finance); }
public async Task <ServiceResponse <FirmUserDto> > GrantPermissionAsync(ClaimsPrincipal claims, int id, GrantPermissionRequest request) { var response = new ServiceResponse <FirmUserDto>(); var user = await _userManager.FindByIdAsync(claims.GetUserId()); var found = await claims.GetFirmIfHasMinPermissionOf(_repository, id, Permission.Manage); if (found == null) { response.FailForbiden(); return(response); } var firmUser = new FirmUser() { User = user, Firm = found }; var result = await _repository.AddAsync(firmUser); if (result == null) { response.FailOperation(); } response.Data = _mapper.Map <FirmUserDto>(result); return(response); }
public async Task AddCharity(CharityDto charityDto) { charityDto.Commission = CalculateCommission(charityDto.Amount); charityDto.Service = "charity"; var charity = _mapper.Map <Charity>(charityDto); await _charityRepository.AddAsync(charity); }
public async Task TestDeleteJObAsyncTest() { ApplicationUser user = new ApplicationUser(); Category category = new Category(); var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfDeletableEntityRepository <Job>(new ApplicationDbContext(options.Options)); var studentRepo = new EfRepository <StudentJob>(new ApplicationDbContext(options.Options)); var job = new Job { ApplicationUser = user, Salary = 900, Description = "test", }; await repository.AddAsync(job); await repository.SaveChangesAsync(); var studentJob = new StudentJob { Job = job, ApplicationUser = user, }; await studentRepo.AddAsync(studentJob); var jobService = new JobsService(repository, studentRepo); AutoMapperConfig.RegisterMappings(typeof(MyTestJob).Assembly); Assert.Throws <Exception>(() => jobService.DeleteAsync(3516516).GetAwaiter().GetResult()); }
public async Task RemoveFollowerAsync_WithValidData_ShouldRemoveFollowerFromDatabase() { // Arrange this.InitilaizeMapper(); var context = InMemoryDbContext.Initiliaze(); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context); var userFollowerRepository = new EfRepository <UserFollower>(context); var profileService = new ProfilesService(userRepository, userFollowerRepository); await this.SeedUsers(context); await userFollowerRepository.AddAsync(new UserFollower { UserId = "userOneId", FollowerId = "userTwoId" }); await userFollowerRepository.SaveChangesAsync(); var model = new AddFollowerModel { UserId = "userOneId", FollowerId = "userTwoId", }; // Act int expectedCount = userFollowerRepository.All().Count() - 1; await profileService.RemoveFollowerAsync(model); int actualCount = userFollowerRepository.All().Count(); // Assert Assert.Equal(expectedCount, actualCount); }
public async Task GetFollowerRequestsAsync_WithValidUserId_ShouldReturnFollowerRequests() { // Arrange this.InitilaizeMapper(); var context = InMemoryDbContext.Initiliaze(); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context); var userFollowerRepository = new EfRepository <UserFollower>(context); var profileService = new ProfilesService(userRepository, userFollowerRepository); await this.SeedUsers(context); await userFollowerRepository.AddAsync(new UserFollower { UserId = "userOneId", FollowerId = "userTwoId", IsApproved = false }); await userFollowerRepository.SaveChangesAsync(); // Act var requests = await profileService.GetFollowerRequestsAsync("userOneId"); string expectedUsername = "******"; // The username of the follower. string actualUsername = requests.FirstOrDefault().FollowerUserName; // Assert Assert.Equal(expectedUsername, actualUsername); }
public void GetTicketDetailsShouldWorkCorrectly() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "TicketTest1").Options; var dbContext = new ApplicationDbContext(options); AutoMapperConfig.RegisterMappings(typeof(ErrorViewModel).GetTypeInfo().Assembly); var ticketsRepository = new EfRepository <Ticket>(dbContext); var projectionsRepository = new EfRepository <Projection>(dbContext); var hallsRepository = new EfRepository <Hall>(dbContext); var seatsRepository = new EfRepository <Seat>(dbContext); var moviesRepository = new EfRepository <Movie>(dbContext); var ciemasRepository = new EfRepository <Cinema>(dbContext); var service = new TicketsService( ticketsRepository, projectionsRepository, hallsRepository, seatsRepository, moviesRepository, ciemasRepository); ticketsRepository.AddAsync(this.TicketTest()).GetAwaiter().GetResult(); ticketsRepository.SaveChangesAsync().GetAwaiter().GetResult(); var ticketViewModel = service.GetTicketDetails(1); Assert.IsType <TicketViewModel>(ticketViewModel); Assert.Equal(0, ticketViewModel.OccupiedSeats.Count); Assert.Equal(10, ticketViewModel.Price); }
public async Task MovieGetByIdShouldReturnCorrectData() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "MoviesDbTest132").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfRepository <Movie>(dbContext); var service = new MovieService(repository); await repository.AddAsync(new Movie { Id = 1, Title = "Test123", Actors = "Some actors", Country = MegaCinema.Data.Models.Enums.Country.Australia, Description = "some test description", Director = "Adam West", Duration = new System.TimeSpan(1, 52, 25), Genre = GenreType.Action, Language = MegaCinema.Data.Models.Enums.Language.English, Rating = MPAARating.G, Score = 7.5, ReleaseDate = new System.DateTime(2020, 02, 22), }); // Assert.True(service.MovieExist(1)); Assert.True(service.MovieTitleExists("Test123")); }
public async Task AddAsync_SavesObject( List <Core.Entities.Product> products ) { //Arrange var mockSet = products.AsQueryable().BuildMockDbSet(); var mockContext = new Mock <AWContext>(); mockContext.Setup(x => x.Set <Core.Entities.Product>()) .Returns(mockSet.Object); var repository = new EfRepository <Core.Entities.Product>(mockContext.Object); //Act var newProduct = new Core.Entities.Product( products[0].ProductNumber, products[0].Color ); var savedProduct = await repository.AddAsync(newProduct); //Assert mockSet.Verify(x => x.Add(It.IsAny <Core.Entities.Product>())); mockContext.Verify(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())); newProduct.Should().BeEquivalentTo(savedProduct); }
public async Task TestEditRecipeStepToItem() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var recipeStepRepository = new EfRepository <RecipeStep>(new ApplicationDbContext(options.Options)); var itemRepository = new EfRepository <Item>(new ApplicationDbContext(options.Options)); recipeStepRepository.AddAsync(new RecipeStep { Description = "Testing description", Minutes = 10 }).GetAwaiter().GetResult(); recipeStepRepository.SaveChangesAsync().GetAwaiter().GetResult(); itemRepository.AddAsync(new Item { Name = "Meatball" }).GetAwaiter().GetResult(); itemRepository.SaveChangesAsync().GetAwaiter().GetResult(); var item = itemRepository.All().Where(x => x.Name == "Meatball").FirstOrDefault(); var recipeStep = recipeStepRepository.All().Where(x => x.Description == "Testing description").FirstOrDefault(); var recipeStepService = new RecipeStepService(itemRepository, recipeStepRepository); await recipeStepService.AddRecipeStepToItem(recipeStep.Description, recipeStep.Minutes, item.Name); await recipeStepService.EditRecipeToItem("Testing another description", 15, item.Name, 1); Assert.Equal(15, recipeStep.Minutes); Assert.Equal("Testing another description", recipeStep.Description); }
public async Task EditAsyncUpdatesExistingRoute() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var repository = new EfRepository <Company>(new ApplicationDbContext(options.Options)); var company = new Company { Name = "Авангард ЕООД" }; await repository.AddAsync(company); await repository.SaveChangesAsync(); var companiesService = new CompaniesService(repository); company.Bulstat = "2638236483"; company.VatNumber = $"BG{company.Bulstat}"; company.PhoneNumber = "+ 359 999 000 000"; await companiesService.EditAsync(company); var updatedCompany = repository.AllAsNoTracking().FirstOrDefault(c => c.Name == "Авангард ЕООД"); Assert.Equal(company.Bulstat, updatedCompany.Bulstat); Assert.Equal(company.VatNumber, updatedCompany.VatNumber); Assert.Equal(company.PhoneNumber, updatedCompany.PhoneNumber); }
public async Task AddCommunal(CommunalDto communalDto) { communalDto.Commission = CalculateCommission(communalDto.Amount); communalDto.Service = "communal"; var communal = _mapper.Map <Communal>(communalDto); await _communalRepository.AddAsync(communal); }
public async Task <ServiceResponse <MeetingDto> > AddAsync(ClaimsPrincipal claims, int firmId, MeetingDto request) { var response = new ServiceResponse <MeetingDto>(); var found = await claims.GetFirmIfHasMinPermissionOf(_repository, firmId, Permission.Manage); if (found == null) { response.FailForbiden(); return(response); } var meeting = _mapper.Map <Meeting>(request); meeting.Firm = found; var result = await _repository.AddAsync(meeting); if (result == null) { response.FailOperation(); return(response); } response.Data = _mapper.Map <MeetingDto>(result); return(response); }
public async Task TestEditIngredientToItem() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var ingredientrRepository = new EfRepository <Ingredient>(new ApplicationDbContext(options.Options)); var itemRepository = new EfRepository <Item>(new ApplicationDbContext(options.Options)); ingredientrRepository.AddAsync(new Ingredient { Name = "Eggs", Quantity = 5 }).GetAwaiter().GetResult(); ingredientrRepository.SaveChangesAsync().GetAwaiter().GetResult(); itemRepository.AddAsync(new Item { Name = "Meatball" }).GetAwaiter().GetResult(); itemRepository.SaveChangesAsync().GetAwaiter().GetResult(); var item = itemRepository.All().Where(x => x.Name == "Meatball").FirstOrDefault(); var ingredient = ingredientrRepository.All().Where(x => x.Name == "Eggs").FirstOrDefault(); var ingredientService = new IngredientService(itemRepository, ingredientrRepository); //AutoMapperConfig.RegisterMappings(typeof(MyTestIngredient).Assembly); await ingredientService.AddIngredientToItem(ingredient.Name, ingredient.Quantity, item.Name); await ingredientService.EditIngredientToItem("Milk", 10, item.Name, 1); Assert.Equal(10, ingredient.Quantity); Assert.Equal("Milk", ingredient.Name); ingredientrRepository.Delete(ingredient); }
public async Task TestAddItemToOrderOrderedQuantityIsBiggerThenItemQuantity() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var orderRepository = new EfDeletableEntityRepository <Order>(new ApplicationDbContext(options.Options)); var itemsRepository = new EfRepository <Item>(new ApplicationDbContext(options.Options)); var orderItemRepository = new EfRepository <OrderItem>(new ApplicationDbContext(options.Options)); orderRepository.AddAsync(new Order { UserId = "1" }).GetAwaiter().GetResult(); orderRepository.SaveChangesAsync().GetAwaiter().GetResult(); itemsRepository.AddAsync(new Item { Name = "Pizza", Quantity = 5 }).GetAwaiter().GetResult(); itemsRepository.SaveChangesAsync().GetAwaiter().GetResult(); var orderService = new OrderService(itemsRepository, orderRepository, orderItemRepository); await orderService.AddItemToOrder(1, "1", 10); var orderItems = orderItemRepository.All().Where(x => x.ItemId == 1).FirstOrDefault(); Assert.Equal(5, orderItems.Quantity); }
private async Task <Client> AddClient(int id) { var client = new ClientBuilder().Id(id).Build(); await _repository.AddAsync(client); return(client); }
private async Task <Room> AddRoom(int id) { var room = new RoomBuilder().Id(id).Build(); await _repository.AddAsync(room); return(room); }
private async Task <Doctor> AddDoctor(int id) { var doctor = new DoctorBuilder().Id(id).Build(); await _repository.AddAsync(doctor); return(doctor); }
private async Task <ClinicManagement.Core.Aggregates.Doctor> AddDoctor(int id) { var doctor = new DoctorBuilder().Id(id).Build(); await _repository.AddAsync <ClinicManagement.Core.Aggregates.Doctor, int>(doctor); return(doctor); }
private async Task <FrontDesk.Core.Aggregates.Doctor> AddDoctor(int id) { var doctor = new DoctorBuilder().Id(id).Build(); await _repository.AddAsync <FrontDesk.Core.Aggregates.Doctor, int>(doctor); return(doctor); }
private async Task <FrontDesk.Core.Aggregates.Room> AddRoom(int id) { var room = new RoomBuilder().Id(id).Build(); await _repository.AddAsync <FrontDesk.Core.Aggregates.Room, int>(room); return(room); }
private async Task <ClinicManagement.Core.Aggregates.Room> AddRoom(int id) { var room = new RoomBuilder().Id(id).Build(); await _repository.AddAsync <ClinicManagement.Core.Aggregates.Room, int>(room); return(room); }
private async Task <ClinicManagement.Core.Aggregates.Client> AddClient() { var client = new ClientBuilder().Id(7).Build(); await _repository.AddAsync <ClinicManagement.Core.Aggregates.Client, int>(client); return(client); }