コード例 #1
0
        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);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Navrazz/CSharpEssentials
        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());
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        public Article Create(IArticleCreateDto model)
        {
            var builder = new ArticleBuilder()
                          .WithName(model.Name)
            ;

            return(builder.Build());
        }
コード例 #6
0
ファイル: UnravelEntry.cs プロジェクト: oisnull/hwl-dc
        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);
        }
コード例 #7
0
ファイル: UpdateTests.cs プロジェクト: ylvict/Timor.Cms
        public async Task ShouldUpdateFailedWhenEntityNotExists()
        {
            var exception = await Assert.ThrowsAsync <KeyNotFoundException>(async() =>
            {
                await _repository.UpdateAsync(Mapper.Map <Article>(ArticleBuilder.Build()));
            });

            Assert.NotNull(exception.Message);
        }
コード例 #8
0
ファイル: ArticleBuilderTest.cs プロジェクト: kaga/Pos
 public void testMissingBaseUnitPrice()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() =>
     {
         var builder = new ArticleBuilder();
         builder.SetProductCode(productCode: "A");
         builder.BuildArticle();
     });
 }
コード例 #9
0
ファイル: ArticleBuilderTest.cs プロジェクト: kaga/Pos
 public void testMissingProductCode()
 {
     Assert.Throws <InvalidOperationException>(() =>
     {
         var builder = new ArticleBuilder();
         builder.SetUnitPrice(unitPrice: 1.25M);
         builder.BuildArticle();
     });
 }
コード例 #10
0
        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();
        }
コード例 #11
0
ファイル: GetByIdTests.cs プロジェクト: ylvict/Timor.Cms
        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();
        }
コード例 #12
0
        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();
        }
コード例 #13
0
ファイル: ArticleBuilderTest.cs プロジェクト: kaga/Pos
        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));
        }
コード例 #14
0
        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();
        }
コード例 #15
0
        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");
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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);
        }
コード例 #18
0
ファイル: ArticleBuilderTest.cs プロジェクト: kaga/Pos
        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));
        }
コード例 #19
0
        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();
        }
コード例 #20
0
        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();
        }
コード例 #21
0
        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);
        }
コード例 #22
0
        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);
        }
コード例 #23
0
ファイル: UpdateTests.cs プロジェクト: ylvict/Timor.Cms
        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);
        }
コード例 #24
0
        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);
        }
コード例 #25
0
        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();
        }
コード例 #26
0
        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));
        }
コード例 #27
0
        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);
        }
コード例 #28
0
        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");
        }
コード例 #29
0
ファイル: ArticleBuilderTest.cs プロジェクト: kaga/Pos
        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");
        }
コード例 #30
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);
        }