Beispiel #1
0
        public void Expenses_WhenSetterCalledWithValueEqualToZero_SetValueToZero()
        {
            IBudgetInfo sut = CreateSut();

            sut.Expenses = 0M;

            Assert.That(sut.Expenses, Is.EqualTo(0M));
        }
Beispiel #2
0
        public void ToDomain_WhenBudgetAccountIsNotNull_ReturnsBudgetInfo()
        {
            IBudgetInfoCommand sut = CreateSut();

            IBudgetInfo result = sut.ToDomain(_fixture.BuildBudgetAccountMock().Object);

            Assert.That(result, Is.TypeOf <BudgetInfo>());
        }
Beispiel #3
0
        public async Task CalculateAsync_WhenCalled_ReturnsSameBudgetInfo()
        {
            IBudgetInfo sut = CreateSut();

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

            Assert.That(result, Is.SameAs(sut));
        }
Beispiel #4
0
        private Mock <IBudgetInfoCommand> CreateBudgetInfoCommandMock(IBudgetInfo budgetInfo = null)
        {
            Mock <IBudgetInfoCommand> budgetInfoCommandMock = new Mock <IBudgetInfoCommand>();

            budgetInfoCommandMock.Setup(m => m.ToDomain(It.IsAny <IBudgetAccount>()))
            .Returns(budgetInfo ?? _fixture.BuildBudgetInfoMock().Object);
            return(budgetInfoCommandMock);
        }
        public void Income_WhenSetterCalledWithValueEqualToZero_SetValueToZero()
        {
            IBudgetInfo sut = CreateSut();

            sut.Income = 0M;

            Assert.That(sut.Income, Is.EqualTo(0M));
        }
        public void Income_WhenSetterCalledWithValueBelowZero_ThrowsArgumentException()
        {
            IBudgetInfo sut = CreateSut();

            ArgumentException result = Assert.Throws <ArgumentException>(() => sut.Income = Math.Abs(_fixture.Create <decimal>() + 0.75M) * -1);

            Assert.That(result.Message.StartsWith("The income value cannot be below 0."), Is.True);
            Assert.That(result.ParamName, Is.EqualTo("value"));
        }
Beispiel #7
0
        public void ToDomain_WhenBudgetAccountIsNotNull_ReturnsBudgetInfoWhereBudgetAccountIsEqualToArgument()
        {
            IBudgetInfoCommand sut = CreateSut();

            IBudgetAccount budgetAccount = _fixture.BuildBudgetAccountMock().Object;
            IBudgetInfo    result        = sut.ToDomain(budgetAccount);

            Assert.That(result.BudgetAccount, Is.EqualTo(budgetAccount));
        }
Beispiel #8
0
        public void ToDomain_WhenBudgetAccountIsNotNull_ReturnsBudgetInfoWhereYearIsEqualToYearInBudgetInfoCommand()
        {
            short year             = (short)_random.Next(BudgetInfo.MinYear, BudgetInfo.MaxYear);
            IBudgetInfoCommand sut = CreateSut(year);

            IBudgetInfo result = sut.ToDomain(_fixture.BuildBudgetAccountMock().Object);

            Assert.That(result.Year, Is.EqualTo(year));
        }
Beispiel #9
0
        public async Task CalculateAsync_WhenCalled_AssertPostingLineCollectionWasCalledOnBudgetAccount()
        {
            Mock <IBudgetAccount> budgetAccountMock = _fixture.BuildBudgetAccountMock();
            IBudgetInfo           sut = CreateSut(budgetAccountMock.Object);

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

            budgetAccountMock.Verify(m => m.PostingLineCollection, Times.Once);
        }
Beispiel #10
0
        public async Task CalculateAsync_WhenCalled_ReturnsSameBudgetInfoWhereStatusDateEqualDateFromCall()
        {
            IBudgetInfo sut = CreateSut();

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

            Assert.That(result.StatusDate, Is.EqualTo(statusDate.Date));
        }
Beispiel #11
0
        public void ToDomain_WhenBudgetAccountIsNotNull_ReturnsBudgetInfoWhereMonthIsEqualToMonthInBudgetInfoCommand()
        {
            short month            = (short)_random.Next(BudgetInfo.MinMonth, BudgetInfo.MaxMonth);
            IBudgetInfoCommand sut = CreateSut(month: month);

            IBudgetInfo result = sut.ToDomain(_fixture.BuildBudgetAccountMock().Object);

            Assert.That(result.Month, Is.EqualTo(month));
        }
Beispiel #12
0
        public void ToDomain_WhenBudgetAccountIsNotNull_ReturnsBudgetInfoWhereExpensesIsEqualToExpensesInBudgetInfoCommand()
        {
            decimal            expenses = _fixture.Create <decimal>();
            IBudgetInfoCommand sut      = CreateSut(expenses: expenses);

            IBudgetInfo result = sut.ToDomain(_fixture.BuildBudgetAccountMock().Object);

            Assert.That(result.Expenses, Is.EqualTo(expenses));
        }
Beispiel #13
0
        public void ToDomain_WhenBudgetAccountIsNotNull_ReturnsBudgetInfoWhereIncomeIsEqualToIncomeInBudgetInfoCommand()
        {
            decimal            income = _fixture.Create <decimal>();
            IBudgetInfoCommand sut    = CreateSut(income: income);

            IBudgetInfo result = sut.ToDomain(_fixture.BuildBudgetAccountMock().Object);

            Assert.That(result.Income, Is.EqualTo(income));
        }
        public void Budget_WhenCalled_CalculatesBudget()
        {
            decimal     income   = Math.Abs(_fixture.Create <decimal>());
            decimal     expenses = Math.Abs(_fixture.Create <decimal>());
            IBudgetInfo sut      = CreateSut(income, expenses);

            decimal result = sut.Budget;

            Assert.That(result, Is.EqualTo(income - expenses));
        }
        public void Income_WhenSetterCalledWithValueGreaterToZero_SetValueToGivenValue()
        {
            IBudgetInfo sut = CreateSut();

            decimal value = Math.Abs(_fixture.Create <decimal>() + 0.75M);

            sut.Income = value;

            Assert.That(sut.Income, Is.EqualTo(value));
        }
        internal static IPostingLine ToDomain(this PostingLineModel postingLineModel, IAccount account, IBudgetAccount budgetAccount, IContactAccount contactAccount, MapperCache mapperCache)
        {
            NullGuard.NotNull(postingLineModel, nameof(postingLineModel))
            .NotNull(account, nameof(account))
            .NotNull(mapperCache, nameof(mapperCache));

            Guid postingLineIdentification = Guid.Parse(postingLineModel.PostingLineIdentification);

            lock (mapperCache.SyncRoot)
            {
                if (mapperCache.PostingLineDictionary.TryGetValue(postingLineIdentification, out IPostingLine postingLine))
                {
                    return(postingLine);
                }

                ICreditInfo       creditInfo = account.CreditInfoCollection.Find(postingLineModel.PostingDate);
                ICreditInfoValues accountValuesAtPostingDate = new CreditInfoValues(creditInfo?.Credit ?? 0M, postingLineModel.PostingValueForAccount);

                IBudgetInfoValues budgetAccountValuesAtPostingDate = null;
                if (budgetAccount != null)
                {
                    IBudgetInfo budgetInfo = budgetAccount.BudgetInfoCollection.Find(postingLineModel.PostingDate);
                    budgetAccountValuesAtPostingDate = new BudgetInfoValues(budgetInfo?.Budget ?? 0M, postingLineModel.PostingValueForBudgetAccount ?? 0M);
                }

                IContactInfoValues contactAccountValuesAtPostingDate = null;
                if (contactAccount != null)
                {
                    contactAccountValuesAtPostingDate = new ContactInfoValues(postingLineModel.PostingValueForContactAccount ?? 0M);
                }

                postingLine = new PostingLine(postingLineIdentification, postingLineModel.PostingDate, postingLineModel.Reference, account, postingLineModel.Details, budgetAccount, postingLineModel.Debit ?? 0M, postingLineModel.Credit ?? 0M, contactAccount, postingLineModel.PostingLineIdentifier, accountValuesAtPostingDate, budgetAccountValuesAtPostingDate, contactAccountValuesAtPostingDate);
                postingLine.AddAuditInformation(postingLineModel.CreatedUtcDateTime, postingLineModel.CreatedByIdentifier, postingLineModel.ModifiedUtcDateTime, postingLineModel.ModifiedByIdentifier);

                mapperCache.PostingLineDictionary.Add(postingLineIdentification, postingLine);

                if (account.PostingLineCollection.Contains(postingLine) == false)
                {
                    account.PostingLineCollection.Add(postingLine);
                }

                if (budgetAccount != null && budgetAccount.PostingLineCollection.Contains(postingLine) == false)
                {
                    budgetAccount.PostingLineCollection.Add(postingLine);
                }

                if (contactAccount != null && contactAccount.PostingLineCollection.Contains(postingLine) == false)
                {
                    contactAccount.PostingLineCollection.Add(postingLine);
                }

                return(postingLine);
            }
        }
        public async Task ApplyCalculationAsync_WhenCalledOnPostingLineWhereBudgetAccountValuesAtPostingDateWasNotGivenAndCalculatedBudgetAccountHasBudgetInfoForPostingDate_ReturnsSamePostingLineWhereBudgetOnBudgetAccountValuesAtPostingDateEqualToBudgetFromBudgetInfoForPostingDate()
        {
            IPostingLine sut = CreateSut();

            IBudgetInfo           budgetInfo              = _fixture.BuildBudgetInfoMock().Object;
            IBudgetInfoCollection budgetInfoCollection    = _fixture.BuildBudgetInfoCollectionMock(budgetInfoForFind: budgetInfo).Object;
            IBudgetAccount        calculatedBudgetAccount = _fixture.BuildBudgetAccountMock(budgetInfoCollection: budgetInfoCollection).Object;
            IPostingLine          result = await sut.ApplyCalculationAsync(calculatedBudgetAccount);

            Assert.That(result.BudgetAccountValuesAtPostingDate.Budget, Is.EqualTo(budgetInfo.Budget));
        }
        public async Task UpdateBudgetAccountAsync_WhenCalled_UpdatesBudgetAccount()
        {
            IAccountingRepository sut = CreateSut();

            IBudgetAccountGroup[] budgetAccountGroupCollection = (await sut.GetBudgetAccountGroupsAsync()).ToArray();

            DateTime today = DateTime.Today;

            IBudgetAccount budgetAccount = await sut.GetBudgetAccountAsync(WithExistingAccountingNumber(), WithExistingAccountNumberForBudgetAccount(), today);

            budgetAccount.Description        = _fixture.Create <string>();
            budgetAccount.Note               = _fixture.Create <string>();
            budgetAccount.BudgetAccountGroup = budgetAccountGroupCollection[_random.Next(0, budgetAccountGroupCollection.Length - 1)];

            decimal income   = _random.Next(50, 70) * 1000;
            decimal expenses = _random.Next(25, 35) * 1000;

            IBudgetInfo budgetInfo = budgetAccount.BudgetInfoCollection.Single(m => m.Year == today.AddMonths(-3).Year&& m.Month == today.AddMonths(-3).Month);

            budgetInfo.Income   = income;
            budgetInfo.Expenses = expenses;

            budgetInfo          = budgetAccount.BudgetInfoCollection.Next(budgetInfo);
            budgetInfo.Income   = income;
            budgetInfo.Expenses = expenses;

            budgetInfo          = budgetAccount.BudgetInfoCollection.Next(budgetInfo);
            budgetInfo.Income   = income;
            budgetInfo.Expenses = expenses;

            income   += _random.Next(5, 10) * 1000;
            expenses += _random.Next(5, 10) * 1000;

            budgetInfo          = budgetAccount.BudgetInfoCollection.Next(budgetInfo);
            budgetInfo.Income   = income;
            budgetInfo.Expenses = expenses;

            budgetInfo          = budgetAccount.BudgetInfoCollection.Next(budgetInfo);
            budgetInfo.Income   = income;
            budgetInfo.Expenses = expenses;

            budgetInfo          = budgetAccount.BudgetInfoCollection.Next(budgetInfo);
            budgetInfo.Income   = income;
            budgetInfo.Expenses = expenses;

            budgetInfo          = budgetAccount.BudgetInfoCollection.Next(budgetInfo);
            budgetInfo.Income   = 0M;
            budgetInfo.Expenses = 0M;

            IBudgetAccount result = await sut.UpdateBudgetAccountAsync(budgetAccount);

            Assert.That(result, Is.Not.Null);
        }
Beispiel #19
0
        public async Task CalculateAsync_WhenCalled_AssertPostedIsCalculated()
        {
            DateTime today           = DateTime.Today;
            DateTime calculationDate = DateTime.Today.AddMonths(_random.Next(1, 5) * -1).AddDays(today.Day * -1).AddDays(1);

            int year  = calculationDate.Year;
            int month = calculationDate.Month;

            decimal calculatedPostingValue = _fixture.Create <decimal>();
            IPostingLineCollection postingLineCollection = _fixture.BuildPostingLineCollectionMock(calculatedPostingValue: calculatedPostingValue).Object;
            IBudgetAccount         budgetAccount         = _fixture.BuildBudgetAccountMock(postingLineCollection: postingLineCollection).Object;
            IBudgetInfo            sut = CreateSut(budgetAccount, (short)year, (short)month);

            IBudgetInfo result = await sut.CalculateAsync(calculationDate);

            Assert.That(result.Posted, Is.EqualTo(calculatedPostingValue));
        }
Beispiel #20
0
        public async Task CalculateAsync_WhenStatusDateIsNewerThanBudgetInfo_AssertCalculatePostingValueWasCalledOnPostingLineCollectionFromBudgetAccount()
        {
            DateTime statusDate = DateTime.Today;

            int year  = statusDate.AddMonths(-3).Year;
            int month = statusDate.AddMonths(-3).Month;

            Mock <IPostingLineCollection> postingLineCollectionMock = _fixture.BuildPostingLineCollectionMock();
            IBudgetAccount budgetAccount = _fixture.BuildBudgetAccountMock(postingLineCollection: postingLineCollectionMock.Object).Object;
            IBudgetInfo    sut           = CreateSut(budgetAccount, (short)year, (short)month);

            await sut.CalculateAsync(statusDate);

            postingLineCollectionMock.Verify(m => m.CalculatePostingValue(
                                                 It.Is <DateTime>(value => value == new DateTime(year, month, 1)),
                                                 It.Is <DateTime>(value => value == new DateTime(year, month, DateTime.DaysInMonth(year, month))),
                                                 It.Is <int?>(value => value.HasValue == false)),
                                             Times.Once());
        }
        public async Task CalculateAsync_WhenBudgetInfoCollectionContainBudgetInfoForLastMonthOfStatusDate_ReturnsBudgetInfoCollectionWherePostedInValuesForLastMonthOfStatusDateIsEqualToPostedInBudgetInfoForLastMonthOfStatusDate()
        {
            IBudgetInfoCollection sut = CreateSut();

            DateTime    budgetInfoOffset = DateTime.Today.AddDays(_random.Next(1, _random.Next(1, 365)) * -1);
            IBudgetInfo budgetInfoForLastMonthOfStatusDate = _fixture.BuildBudgetInfoMock(budgetInfoOffset, isLastMonthOfStatusDate: true).Object;
            IEnumerable <IBudgetInfo> budgetInfoCollection = new List <IBudgetInfo>
            {
                budgetInfoForLastMonthOfStatusDate,
                _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.ValuesForLastMonthOfStatusDate.Posted, Is.EqualTo(budgetInfoForLastMonthOfStatusDate.Posted));
        }
        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);
        }
        public Task <IPostingLine> ApplyCalculationAsync(IBudgetAccount calculatedBudgetAccount)
        {
            NullGuard.NotNull(calculatedBudgetAccount, nameof(calculatedBudgetAccount));

            return(Task.Run <IPostingLine>(() =>
            {
                lock (_syncRoot)
                {
                    BudgetAccount = calculatedBudgetAccount;
                }

                if (_calculateBudgetAccountValuesAtPostingDate == false)
                {
                    return this;
                }

                IBudgetInfo budgetInfo = BudgetAccount.BudgetInfoCollection.Find(PostingDate);
                decimal posted = BudgetAccount.PostingLineCollection.CalculatePostingValue(new DateTime(PostingDate.Year, PostingDate.Month, 1), PostingDate, SortOrder);
                BudgetAccountValuesAtPostingDate = new BudgetInfoValues(budgetInfo?.Budget ?? 0M, posted);

                return this;
            }));
        }
Beispiel #24
0
 private IBudgetInfoCommand CreateBudgetInfoCommand(IBudgetInfo budgetInfo = null)
 {
     return(CreateBudgetInfoCommandMock(budgetInfo).Object);
 }
        private static IBudgetInfo BuildBudgetInfo(IBudgetAccount budgetAccount, short year, short month, IBudgetInfo copyFromBudgetInfo)
        {
            NullGuard.NotNull(budgetAccount, nameof(budgetAccount))
            .NotNull(copyFromBudgetInfo, nameof(copyFromBudgetInfo));

            IBudgetInfo budgetInfo = new BudgetInfo(budgetAccount, year, month, copyFromBudgetInfo.Income, copyFromBudgetInfo.Expenses);

            budgetInfo.AddAuditInformation(copyFromBudgetInfo.CreatedDateTime.ToUniversalTime(), copyFromBudgetInfo.CreatedByIdentifier, copyFromBudgetInfo.ModifiedDateTime.ToUniversalTime(), copyFromBudgetInfo.ModifiedByIdentifier);
            return(budgetInfo);
        }