public void GivenCsvRowDataCollection_WhenContainsEmptyExtensionContentId_ShouldBuildResultDataLikeSimpleArticle(int contentId, int articleId)
        {
            // Fixture setup
            var contentRepository = _fixture.Freeze <Mock <IContentRepository> >();
            var fieldRepository   = _fixture.Freeze <Mock <IFieldRepository> >();
            var articleService    = _fixture.Freeze <Mock <IBatchUpdateService> >();
            var sut = _fixture.Create <CsvDbUpdateService>();

            var dbFields        = _fixture.CreateMany <Field>().ToList();
            var simpleRowFields = CsvDbUpdateTestHelpers.GenerateCsvRowFields(articleId, dbFields, _fixture);

            var classifierDbField      = _postProcessFieldComposer.With(f => f.IsClassifier, true).Create();
            var emptyClasifierCsvField = _fixture.Build <CsvDbUpdateFieldModel>()
                                         .With(f => f.Name, classifierDbField.Name)
                                         .With(f => f.Value, string.Empty)
                                         .Create();

            dbFields.Add(classifierDbField);
            simpleRowFields.Add(emptyClasifierCsvField);

            fieldRepository
            .Setup(m => m.GetByNames(contentId, It.Is(CsvDbUpdateTestHelpers.CompareStringCollections(simpleRowFields.Select(csvf => csvf.Name)))))
            .Returns(dbFields)
            .Verifiable();

            var csvData        = CsvDbUpdateTestHelpers.GenerateCsvDbUpdateModel(contentId, simpleRowFields, _fixture);
            var expectedResult = new List <ArticleData>
            {
                new ArticleData
                {
                    Id        = -articleId,
                    ContentId = contentId,
                    Fields    = dbFields.Zip(simpleRowFields.Skip(1), (dbf, csvf) => new FieldData
                    {
                        Id         = dbf.Id,
                        Value      = csvf.Value,
                        ArticleIds = Enumerable.Empty <int>().ToArray()
                    }).ToList()
                }
            };

            // Exercise system
            sut.Process(csvData);

            // Verify outcome
            fieldRepository.Verify();
            contentRepository.Verify(m => m.GetById(It.IsAny <int>()), Times.Never);
            articleService.Verify(m => m.BatchUpdate(It.Is(CsvDbUpdateTestHelpers.CompareArticleDataCollections(expectedResult))));
        }
        public void GivenCsvRowDataCollection_WhenWithoutExtensionsAndRelations_ShouldCallServicesWithCorrectlyFormattedData(int contentId, int articleId)
        {
            // Fixture setup
            var contentRepository = _fixture.Freeze <Mock <IContentRepository> >();
            var fieldRepository   = _fixture.Freeze <Mock <IFieldRepository> >();
            var articleService    = _fixture.Freeze <Mock <IBatchUpdateService> >();
            var sut = _fixture.Create <CsvDbUpdateService>();

            var dbFields     = _fixture.CreateMany <Field>().ToList();
            var csvRowFields = CsvDbUpdateTestHelpers.GenerateCsvRowFields(articleId, dbFields, _fixture);
            var csvData      = CsvDbUpdateTestHelpers.GenerateCsvDbUpdateModel(contentId, csvRowFields, _fixture);

            fieldRepository
            .Setup(m => m.GetByNames(contentId, It.Is(CsvDbUpdateTestHelpers.CompareStringCollections(csvRowFields.Select(csvf => csvf.Name)))))
            .Returns(dbFields)
            .Verifiable();

            var expectedResult = new List <ArticleData>
            {
                new ArticleData
                {
                    Id        = -articleId,
                    ContentId = contentId,
                    Fields    = dbFields.Zip(csvRowFields.Skip(1), (dbf, csvf) => new FieldData
                    {
                        Id    = dbf.Id,
                        Value = csvf.Value
                    }).ToList()
                }
            };

            // Exercise system
            sut.Process(csvData);

            // Verify outcome
            fieldRepository.Verify();
            contentRepository.Verify(m => m.GetById(It.IsAny <int>()), Times.Never);
            articleService.Verify(m => m.BatchUpdate(It.Is(CsvDbUpdateTestHelpers.CompareArticleDataCollections(expectedResult))));
        }
        public void GivenCsvRowDataCollection_WhenWithDbRelations_ShouldCallServicesWithCorrectlyFormattedData(
            int contentId,
            int articleId,
            int relatedArticleId,
            Generator <FieldExactTypes> fetGenerator)
        {
            // Fixture setup
            var articleRepository = _fixture.Freeze <Mock <IArticleRepository> >();
            var fieldRepository   = _fixture.Freeze <Mock <IFieldRepository> >();
            var articleService    = _fixture.Freeze <Mock <IBatchUpdateService> >();
            var sut = _fixture.Create <CsvDbUpdateService>();

            var dbFields     = _fixture.CreateMany <Field>().ToList();
            var csvRowFields = CsvDbUpdateTestHelpers.GenerateCsvRowFields(articleId, dbFields, _fixture);
            var m2mDbField   = _postProcessFieldComposer.With(f => f.ExactType, fetGenerator.First(fet => new[]
            {
                FieldExactTypes.O2MRelation,
                FieldExactTypes.M2ORelation,
                FieldExactTypes.M2MRelation
            }.Contains(fet))).Create();

            var m2mCsvField = _fixture.Build <CsvDbUpdateFieldModel>()
                              .With(f => f.Name, m2mDbField.Name)
                              .With(f => f.Value, relatedArticleId.ToString())
                              .Create();

            dbFields.Add(m2mDbField);
            csvRowFields.Add(m2mCsvField);
            var csvData = CsvDbUpdateTestHelpers.GenerateCsvDbUpdateModel(contentId, csvRowFields, _fixture);

            fieldRepository
            .Setup(m => m.GetByNames(contentId, It.Is(CsvDbUpdateTestHelpers.CompareStringCollections(csvRowFields.Select(csvf => csvf.Name)))))
            .Returns(dbFields)
            .Verifiable();

            articleRepository
            .Setup(m => m.IsExist(relatedArticleId))
            .Returns(true)
            .Verifiable();

            var expectedResult = new List <ArticleData>
            {
                new ArticleData
                {
                    Id        = -articleId,
                    ContentId = contentId,
                    Fields    = dbFields.Zip(csvRowFields.Skip(1), (dbf, csvf) => new FieldData
                    {
                        Id         = dbf.Id,
                        Value      = csvf.Value,
                        ArticleIds = dbf.ExactType == m2mDbField.ExactType ? new[] { relatedArticleId } : Enumerable.Empty <int>().ToArray()
                    }).ToList()
                }
            };

            // Exercise system
            sut.Process(csvData, false);

            // Verify outcome
            fieldRepository.Verify();
            articleRepository.Verify();
            articleService.Verify(m => m.BatchUpdate(It.Is(CsvDbUpdateTestHelpers.CompareArticleDataCollections(expectedResult)), false));
        }
        public void GivenCsvRowDataCollection_WhenContainsExtension_ShouldCallServicesWithCorrectlyFormattedData(int contentId, int articleId, int externalContentId, int externalArticleId, string externalContentName)
        {
            // Fixture setup
            var contentRepository = _fixture.Freeze <Mock <IContentRepository> >();
            var fieldRepository   = _fixture.Freeze <Mock <IFieldRepository> >();
            var articleService    = _fixture.Freeze <Mock <IBatchUpdateService> >();
            var sut = _fixture.Create <CsvDbUpdateService>();

            var dbFields        = _fixture.CreateMany <Field>().ToList();
            var simpleRowFields = CsvDbUpdateTestHelpers.GenerateCsvRowFields(articleId, dbFields, _fixture);

            var classifierDbField = _postProcessFieldComposer.With(f => f.IsClassifier, true).Create();
            var clasifierCsvField = _fixture.Build <CsvDbUpdateFieldModel>()
                                    .With(f => f.Name, classifierDbField.Name)
                                    .With(f => f.Value, externalContentId.ToString())
                                    .Create();

            dbFields.Add(classifierDbField);
            simpleRowFields.Add(clasifierCsvField);

            var externalDbFields  = _fixture.CreateMany <Field>().ToList();
            var externalRowFields = CsvDbUpdateTestHelpers.GenerateCsvRowFields(externalArticleId, externalDbFields, _fixture, externalContentName);

            var csvRowFields = simpleRowFields.Concat(externalRowFields).ToList();
            var csvData      = CsvDbUpdateTestHelpers.GenerateCsvDbUpdateModel(contentId, csvRowFields, _fixture);

            fieldRepository
            .Setup(m => m.GetByNames(contentId, It.Is(CsvDbUpdateTestHelpers.CompareStringCollections(csvRowFields.Select(csvf => csvf.Name)))))
            .Returns(dbFields)
            .Verifiable();

            fieldRepository
            .Setup(m => m.GetByNames(externalContentId, It.Is(CsvDbUpdateTestHelpers.CompareStringCollections(externalRowFields.Select(csvf => CsvDbUpdateTestHelpers.FilterFromPrefix(csvf.Name, externalContentName))))))
            .Returns(externalDbFields)
            .Verifiable();

            contentRepository
            .Setup(m => m.GetById(externalContentId))
            .Returns(new Content {
                Name = externalContentName
            })
            .Verifiable();

            var expectedResult = new List <ArticleData>
            {
                new ArticleData
                {
                    Id        = -articleId,
                    ContentId = contentId,
                    Fields    = dbFields.Zip(simpleRowFields.Skip(1), (dbf, csvf) => new FieldData
                    {
                        Id         = dbf.Id,
                        Value      = csvf.Value,
                        ArticleIds = dbf.IsClassifier ? new[] { -externalArticleId } : Enumerable.Empty <int>().ToArray()
                    }).ToList()
                },
                new ArticleData
                {
                    Id        = -externalArticleId,
                    ContentId = externalContentId,
                    Fields    = externalDbFields.Zip(externalRowFields.Skip(1), (dbf, csvf) => new FieldData
                    {
                        Id         = dbf.Id,
                        Value      = csvf.Value,
                        ArticleIds = Enumerable.Empty <int>().ToArray()
                    }).ToList()
                }
            };

            // Exercise system
            sut.Process(csvData);

            // Verify outcome
            fieldRepository.Verify();
            contentRepository.Verify(m => m.GetById(It.IsAny <int>()), Times.Exactly(2));
            articleService.Verify(m => m.BatchUpdate(It.Is(CsvDbUpdateTestHelpers.CompareArticleDataCollections(expectedResult))));
        }