public async void Dihes_AddAsync_Positive_Test() { var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>() .UseInMemoryDatabase(databaseName: "Dihes_AddAsync_Positive_Test") .Options; using (var context = new DreamFoodDeliveryContext(options)) { var tagService = new TagService(_mapper, context); var service = new DishService(_mapper, context, tagService); TagToAdd[] tags = new TagToAdd[] { new TagToAdd { TagName = "New" } }; DishToAdd dish = new DishToAdd() { Name = "Name", Composition = "Composition", Description = "Description", Weight = "Weight", TagNames = new HashSet <TagToAdd>(tags) }; var resultPositive = await service.AddAsync(dish); resultPositive.IsSuccess.Should().BeTrue(); resultPositive.Data.Name.Should().BeEquivalentTo(dish.Name); resultPositive.Data.Composition.Should().BeEquivalentTo(dish.Composition); resultPositive.Data.Description.Should().BeEquivalentTo(dish.Description); resultPositive.Data.Weight.Should().BeEquivalentTo(dish.Weight); } }
public async Task <Result <TagDB> > AddTagDBAsync(TagToAdd tag, CancellationToken cancellationToken = default) { var tagToAdd = _mapper.Map <TagDB>(tag); _context.Tags.Add(tagToAdd); try { await _context.SaveChangesAsync(cancellationToken); TagDB tagAfterAdding = await _context.Tags.Where(_ => _.TagName == tagToAdd.TagName).Select(_ => _).AsNoTracking().FirstOrDefaultAsync(cancellationToken); return(Result <TagDB> .Ok(tagAfterAdding)); } catch (DbUpdateConcurrencyException ex) { return(Result <TagDB> .Fail <TagDB>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message)); } catch (DbUpdateException ex) { return(Result <TagDB> .Fail <TagDB>(ExceptionConstants.CANNOT_SAVE_MODEL + ex.Message)); } catch (ArgumentNullException ex) { return(Result <TagDB> .Fail <TagDB>(ExceptionConstants.SOURCE_IS_NULL + ex.Message)); } }
public async Task <IActionResult> Create([FromBody, CustomizeValidator] TagToAdd tag, CancellationToken cancellationToken = default) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await _tagService.AddAsync(tag, cancellationToken); return(result.IsError ? BadRequest(result.Message) : (IActionResult)Ok(result.Data)); }
public async void Tags_AddAsync_Positive_Test() { var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>() .UseInMemoryDatabase(databaseName: "Tags_AddAsync_Positive_Test") .Options; using (var context = new DreamFoodDeliveryContext(options)) { var service = new TagService(_mapper, context); string newTag = "NewTag"; TagToAdd tag = new TagToAdd() { TagName = newTag, }; var resultPositive = await service.AddAsync(tag); resultPositive.IsSuccess.Should().BeTrue(); resultPositive.Data.TagName.Should().BeEquivalentTo(tag.TagName); } }
public async void Dishes_UpdateAsync_PositiveAndNegative_Test() { var options = new DbContextOptionsBuilder <DreamFoodDeliveryContext>() .UseInMemoryDatabase(databaseName: "Dishes_UpdateAsync_PositiveAndNegative_Test") .Options; using (var context = new DreamFoodDeliveryContext(options)) { context.AddRange(_dishes); await context.SaveChangesAsync(); } using (var context = new DreamFoodDeliveryContext(options)) { var dish = await context.Dishes.AsNoTracking().FirstOrDefaultAsync(); var tagService = new TagService(_mapper, context); var service = new DishService(_mapper, context, tagService); TagToAdd[] tags = new TagToAdd[] { new TagToAdd { TagName = "New" } }; DishToUpdate updateDish = new DishToUpdate() { Id = dish.Id, Name = "Name", Composition = "Composition", Description = "Description", Weight = "Weight", TagNames = new HashSet <TagToAdd>(tags) }; DishToUpdate failDish = new DishToUpdate() { Id = Guid.NewGuid(), Name = "Name", Composition = "Composition", Description = "Description", Weight = "Weight", TagNames = new HashSet <TagToAdd>(tags) }; var resultPositive = await service.UpdateAsync(updateDish); var resultNegative = await service.UpdateAsync(failDish); resultPositive.IsSuccess.Should().BeTrue(); resultPositive.Data.Name.Should().BeEquivalentTo(updateDish.Name); resultPositive.Data.Composition.Should().BeEquivalentTo(updateDish.Composition); resultPositive.Data.Description.Should().BeEquivalentTo(updateDish.Description); resultPositive.Data.Weight.Should().BeEquivalentTo(updateDish.Weight); resultPositive.Data.Name.Should().NotBeEquivalentTo(dish.Name); resultPositive.Data.Composition.Should().NotBeEquivalentTo(dish.Composition); resultPositive.Data.Description.Should().NotBeEquivalentTo(dish.Description); resultPositive.Data.Weight.Should().NotBeEquivalentTo(dish.Weight); resultNegative.IsSuccess.Should().BeFalse(); } }