public void ValuesForLastMonthOfStatusDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsBudgetInfoValuesWherePostedIsEqualToZero()
        {
            IBudgetInfoCollection sut = CreateSut();

            IBudgetInfoValues result = sut.ValuesForLastMonthOfStatusDate;

            Assert.That(result.Posted, Is.EqualTo(0M));
        }
        public async Task CalculateAsync_WhenCalled_ReturnsBudgetInfoCollection()
        {
            IBudgetInfoCollection sut = CreateSut();

            IBudgetInfoCollection result = await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1));

            Assert.That(result, Is.SameAs(sut));
        }
        public void ValuesForLastMonthOfStatusDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsNotNull()
        {
            IBudgetInfoCollection sut = CreateSut();

            IBudgetInfoValues result = sut.ValuesForLastMonthOfStatusDate;

            Assert.That(result, Is.Not.Null);
        }
        public BudgetAccount(IAccounting accounting, string accountNumber, string accountName, IBudgetAccountGroup budgetAccountGroup, IBudgetInfoCollection budgetInfoCollection, IPostingLineCollection postingLineCollection)
            : base(accounting, accountNumber, accountName, postingLineCollection)
        {
            NullGuard.NotNull(budgetAccountGroup, nameof(budgetAccountGroup))
            .NotNull(budgetInfoCollection, nameof(budgetInfoCollection));

            BudgetAccountGroup   = budgetAccountGroup;
            BudgetInfoCollection = budgetInfoCollection;
        }
Beispiel #5
0
        public void ValuesForMonthOfStatusDate_WhenCalled_ReturnsSameBudgetInfoValuesAsValuesForMonthOfStatusDateOnBudgetInfoCollection()
        {
            IBudgetInfoCollection budgetInfoCollection = _fixture.BuildBudgetInfoCollectionMock().Object;
            IBudgetAccount        sut = CreateSut(budgetInfoCollection);

            IBudgetInfoValues result = sut.ValuesForMonthOfStatusDate;

            Assert.That(result, Is.SameAs(budgetInfoCollection.ValuesForMonthOfStatusDate));
        }
        public async Task ApplyCalculationAsync_WhenCalledOnPostingLineWhereBudgetAccountValuesAtPostingDateWasNotGivenAndCalculatedBudgetAccountDoesNotHaveBudgetInfoForPostingDate_ReturnsSamePostingLineWhereBudgetOnBudgetAccountValuesAtPostingDateEqualToZero()
        {
            IPostingLine sut = CreateSut();

            IBudgetInfoCollection budgetInfoCollection    = _fixture.BuildBudgetInfoCollectionMock(hasBudgetInfoForFind: false).Object;
            IBudgetAccount        calculatedBudgetAccount = _fixture.BuildBudgetAccountMock(budgetInfoCollection: budgetInfoCollection).Object;
            IPostingLine          result = await sut.ApplyCalculationAsync(calculatedBudgetAccount);

            Assert.That(result.BudgetAccountValuesAtPostingDate.Budget, Is.EqualTo(0M));
        }
        public async Task CalculateAsync_WhenCalled_ReturnsSameBudgetAccountWhereBudgetInfoCollectionIsEqualToCalculatedBudgetInfoCollection()
        {
            IBudgetInfoCollection calculatedBudgetInfoCollection = _fixture.BuildBudgetInfoCollectionMock().Object;
            IBudgetInfoCollection budgetInfoCollection           = _fixture.BuildBudgetInfoCollectionMock(calculatedBudgetInfoCollection: calculatedBudgetInfoCollection).Object;
            IBudgetAccount        sut = CreateSut(budgetInfoCollection);

            IBudgetAccount result = await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1));

            Assert.That(result.BudgetInfoCollection, Is.EqualTo(calculatedBudgetInfoCollection));
        }
        internal static void EnsurePopulation(this IBudgetInfoCollection budgetInfoCollection, IBudgetAccount budgetAccount)
        {
            NullGuard.NotNull(budgetInfoCollection, nameof(budgetInfoCollection))
            .NotNull(budgetAccount, nameof(budgetAccount));

            budgetInfoCollection.EnsurePopulation <IBudgetInfo, IBudgetInfoCollection>(budgetInfo =>
            {
                DateTime nextBudgetInfoFromDate = budgetInfo.ToDate.AddDays(1);
                return(new BudgetInfo(budgetAccount, (short)nextBudgetInfoFromDate.Year, (short)nextBudgetInfoFromDate.Month, budgetInfo.Income, budgetInfo.Expenses));
            });
        }
        public async Task CalculateAsync_WhenBudgetInfoCollectionContainBudgetInfoForLastYearOfStatusDate_ReturnsBudgetInfoCollectionWherePostInValuesForLastYearOfStatusDateIsEqualToSumOfPostedFromBudgetInfoForLastYearOfStatusDate()
        {
            IBudgetInfoCollection sut = CreateSut();

            DateTime budgetInfoOffset = DateTime.Today.AddDays(_random.Next(1, _random.Next(1, 365)) * -1);
            IEnumerable <IBudgetInfo> budgetInfoCollection = new List <IBudgetInfo>
            {
                _fixture.BuildBudgetInfoMock(budgetInfoOffset, isLastYearOfStatusDate: true).Object,
                _fixture.BuildBudgetInfoMock(budgetInfoOffset.AddMonths(-1), isLastYearOfStatusDate: true).Object,
                _fixture.BuildBudgetInfoMock(budgetInfoOffset.AddMonths(-2)).Object
            };

            sut.Add(budgetInfoCollection);

            IBudgetInfoCollection result = await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1));

            Assert.That(result.ValuesForLastYearOfStatusDate.Posted, Is.EqualTo(budgetInfoCollection.Where(budgetInfo => budgetInfo.IsLastYearOfStatusDate).Sum(budgetInfo => budgetInfo.Posted)));
        }
        public async Task CalculateAsync_WhenBudgetInfoCollectionDoesNotContainBudgetInfoForLastYearOfStatusDate_ReturnsBudgetInfoCollectionWhereBudgetInValuesForLastYearOfStatusDateIsEqualToZero()
        {
            IBudgetInfoCollection sut = CreateSut();

            DateTime budgetInfoOffset = DateTime.Today.AddDays(_random.Next(1, _random.Next(1, 365)) * -1);
            IEnumerable <IBudgetInfo> budgetInfoCollection = new List <IBudgetInfo>
            {
                _fixture.BuildBudgetInfoMock(budgetInfoOffset).Object,
                _fixture.BuildBudgetInfoMock(budgetInfoOffset.AddMonths(-1)).Object,
                _fixture.BuildBudgetInfoMock(budgetInfoOffset.AddMonths(-2)).Object
            };

            sut.Add(budgetInfoCollection);

            IBudgetInfoCollection result = await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1));

            Assert.That(result.ValuesForLastYearOfStatusDate.Budget, Is.EqualTo(0M));
        }
        public async Task CalculateAsync_WhenBudgetInfoCollectionContainBudgetInfoForYearToDateOfStatusDate_ReturnsBudgetInfoCollectionWhereValuesForYearToDateOfStatusDateIsNotNull()
        {
            IBudgetInfoCollection sut = CreateSut();

            DateTime budgetInfoOffset = DateTime.Today.AddDays(_random.Next(1, _random.Next(1, 365)) * -1);
            IEnumerable <IBudgetInfo> budgetInfoCollection = new List <IBudgetInfo>
            {
                _fixture.BuildBudgetInfoMock(budgetInfoOffset, isYearToDateOfStatusDate: true).Object,
                _fixture.BuildBudgetInfoMock(budgetInfoOffset.AddMonths(-1), isYearToDateOfStatusDate: true).Object,
                _fixture.BuildBudgetInfoMock(budgetInfoOffset.AddMonths(-2)).Object
            };

            sut.Add(budgetInfoCollection);

            IBudgetInfoCollection result = await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1));

            Assert.That(result.ValuesForYearToDateOfStatusDate, Is.Not.Null);
        }
        public async Task UpdateBudgetAccount_WhenCalledWithAccountingNumberAndAccountNumberForExistingBudgetAccount_ReturnsPartialViewResultWhereModelIsBudgetAccountViewModelWithBudgetInfosContainingDataFromBudgetInfoCollectionOnBudgetAccountFromQueryBus()
        {
            IBudgetInfo[]         budgetInfos          = _fixture.BuildBudgetInfoCollectionMock().Object.ToArray();
            IBudgetInfoCollection budgetInfoCollection = _fixture.BuildBudgetInfoCollectionMock(budgetInfoCollection: budgetInfos).Object;
            IBudgetAccount        budgetAccount        = _fixture.BuildBudgetAccountMock(budgetInfoCollection: budgetInfoCollection).Object;
            Controller            sut = CreateSut(budgetAccount: budgetAccount);

            PartialViewResult result = (PartialViewResult)await sut.UpdateBudgetAccount(_fixture.Create <int>(), _fixture.Create <string>());

            BudgetAccountViewModel budgetAccountViewModel = (BudgetAccountViewModel)result.Model;

            foreach (IGrouping <short, IBudgetInfo> group in budgetInfos.GroupBy(budgetInfo => budgetInfo.Year))
            {
                Assert.That(budgetAccountViewModel.BudgetInfos.ContainsKey(group.Key), Is.True);

                BudgetInfoCollectionViewModel budgetInfoCollectionViewModel = budgetAccountViewModel.BudgetInfos[group.Key];
                Assert.That(budgetInfoCollectionViewModel, Is.Not.Null);
                Assert.That(budgetInfoCollectionViewModel.All(budgetInfoViewModel => group.SingleOrDefault(m => m.Month == budgetInfoViewModel.Month) != null), Is.True);
            }
        }
        public async Task CalculateAsync_WhenCalled_AssertIsYearToDateOfStatusDateWasCalledOnEachBudgetInfoInBudgetInfoCollection()
        {
            IBudgetInfoCollection sut = CreateSut();

            DateTime budgetInfoOffset = DateTime.Today.AddDays(_random.Next(1, _random.Next(1, 365)) * -1);
            IEnumerable <Mock <IBudgetInfo> > budgetInfoMockCollection = new List <Mock <IBudgetInfo> >
            {
                _fixture.BuildBudgetInfoMock(budgetInfoOffset, isYearToDateOfStatusDate: true),
                _fixture.BuildBudgetInfoMock(budgetInfoOffset.AddMonths(-1), isYearToDateOfStatusDate: true),
                _fixture.BuildBudgetInfoMock(budgetInfoOffset.AddMonths(-2))
            };

            sut.Add(budgetInfoMockCollection.Select(budgetInfoMock => budgetInfoMock.Object).ToArray());

            await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1));

            foreach (Mock <IBudgetInfo> budgetInfoMock in budgetInfoMockCollection)
            {
                budgetInfoMock.Verify(m => m.IsYearToDateOfStatusDate, Times.Once);
            }
        }
        internal static void Populate(this IBudgetInfoCollection budgetInfoCollection, IBudgetAccount budgetAccount, IBudgetInfo[] budgetInfos, DateTime statusDate, DateTime statusDateForBudgetInfos)
        {
            NullGuard.NotNull(budgetInfoCollection, nameof(budgetInfoCollection))
            .NotNull(budgetAccount, nameof(budgetAccount))
            .NotNull(budgetInfos, nameof(budgetInfos));

            DateTime fromDate = new DateTime(statusDate.AddYears(-1).Year, 1, 1);

            if (budgetInfos.Length == 0)
            {
                budgetInfoCollection.Add(BuildBudgetInfo(budgetAccount, (short)fromDate.Year, (short)fromDate.Month));
                budgetInfoCollection.Add(BuildBudgetInfo(budgetAccount, (short)statusDateForBudgetInfos.Year, (short)statusDateForBudgetInfos.Month));

                budgetInfoCollection.EnsurePopulation(budgetAccount);

                return;
            }

            IBudgetInfo budgetInfoForFromDate = budgetInfos.FindInfoForFromDate(fromDate,
                                                                                (year, month, budgetInfoBeforeFromDate) => BuildBudgetInfo(budgetAccount, year, month, budgetInfoBeforeFromDate),
                                                                                (year, month) => BuildBudgetInfo(budgetAccount, year, month));

            budgetInfoCollection.Add(budgetInfoForFromDate);

            budgetInfoCollection.Add(budgetInfos.Between(budgetInfoForFromDate.ToDate.AddDays(1), statusDateForBudgetInfos));

            IBudgetInfo lastBudgetInfo = budgetInfoCollection.Last();

            if (lastBudgetInfo == null || lastBudgetInfo.Year > (short)statusDateForBudgetInfos.Year || lastBudgetInfo.Year == (short)statusDateForBudgetInfos.Year && lastBudgetInfo.Month >= (short)statusDateForBudgetInfos.Month)
            {
                budgetInfoCollection.EnsurePopulation(budgetAccount);

                return;
            }

            budgetInfoCollection.Add(BuildBudgetInfo(budgetAccount, (short)statusDateForBudgetInfos.Year, (short)statusDateForBudgetInfos.Month, lastBudgetInfo));

            budgetInfoCollection.EnsurePopulation(budgetAccount);
        }
        internal async Task CreateOrUpdateAsync(IBudgetInfoCollection budgetInfoCollection, BudgetAccountModel budgetAccountModel)
        {
            NullGuard.NotNull(budgetInfoCollection, nameof(budgetInfoCollection))
            .NotNull(budgetAccountModel, nameof(budgetAccountModel));

            IBudgetInfo budgetInfo = budgetInfoCollection.First();

            while (budgetInfo != null)
            {
                BudgetInfoModel currentBudgetInfoModel  = budgetAccountModel.BudgetInfos.SingleOrDefault(budgetInfoModel => budgetInfoModel.YearMonth.Year == budgetInfo.Year && budgetInfoModel.YearMonth.Month == budgetInfo.Month);
                BudgetInfoModel previousBudgetInfoModel = budgetAccountModel.BudgetInfos.Where(budgetInfoModel => budgetInfoModel.YearMonth.Year < budgetInfo.Year || budgetInfoModel.YearMonth.Year == budgetInfo.Year && budgetInfoModel.YearMonth.Month < budgetInfo.Month)
                                                          .OrderByDescending(budgetInfoModel => budgetInfoModel.YearMonth.Year)
                                                          .ThenByDescending(budgetInfoModel => budgetInfoModel.YearMonth.Month)
                                                          .FirstOrDefault();

                if (currentBudgetInfoModel != null)
                {
                    if (previousBudgetInfoModel != null && budgetInfo.Income == previousBudgetInfoModel.Income && budgetInfo.Expenses == previousBudgetInfoModel.Expenses)
                    {
                        budgetAccountModel.BudgetInfos.Remove(await OnDeleteAsync(currentBudgetInfoModel));

                        budgetInfo = budgetInfoCollection.Next(budgetInfo);
                        continue;
                    }

                    if (budgetInfo.Income == currentBudgetInfoModel.Income && budgetInfo.Expenses == currentBudgetInfoModel.Expenses)
                    {
                        budgetInfo = budgetInfoCollection.Next(budgetInfo);
                        continue;
                    }

                    await OnUpdateAsync(budgetInfo, currentBudgetInfoModel);

                    budgetInfo = budgetInfoCollection.Next(budgetInfo);
                    continue;
                }

                if (previousBudgetInfoModel != null)
                {
                    if (budgetInfo.Income == previousBudgetInfoModel.Income && budgetInfo.Expenses == previousBudgetInfoModel.Expenses)
                    {
                        budgetInfo = budgetInfoCollection.Next(budgetInfo);
                        continue;
                    }

                    await CreateAsync(budgetInfo, budgetAccountModel);

                    budgetInfo = budgetInfoCollection.Next(budgetInfo);
                    continue;
                }

                if (budgetInfo.Income == 0M && budgetInfo.Expenses == 0M)
                {
                    budgetInfo = budgetInfoCollection.Next(budgetInfo);
                    continue;
                }

                await CreateAsync(budgetInfo, budgetAccountModel);

                budgetInfo = budgetInfoCollection.Next(budgetInfo);
            }
        }
Beispiel #16
0
 private IBudgetAccount CreateSut(IBudgetInfoCollection budgetInfoCollection = null)
 {
     return(new Domain.Accounting.BudgetAccount(_fixture.BuildAccountingMock().Object, _fixture.Create <string>(), _fixture.Create <string>(), _fixture.BuildBudgetAccountGroupMock().Object, budgetInfoCollection ?? _fixture.BuildBudgetInfoCollectionMock().Object, _fixture.BuildPostingLineCollectionMock().Object));
 }