public void ShouldProduceCalculateStatisticsOperationForCategoryStatisticsDto()
        {
            // Arrange
            var repositoryFactory = new VerifiableRepositoryFactory();
            var dto = new CategoryStatisticsDto
            {
                ProjectId  = 1,
                Categories = new[]
                {
                    new CategoryStatisticsDto.CategoryDto
                    {
                        CategoryId       = 2,
                        AdvertisersCount = 3,
                    }
                }
            };

            SourceDb.Has(new Bit::ProjectCategoryStatistics {
                ProjectId = 1, CategoryId = 7
            },
                         new Bit::ProjectCategoryStatistics {
                ProjectId = 2, CategoryId = 7
            });

            var metadataSource = new ImportStatisticsMetadataSource();
            IMetadataElement importStatisticsMetadata;

            if (!metadataSource.Metadata.Values.TryGetElementById(new Uri(typeof(CategoryStatisticsDto).Name, UriKind.Relative), out importStatisticsMetadata))
            {
                throw new NotSupportedException($"The aggregate of type '{typeof(CategoryStatisticsDto).Name}' is not supported.");
            }

            var importer = new StatisticsFactImporter <Bit::ProjectCategoryStatistics, CategoryStatisticsDto>(
                (ImportStatisticsMetadata <Bit::ProjectCategoryStatistics, CategoryStatisticsDto>)importStatisticsMetadata,
                Query,
                repositoryFactory.Create <Bit::ProjectCategoryStatistics>());

            // Act
            var operations = importer.Import(dto).ToArray();

            // Assert
            Assert.That(operations.Count(), Is.EqualTo(1));
            repositoryFactory.Verify <Bit::ProjectCategoryStatistics>(
                m => m.Delete(It.Is(Predicate.Match(new Bit::ProjectCategoryStatistics {
                ProjectId = 1, CategoryId = 7
            }))),
                Times.AtLeastOnce);
            repositoryFactory.Verify <Bit::ProjectCategoryStatistics>(
                m => m.Add(It.Is(Predicate.Match(new Bit::ProjectCategoryStatistics {
                ProjectId = 1, CategoryId = 2, AdvertisersCount = 3
            }))),
                Times.AtLeastOnce);
        }
        private bool TryParseRubricPopularity(XElement xml, out IDataTransferObject dto)
        {
            var branchElement = xml.Element("Branch");

            if (branchElement == null)
            {
                dto = null;
                return(false);
            }

            try
            {
                dto = new CategoryStatisticsDto
                {
                    ProjectId  = (long)branchElement.Attribute("Code"),
                    Categories = xml.Descendants("Rubric").Select(x =>
                    {
                        var advFirmCountAttr = x.Attribute("AdvFirmCount");
                        if (advFirmCountAttr == null)
                        {
                            throw new ArgumentException();
                        }

                        var rubricDto = new CategoryStatisticsDto.CategoryDto
                        {
                            CategoryId       = (long)x.Attribute("Code"),
                            AdvertisersCount = (long)advFirmCountAttr
                        };

                        return(rubricDto);
                    }).ToArray()
                };

                return(true);
            }
            catch (ArgumentException)
            {
                _tracer.Warn("Skip RubricPopularity message due to unsupported format");
                dto = null;
                return(false);
            }
        }
        public void ShouldProduceCalculateStatisticsOperationForCategoryStatisticsDto()
        {
            // Arrange
            var repositoryFactory = new VerifiableRepositoryFactory();
            var dto = new CategoryStatisticsDto
            {
                ProjectId  = 1,
                Categories = new[]
                {
                    new CategoryStatisticsDto.CategoryDto
                    {
                        CategoryId       = 2,
                        AdvertisersCount = 3,
                    }
                }
            };

            SourceDb.Has(new Bit::ProjectCategoryStatistics {
                ProjectId = 1, CategoryId = 7
            },
                         new Bit::ProjectCategoryStatistics {
                ProjectId = 2, CategoryId = 7
            });

            var importer = CreateProcessor <CategoryStatisticsDto, Bit::ProjectCategoryStatistics>(repositoryFactory);

            // Act
            var operations = importer.Import(dto).ToArray();

            // Assert
            Assert.That(operations.Count(), Is.EqualTo(1));
            repositoryFactory.Verify <Bit::ProjectCategoryStatistics>(
                m => m.Delete(It.Is(Predicate.Match(new Bit::ProjectCategoryStatistics {
                ProjectId = 1, CategoryId = 7
            }))),
                Times.AtLeastOnce);
            repositoryFactory.Verify <Bit::ProjectCategoryStatistics>(
                m => m.Add(It.Is(Predicate.Match(new Bit::ProjectCategoryStatistics {
                ProjectId = 1, CategoryId = 2, AdvertisersCount = 3
            }))),
                Times.AtLeastOnce);
        }
        public void ShouldMapFromCategoryStatisticsDto()
        {
            var dto = new CategoryStatisticsDto
            {
                ProjectId  = 1,
                Categories = new[]
                {
                    new CategoryStatisticsDto.CategoryDto
                    {
                        CategoryId       = 2,
                        AdvertisersCount = 3,
                    }
                }
            };

            var entities = Specs.Map.Bit.ProjectCategoryStatistics().Map(dto);

            Assert.That(entities.Count(), Is.EqualTo(1));
            Assert.That(entities.Single().ProjectId, Is.EqualTo(1));
            Assert.That(entities.Single().CategoryId, Is.EqualTo(2));
            Assert.That(entities.Single().AdvertisersCount, Is.EqualTo(3));
        }