public async Task GetHives_HiveAndChainedSection_HiveListItemWithProperlyCountedSections( [Frozen] Mock <IProductStoreHiveContext> context, HiveService service) { var hive = new StoreHive { Id = 1 }; var hiveSection = new StoreHiveSection { Id = 1, StoreHive = hive, StoreHiveId = 1 }; hive.Sections = new[] { hiveSection }; context.Setup(c => c.Hives).ReturnsAsyncEntitySet(new[] { hive }); context.Setup(c => c.Sections).ReturnsAsyncEntitySet(new[] { hiveSection }); var result = await service.GetHivesAsync(); Assert.True(result[0].HiveSectionCount == 1); }
public async Task UpdateHiveSection_UpdateHiveSectionWithExistedCode_ExceptionThrown( [Frozen] Mock <IProductStoreHiveContext> context, HiveSectionService service) { var existedHiveSection = new StoreHiveSection { Id = 1, Code = "Code" }; var updatedExistedHiveSection = new StoreHiveSection { Id = 2, Code = "Code1" }; var newHiveSection = new UpdateHiveSectionRequest { Code = "Code" }; context.Setup(c => c.Sections).ReturnsAsyncEntitySet(new[] { existedHiveSection, updatedExistedHiveSection }); await Assert.ThrowsAsync <RequestedResourceHasConflictException>(async() => await service.UpdateHiveSectionAsync(updatedExistedHiveSection.Id, newHiveSection)); }
public async Task GetHiveSection_RequestWithUnExistedId_ExceptionThrown( [Frozen] Mock <IProductStoreHiveContext> context, HiveSectionService service) { var hiveSection = new StoreHiveSection { Id = 1 }; context.Setup(c => c.Sections).ReturnsAsyncEntitySet(new[] { hiveSection }); await Assert.ThrowsAsync <RequestedResourceNotFoundException>(async() => await service.GetHiveSectionAsync(2)); }
public async Task DeleteHive_DeletingHiveWithUndeletedStatus_ExceptionThrown( [Frozen] Mock <IProductStoreHiveContext> context, HiveSectionService service) { var hiveSection = new StoreHiveSection { Id = 1, IsDeleted = false }; context.Setup(c => c.Sections).ReturnsAsyncEntitySet(new[] { hiveSection }); await Assert.ThrowsAsync <RequestedResourceHasConflictException>(async() => await service.DeleteHiveSectionAsync(hiveSection.Id)); }
public async Task GetHiveSection_RequestWithExistedId_HiveSectionReturned( [Frozen] Mock <IProductStoreHiveContext> context, HiveSectionService service) { var hiveSection = new StoreHiveSection { Id = 1 }; context.Setup(c => c.Sections).ReturnsAsyncEntitySet(new[] { hiveSection }); var resultHive = await service.GetHiveSectionAsync(1); Assert.True(hiveSection.Id == resultHive.Id); }
public async Task <HiveSection> CreateHiveSectionAsync(UpdateHiveSectionRequest createRequest) { StoreHiveSection[] dbHiveSections = await _context.Sections.Where(x => x.Code == createRequest.Code).ToArrayAsync(); if (dbHiveSections.Length > 0) { throw new RequestedResourceHasConflictException("code"); } StoreHiveSection dbHiveSection = Mapper.Map <UpdateHiveSectionRequest, DbHiveSection>(createRequest); dbHiveSection.CreatedBy = _userContext.UserId; dbHiveSection.LastUpdatedBy = _userContext.UserId; _context.Sections.Add(dbHiveSection); await _context.SaveChangesAsync(); return(Mapper.Map <HiveSection>(dbHiveSection)); }
/// <inheritdoc/> public async Task SetStatusAsync(int hiveSectionId, bool deletedStatus) { var dbHiveSections = await _context.Sections.Where(x => x.Id == hiveSectionId).ToArrayAsync(); if (dbHiveSections.Length == 0) { throw new RequestedResourceNotFoundException(); } StoreHiveSection dbHiveSection = dbHiveSections[0]; if (dbHiveSection.IsDeleted != deletedStatus) { dbHiveSection.IsDeleted = deletedStatus; dbHiveSection.LastUpdated = DateTime.Now; dbHiveSection.LastUpdatedBy = _userContext.UserId; await _context.SaveChangesAsync(); } }
public async Task DeleteHiveSectionAsync(int hiveSectionId) { StoreHiveSection[] dbHiveSections = await _context.Sections.Where(x => x.Id == hiveSectionId).ToArrayAsync(); if (dbHiveSections.Length == 0) { throw new RequestedResourceNotFoundException(); } StoreHiveSection dbHiveSection = dbHiveSections[0]; if (dbHiveSection.IsDeleted == false) { throw new RequestedResourceHasConflictException(); } _context.Sections.Remove(dbHiveSection); await _context.SaveChangesAsync(); }
public async Task SetStatusAsync_SetHiveSectionStatus(int sectionId, bool hiveSectionStatusBefor, bool hiveSectionStatusAfter) { var storeHiveSection = new StoreHiveSection(); storeHiveSection.Id = sectionId; storeHiveSection.IsDeleted = hiveSectionStatusBefor; Assert.Equal(hiveSectionStatusBefor, storeHiveSection.IsDeleted); var storeSectionHives = new[] { storeHiveSection }; _context.Setup(s => s.Sections).ReturnsEntitySet(storeSectionHives); var service = new HiveSectionService(_context.Object, _userContext.Object); await service.SetStatusAsync(sectionId, hiveSectionStatusAfter); Assert.Equal(hiveSectionStatusAfter, storeHiveSection.IsDeleted); }
public async Task DeleteHiveSectionAsync_IdAsParametr_StatusIsDeletedTrue() { var list = new StoreHiveSection[1] { new StoreHiveSection { Id = 3, Code = "SECT3", IsDeleted = true } }; var hiveSectionId = list[0].Id; var storeContext = new Mock <IProductStoreHiveContext>(); var userContext = new Mock <IUserContext>(); storeContext.Setup(c => c.Sections).ReturnsAsyncEntitySet(list); var service = new HiveSectionService(storeContext.Object, userContext.Object); await service.DeleteHiveSectionAsync(hiveSectionId); }
public async Task UpdateHiveSection_TryUpdateHiveSection_UpdatedHiveSectionReturned( [Frozen] Mock <IProductStoreHiveContext> context, HiveSectionService service) { var existedHiveSection = new StoreHiveSection { Id = 1, Code = "Code" }; var newHiveSection = new UpdateHiveSectionRequest { Code = "newCode" }; context.Setup(c => c.Sections).ReturnsAsyncEntitySet(new[] { existedHiveSection }); var updatedHiveSection = await service.UpdateHiveSectionAsync(existedHiveSection.Id, newHiveSection); Assert.True(updatedHiveSection.Code == newHiveSection.Code); }
public async Task <HiveSection> UpdateHiveSectionAsync(int hiveSectionId, UpdateHiveSectionRequest updateRequest) { StoreHiveSection[] dbHiveSections = await _context.Sections.Where(x => x.Code == updateRequest.Code && x.Id != hiveSectionId).ToArrayAsync(); if (dbHiveSections.Length > 0) { throw new RequestedResourceHasConflictException("code"); } dbHiveSections = await _context.Sections.Where(x => x.Id == hiveSectionId).ToArrayAsync(); StoreHiveSection dbHiveSection = dbHiveSections.FirstOrDefault() ?? throw new RequestedResourceNotFoundException(); Mapper.Map(updateRequest, dbHiveSection); dbHiveSection.LastUpdatedBy = _userContext.UserId; await _context.SaveChangesAsync(); dbHiveSections = await _context.Sections.Where(x => x.Id == hiveSectionId).ToArrayAsync(); return(dbHiveSections.Select(Mapper.Map <HiveSection>).FirstOrDefault()); }