public async void should_add_only_new_articles() { int scrappedArticlesCount = 5; int articlesInDbCount = 2; var scrappedArticles = new ArticleBuilder().Build(scrappedArticlesCount); ArticlesResponseViewModel scrappedArticlesResponse = new() { Articles = scrappedArticles }; List <ArticleDb> articlesInDb = scrappedArticles.Take(articlesInDbCount).ToList().Map(); List <Article> articlesToAdd = null; Action <object> mapArticlesToAdd = (list) => { articlesToAdd = list as List <Article>; }; List <Article> expectedResult = scrappedArticles.TakeLast(3).ToList(); _pagesScrapperService.Setup(n => n.ScrapAll()).ReturnsAsync(scrappedArticlesResponse); _articlesRepository.Setup(n => n.Get(null, null, "")).Returns(articlesInDb); _mapper.Setup(n => n.Map <List <ArticleDb> >(It.IsAny <List <Article> >())).Callback(mapArticlesToAdd); await _sut.Scrap(); articlesToAdd.Should().BeEquivalentTo(expectedResult); }
static void Main(string[] args) { var director = new Director(); var articleBuilder = new ArticleBuilder(); director.ArticleBuilder = articleBuilder; Console.WriteLine("Simple article"); director.BuildSimpleArticle(); Console.WriteLine(articleBuilder.GetArticle().GetArticleParts()); Console.WriteLine("-----------------------------------------------------------"); Console.WriteLine("Article with picture"); director.BuildArticleWithPicture(); Console.WriteLine(articleBuilder.GetArticle().GetArticleParts()); Console.WriteLine("-----------------------------------------------------------"); Console.WriteLine("Full article"); director.BuildFullArticle(); Console.WriteLine(articleBuilder.GetArticle().GetArticleParts()); Console.WriteLine("-----------------------------------------------------------"); //Builder can be used without Director class Console.WriteLine("Custom Article"); articleBuilder.CreateTitle(); articleBuilder.CreatePicture(); Console.WriteLine(articleBuilder.GetArticle().GetArticleParts()); }
public async Task ShouldAutoFillCreateTime() { var article = Mapper.Map <Article>(ArticleBuilder.Build(a => { a.CreateTime = DateTime.Now.AddYears(-10); })); await _repository.InsertAsync(article); article.CreateTime.Date.Should().Be(DateTime.Now.Date); }
public async Task ShouldFindAnyWithDate() { await articles.Add(new Article[] { ArticleBuilder.CreateOne(), ArticleBuilder.CreateOne() }); var actual = await articles.AnyWithDate(DateTime.UtcNow.Date); actual.Should().Be(true); }
public Article Create(IArticleCreateDto model) { var builder = new ArticleBuilder() .WithName(model.Name) ; return(builder.Build()); }
public ArticleHandler(RuleConfig ruleConfig) { this._ruleConfig = ruleConfig; this._fileOperate = new FileOperate(); this._articleBuilder = new ArticleBuilder(); this._sqlOperate = new SqlOperate(); this._currentRule = new JsonArticleRule(ruleConfig.Rules); }
public async Task ShouldUpdateFailedWhenEntityNotExists() { var exception = await Assert.ThrowsAsync <KeyNotFoundException>(async() => { await _repository.UpdateAsync(Mapper.Map <Article>(ArticleBuilder.Build())); }); Assert.NotNull(exception.Message); }
public void testMissingBaseUnitPrice() { Assert.Throws <ArgumentOutOfRangeException>(() => { var builder = new ArticleBuilder(); builder.SetProductCode(productCode: "A"); builder.BuildArticle(); }); }
public void testMissingProductCode() { Assert.Throws <InvalidOperationException>(() => { var builder = new ArticleBuilder(); builder.SetUnitPrice(unitPrice: 1.25M); builder.BuildArticle(); }); }
public void Setup() { var builder = new ArticleBuilder(); builder.SetProductCode(productCode: "A"); builder.SetUnitPrice(unitPrice: 1.25M); builder.SetBulkPriceCondition(quantity: 3, price: 3.00M); article = builder.BuildArticle(); }
public async Task ShouldGetArticleSuccess() { var article = Mapper.Map <Article>(ArticleBuilder.Build()); await _articleRepository.InsertAsync(article); var searchedArticle = await _articleRepository.GetByIdAsync(article.Id); searchedArticle.Should().NotBeNull(); }
public async Task ShouldInsertSuccess() { var article = Mapper.Map <Article>(ArticleBuilder.Build()); await _repository.InsertAsync(article); var insertedArticle = await _repository.GetByIdAsync(article.Id); insertedArticle.Should().NotBeNull(); }
public void testBaseUnitPriceArticle() { var builder = new ArticleBuilder(); builder.SetProductCode(productCode: "A"); builder.SetUnitPrice(unitPrice: 1.25M); var article = builder.BuildArticle(); Assert.AreEqual("A", article.productCode); Assert.AreEqual(1.25M, article.CalculatePrice(1)); }
public void should_return_null_if_wrong_page_passed() { List <ArticleDb> articlesFromDb = new ArticleBuilder().Build(9).Map(); _articlesRepository.Setup(n => n.Get(null, It.IsAny <Func <IQueryable <ArticleDb>, IOrderedQueryable <ArticleDb> > >(), "")) .Returns(articlesFromDb); var result = _sut.Get(5, 3); result.Should().BeNull(); }
public void TestCalculatePriceForKgProduct() { var builder = new ArticleBuilder(); builder.SetProductCode(productCode: "Banana"); builder.SetUnitPrice(unitPrice: 2.99M); article = builder.BuildArticle(); Assert.AreEqual(4.57, article.CalculatePrice(quantity: 1.53M), "Rounded to 2dp"); Assert.AreEqual(3.02, article.CalculatePrice(quantity: 1.01M), "Rounded to 2dp"); }
public async Task ShouldSaveAnArticle() { var article = ArticleBuilder.CreateOne(); await articles.Add(new Article[] { article, ArticleBuilder.CreateOne() }); var(isAddedArticle, countAddedAuthors) = await WhenAllWrapper(IsArticleAdded(article.Id), CountArticle(article.Id)); isAddedArticle.Should().Be(true); countAddedAuthors.Should().Be(2); }
public async Task ShouldAutoFilterDeletedArticle() { var article = Mapper.Map <Article>(ArticleBuilder.Build()); article.IsDelete = true; await _articleRepository.InsertAsync(article); var findArticles = await _articleRepository.FindAllAsync(x => true); findArticles.Should().NotContain(x => x.Id == article.Id); }
public void testBulkPricingArticle() { var builder = new ArticleBuilder(); builder.SetProductCode(productCode: "A"); builder.SetUnitPrice(unitPrice: 1.25M); builder.SetBulkPriceCondition(quantity: 3, price: 3.00M); var article = builder.BuildArticle(); Assert.AreEqual("A", article.productCode); Assert.AreEqual(3.0M, article.CalculatePrice(3)); }
public async Task ShouldDeleteSuccess() { var article = Mapper.Map <Article>(ArticleBuilder.Build()); await _repository.InsertAsync(article); await _repository.DeleteAsync(article.Id); var deletedArticle = await _repository.GetByIdAsync(article.Id); deletedArticle.Should().BeNull(); }
public void should_return_null_when_article_not_found() { ArticleDb articleFromDb = new ArticleBuilder().Build().Map(); ArticleDb mappedArticle = null; Action <object> mapArticle = (item) => { mappedArticle = item as ArticleDb; }; _articlesRepository.Setup(n => n.GetById(articleFromDb.Id)).Returns(articleFromDb); _mapper.Setup(n => n.Map <Article>(It.IsAny <ArticleDb>())).Callback(mapArticle); var result = _sut.GetById(articleFromDb.Id); result.Should().BeNull(); }
public void should_return_article() { ArticleDb articleFromDb = new ArticleBuilder().Build().Map(); ArticleDb mappedArticle = null; Action <object> mapArticle = (item) => { mappedArticle = item as ArticleDb; }; _articlesRepository.Setup(n => n.GetById(articleFromDb.Id)).Returns(articleFromDb); _mapper.Setup(n => n.Map <Article>(It.IsAny <ArticleDb>())).Callback(mapArticle); _sut.GetById(articleFromDb.Id); mappedArticle.Should().BeEquivalentTo(articleFromDb); }
private Article SetUpUpdateArticleWhenImageIsDeleted() { var article = new ArticleBuilder() .SetTitle(It.IsNotNull <string>()) .SetContent(It.IsNotNull <string>()) .SetImageUrl("url") .Build(); database.Setup(d => d.ArticleRepository.FindById(updateRequest.ArticleId)).ReturnsAsync(article); mapper.Setup(m => m.Map(updateRequest, article)).Returns(article); database.Setup(d => d.ArticleRepository.Update(article)).ReturnsAsync(true); return(article); }
public async Task ShouldUpdateSuccess() { var article = Mapper.Map <Article>(ArticleBuilder.Build()); await _repository.InsertAsync(article); article.Title = Guid.NewGuid().ToString(); await _repository.UpdateAsync(article); var updatedArticle = await _repository.GetByIdAsync(article.Id); updatedArticle.Title.Should().Be(article.Title); }
public void should_return_articles() { var articlesFromDb = new ArticleBuilder().Build(5).Map(); List <ArticleDb> mappedArticles = null; Action <object> mapArticles = (list) => { mappedArticles = list as List <ArticleDb>; }; _articlesRepository.Setup(n => n.Get(null, It.IsAny <Func <IQueryable <ArticleDb>, IOrderedQueryable <ArticleDb> > >(), "")) .Returns(articlesFromDb); _mapper.Setup(n => n.Map <List <Article> >(It.IsAny <List <ArticleDb> >())).Callback(mapArticles); _sut.Get(5, 1); mappedArticles.Should().BeEquivalentTo(articlesFromDb); }
public async Task ShouldDeleteSuccessWhenPartOfIdNotExist() { var entity = Mapper.Map <Article>(ArticleBuilder.Build()); await _repository.InsertAsync(entity); var ids = new List <ObjectId> { entity.Id, ObjectId.GenerateNewId() }; await _repository.DeleteMultipleAsync(ids); var deletedEntity = await _repository.GetByIdAsync(entity.Id); deletedEntity.Should().BeNull(); }
public ActionResult Article(string articleSlug) { if (articleSlug == null) { throw new HttpException(404, "An article must be specified."); } var builder = new ArticleBuilder(); var article = builder.ValidateArticle(articleSlug); if (article == null) { throw new HttpException(404, "The specified article was not found."); } return(View(article)); }
private Article SetUpUpdateArticleWhenImageIsChanged(bool imageInserted) { var article = new ArticleBuilder() .SetTitle(It.IsNotNull <string>()) .SetContent(It.IsNotNull <string>()) .SetImageUrl(It.IsAny <string>()) .Build(); database.Setup(d => d.ArticleRepository.FindById(updateRequest.ArticleId)).ReturnsAsync(article); mapper.Setup(m => m.Map(updateRequest, article)).Returns(article); filesManager.Setup(fm => fm.Upload(It.IsNotNull <IFormFile>(), It.IsNotNull <string>())) .ReturnsAsync(new FileModel("path", "url", 1)); database.Setup(d => d.ArticleImageRepository.Insert(It.IsNotNull <ArticleImage>(), false)) .ReturnsAsync(imageInserted); database.Setup(d => d.ArticleRepository.Update(article)).ReturnsAsync(true); return(article); }
public void MustNotAddAnArticleWithDuplicatedTitle() { var articleAlreadySaved = ArticleBuilder.New().WithTitle(_articleModel.Title).Build(); _articleRepositoryMock.Setup(r => r.GetByTitle(_articleModel.Title)).Returns(articleAlreadySaved); var result = _articlesController.Post(_articleModel); var expectedResult = new ResultViewModel { Success = false, Message = "This title has already taken.", Data = null }; expectedResult.ToExpectedObject().ShouldMatch(result); //Assert.Throws<ArgumentException>(() => _articlesController.Post(_articleModel)).WithMessage("This title is already taken"); }
public void testOutOfRangeBulkPricingArticle() { Assert.Throws <ArgumentOutOfRangeException>(() => { var builder = new ArticleBuilder(); builder.SetProductCode(productCode: "A"); builder.SetUnitPrice(unitPrice: 1.25M); builder.SetBulkPriceCondition(quantity: 1, price: 3.00M); builder.BuildArticle(); }, "Bulk quantity should be > 1"); Assert.Throws <ArgumentOutOfRangeException>(() => { var builder = new ArticleBuilder(); builder.SetProductCode(productCode: "A"); builder.SetUnitPrice(unitPrice: 1.25M); builder.SetBulkPriceCondition(quantity: 2, price: 0.00M); builder.BuildArticle(); }, "Bulk price should be > 0"); }
public async void should_return_summary_message() { int articlesCount = 5; string summaryMessage = string.Format(ApiResponses.ArticlesAddedAfterScrapping, articlesCount); List <string> expectedResult = new() { summaryMessage }; var scrappedArticles = new ArticleBuilder().Build(articlesCount); ArticlesResponseViewModel scrappedArticlesResponse = new() { Articles = scrappedArticles }; List <ArticleDb> articleInDb = new(); _pagesScrapperService.Setup(n => n.ScrapAll()).ReturnsAsync(scrappedArticlesResponse); _articlesRepository.Setup(n => n.Get(null, null, "")).Returns(articleInDb); _mapper.Setup(n => n.Map <List <ArticleDb> >(It.IsAny <List <Article> >())).Returns(scrappedArticles.Map()); var result = await _sut.Scrap(); result.Should().BeEquivalentTo(expectedResult); }