예제 #1
0
        public Bcr Read()
        {
            var tier3Hierarchy = _hierarchy.GetHierarchyByTier3();

            var cachedLines       = GetCachedLines();
            var cachedCostCentres = cachedLines.Select(x => x.CostCentre.Code).Distinct();

            var hierarchyToFetch = _updateCache
                ? tier3Hierarchy
                : tier3Hierarchy.Where(x => x.Any(y => !cachedCostCentres.Contains(y.Code)));

            if (!hierarchyToFetch.Any())
            {
                return(new Bcr(cachedLines));
            }

            var fetchedLines = new BcrReport(_factory, _log).RunBcr(hierarchyToFetch).Lines;

            var fetchedCostCentres    = fetchedLines.Select(x => x.CostCentre.Code).Distinct();
            var cachedLinesNotUpdated = cachedLines.Where(x => !fetchedCostCentres.Contains(x.CostCentre.Code));

            var bcr = new Bcr(fetchedLines.Union(cachedLinesNotUpdated));

            _bcrFile.Write(bcr);
            return(bcr);
        }
예제 #2
0
        public void GivenNoTierOption_ThenAllLinesShouldBeIncluded([Values] A.Criteria criteria)
        {
            var filter = A.BcrFilter().Build();

            var bcr = new Bcr(new BcrLine[] { A.BcrLine().With(criteria, "tier") });

            Assert.That(filter.Use(bcr).Lines.ToList(), Has.Count.EqualTo(1));
        }
예제 #3
0
        public void GivenTierOption_ThenLinesNotMatchingThatTierShouldNotBeIncluded([Values] Criteria criteria)
        {
            var filter = A.BcrFilter().With(criteria, "tier").Build();

            var bcr = new Bcr(new BcrLine[] { A.BcrLine().With(criteria, "notTheRightTier") });

            Assert.That(filter.Use(bcr).Lines, Is.Empty);
        }
예제 #4
0
        public void GivenTwoLines_ThenTheyShouldBeSorted()
        {
            var bcr = new Bcr(new BcrLine[] { A.BcrLine().Account("B"), A.BcrLine().Account("A") });

            var orderedLines = new BcrLine[] { A.BcrLine().Account("A"), A.BcrLine().Account("B") };

            Assert.That(A.BcrFilter().Build().Use(bcr).Lines, Is.EqualTo(orderedLines));
        }
예제 #5
0
        public void CanSerializeThenDeserializeTypeBcr()
        {
            var obj = new Bcr(Enumerable.Empty <BcrLine>());

            var json = JsonConvert.SerializeObject(obj);

            Assert.That(() => JsonConvert.DeserializeObject <Bcr>(json), Throws.Nothing);
        }
예제 #6
0
        public void GivenTierOptionWithMultipleValues_ThenLinesMatchingThatAnyOfThoseValuesShouldBeIncluded(
            [Values] Criteria criteria)
        {
            var filter = A.BcrFilter().With(criteria, "firstTier", "secondTier").Build();

            var firstBcrLine  = A.BcrLine().With(criteria, "firstTier").Build();
            var secondBcrLine = A.BcrLine().With(criteria, "secondTier").Build();
            var thirdBcrLine  = A.BcrLine().With(criteria, "thirdTier").Build();

            var bcr = new Bcr(new[] { firstBcrLine, secondBcrLine, thirdBcrLine });

            Assert.That(filter.Use(bcr).Lines.ToList(), Is.EquivalentTo(new[] { firstBcrLine, secondBcrLine }));
        }
예제 #7
0
        public void GivenMatchOnNoTier_ThenTheLineShouldNotBeIncluded()
        {
            var allCriteria = (Criteria[])Enum.GetValues(typeof(Criteria));

            var filter =
                allCriteria.Aggregate(A.BcrFilter(), (builder, c) => builder.With(c, "1")).Build();

            var bcrLine =
                allCriteria.Aggregate(A.BcrLine(), (builder, c) => builder.With(c, "0")).Build();

            var bcr = new Bcr(new[] { bcrLine });

            Assert.That(filter.Use(bcr).Lines.ToList(), Is.Empty);
        }
예제 #8
0
        public void GivenMatchOnAtLeastOneTier_ThenTheLineShouldBeIncluded(IEnumerable <Criteria> criteria)
        {
            var tiers = (Criteria[])Enum.GetValues(typeof(Criteria));

            var filter = tiers.Aggregate(A.BcrFilter(), (builder, t) => builder.With(t, "1")).Build();

            var blankLine = tiers.Aggregate(A.BcrLine(), (builder, t) => builder.With(t, "0"));

            var bcrLine = criteria.Aggregate(blankLine, (builder, t) => builder.With(t, "1")).Build();

            var bcr = new Bcr(new[] { bcrLine });

            Assert.That(filter.Use(bcr).Lines.ToList(), Is.EquivalentTo(new[] { bcrLine }));
        }
        public void GivenBcrFromMiddleware_ThenThatBcrShouldBePassedToTheWriter()
        {
            var bcr = new Bcr(Enumerable.Empty <BcrLine>());

            var middleware = new Mock <IBcrMiddleware>();

            middleware.Setup(x => x.Use(It.IsAny <Bcr>())).Returns(bcr);

            var writer = new Mock <IBcrWriter>();
            var runner = Create(middleware: middleware.Object, writer: writer.Object);

            runner.Run();

            writer.Verify(x => x.Write(It.IsAny <string>(), bcr), Times.Once);
        }
        public void GivenBcrFromReader_ThenThatBcrShouldBePassedToTheMiddleware()
        {
            var bcr = new Bcr(Enumerable.Empty <BcrLine>());

            var reader = new Mock <IBcrReader>();

            reader.Setup(x => x.Read()).Returns(bcr);

            var middleware = new Mock <IBcrMiddleware>();

            var runner = Create(reader: reader.Object, middleware: middleware.Object);

            runner.Run();

            middleware.Verify(x => x.Use(bcr), Times.Once);
        }
예제 #11
0
        public void Write(string path, Bcr bcr)
        {
            var app      = new MSExcel.Application();
            var workbook = app.Workbooks.Add();

            var sheet = workbook.Sheets[1] as MSExcel.Worksheet;

            sheet.Name = Guid.NewGuid().ToString("N").Substring(0, 16);

            var headerRow = 2;

            AddHeader(sheet, headerRow);

            var rowToStartData = headerRow + 1;

            var data = bcr.Lines.ToList();

            Parallel.For(
                0,
                data.Count,
                new ParallelOptions {
                MaxDegreeOfParallelism = 3
            },
                i =>
            {
                var rowNumber = i + rowToStartData;
                AddRow(sheet, rowNumber, data.ElementAt(i));
            });

            SetNumberFormat(
                sheet.Range[sheet.Cells[rowToStartData, 13], sheet.Cells[rowToStartData + data.Count - 1, 18]]);

            var lastRow = rowToStartData + data.Count - 1;

            AutoFilter(sheet.Range[sheet.Cells[headerRow, 1], sheet.Cells[lastRow, 18]]);

            AddSubtotals(sheet, 1, rowToStartData, lastRow);

            sheet.Columns.AutoFit();

            workbook.SaveAs(path);
            workbook.Close();
            app.Quit();
        }
예제 #12
0
 public Bcr Use(Bcr bcr)
 {
     return(new Bcr(bcr.Lines.Where(x => Matches(x.CostCentre)).OrderBy(x => x).ToList()));
 }