Example #1
0
        public void ValuesAtStatusDate_WhenCalled_ReturnsNotNull()
        {
            IAccount sut = CreateSut();

            ICreditInfoValues result = sut.ValuesAtStatusDate;

            Assert.That(result, Is.Not.Null);
        }
        public void ValuesAtStatusDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsNotNull()
        {
            ICreditInfoCollection sut = CreateSut();

            ICreditInfoValues result = sut.ValuesAtStatusDate;

            Assert.That(result, Is.Not.Null);
        }
        public void ValuesAtStatusDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsCreditInfoValuesWhereBalanceIsEqualToZero()
        {
            ICreditInfoCollection sut = CreateSut();

            ICreditInfoValues result = sut.ValuesAtStatusDate;

            Assert.That(result.Balance, Is.EqualTo(0M));
        }
Example #4
0
        public void AccountValuesAtPostingDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsNotNull()
        {
            IPostingLine sut = CreateSut();

            ICreditInfoValues result = sut.AccountValuesAtPostingDate;

            Assert.That(result, Is.Not.Null);
        }
Example #5
0
        public void AccountValuesAtPostingDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsCreditInfoValuesWhereBalanceIsEqualToZero()
        {
            IPostingLine sut = CreateSut();

            ICreditInfoValues result = sut.AccountValuesAtPostingDate;

            Assert.That(result.Balance, Is.EqualTo(0M));
        }
Example #6
0
        public void ValuesAtEndOfLastMonthFromStatusDate_WhenCalculateAsyncHasNotBeenCalled_ReturnsCreditInfoValuesWhereCreditIsEqualToZero()
        {
            ICreditInfoCollection sut = CreateSut();

            ICreditInfoValues result = sut.ValuesAtEndOfLastMonthFromStatusDate;

            Assert.That(result.Credit, Is.EqualTo(0M));
        }
Example #7
0
        public void ValuesAtStatusDate_WhenCalled_ReturnsSameCreditInfoValuesAsValuesAtStatusDateOnCreditInfoCollection()
        {
            ICreditInfoCollection creditInfoCollection = _fixture.BuildCreditInfoCollectionMock().Object;
            IAccount sut = CreateSut(creditInfoCollection);

            ICreditInfoValues result = sut.ValuesAtStatusDate;

            Assert.That(result, Is.SameAs(creditInfoCollection.ValuesAtStatusDate));
        }
Example #8
0
        public async Task ApplyCalculationAsync_WhenCalledOnPostingLineWhereAccountValuesAtPostingDateWasGiven_ReturnsSamePostingLineWhereAccountValuesAtPostingDateEqualToGivenAccountValuesAtPostingDate()
        {
            ICreditInfoValues accountValuesAtPostingDate = _fixture.BuildCreditInfoValuesMock().Object;
            IPostingLine      sut = CreateSut(accountValuesAtPostingDate: accountValuesAtPostingDate);

            IPostingLine result = await sut.ApplyCalculationAsync(_fixture.BuildAccountMock().Object);

            Assert.That(result.AccountValuesAtPostingDate, Is.EqualTo(accountValuesAtPostingDate));
        }
Example #9
0
        public async Task ApplyCalculationAsync_WhenCalledOnPostingLineWhereAccountValuesAtPostingDateWasGiven_AssertPostingLineCollectionWasNotCalledOnCalculatedAccount()
        {
            ICreditInfoValues accountValuesAtPostingDate = _fixture.BuildCreditInfoValuesMock().Object;
            IPostingLine      sut = CreateSut(accountValuesAtPostingDate: accountValuesAtPostingDate);

            Mock <IAccount> calculatedAccountMock = _fixture.BuildAccountMock();
            await sut.ApplyCalculationAsync(calculatedAccountMock.Object);

            calculatedAccountMock.Verify(m => m.PostingLineCollection, Times.Never);
        }
        public async Task CalculateAsync_WhenCalledOnPostingLineWithAccount_ReturnsSamePostingLineWhereAccountValuesAtPostingDateHasNotBeenChanged()
        {
            IAccount     account = _fixture.BuildAccountMock(statusDate: DateTime.MinValue).Object;
            IPostingLine sut     = CreateSut(account: account);

            ICreditInfoValues accountValuesAtPostingDate = sut.AccountValuesAtPostingDate;
            IPostingLine      result = await sut.CalculateAsync(DateTime.Now.AddDays(_random.Next(1, 365) * -1));

            Assert.That(result.AccountValuesAtPostingDate, Is.SameAs(accountValuesAtPostingDate));
        }
Example #11
0
        public void ValuesAtStatusDate_WhenCalled_AssertValuesAtStatusDateWasCalledOnCreditInfoCollection()
        {
            Mock <ICreditInfoCollection> creditInfoCollectionMock = _fixture.BuildCreditInfoCollectionMock();
            IAccount sut = CreateSut(creditInfoCollectionMock.Object);

            ICreditInfoValues result = sut.ValuesAtStatusDate;

            Assert.That(result, Is.Not.Null);

            creditInfoCollectionMock.Verify(m => m.ValuesAtStatusDate, Times.Once);
        }
        protected override ICreditInfoCollection Calculate(DateTime statusDate, IReadOnlyCollection <ICreditInfo> calculatedCreditInfoCollection)
        {
            NullGuard.NotNull(calculatedCreditInfoCollection, nameof(calculatedCreditInfoCollection));

            ICreditInfo creditInfoAtStatusDate = calculatedCreditInfoCollection
                                                 .AsParallel()
                                                 .SingleOrDefault(creditInfo => creditInfo.IsMonthOfStatusDate);
            ICreditInfo creditInfoAtEndOfLastMonthFromStatusDate = calculatedCreditInfoCollection
                                                                   .AsParallel()
                                                                   .SingleOrDefault(creditInfo => creditInfo.IsLastMonthOfStatusDate);
            ICreditInfo creditInfoAtEndOfLastYearFromStatusDate = calculatedCreditInfoCollection
                                                                  .AsParallel()
                                                                  .Where(creditInfo => creditInfo.IsLastYearOfStatusDate)
                                                                  .OrderByDescending(creditInfo => creditInfo.Year)
                                                                  .ThenByDescending(creditInfo => creditInfo.Month)
                                                                  .FirstOrDefault();

            ValuesAtStatusDate = ToCreditInfoValues(creditInfoAtStatusDate);
            ValuesAtEndOfLastMonthFromStatusDate = ToCreditInfoValues(creditInfoAtEndOfLastMonthFromStatusDate);
            ValuesAtEndOfLastYearFromStatusDate  = ToCreditInfoValues(creditInfoAtEndOfLastYearFromStatusDate);

            return(this);
        }
Example #13
0
        private static Task <IPostingWarning> CalculateAsync(IAccount account, ICreditInfoValues creditInfoValues, IPostingLine postingLine)
        {
            NullGuard.NotNull(account, nameof(account))
            .NotNull(creditInfoValues, nameof(creditInfoValues))
            .NotNull(postingLine, nameof(postingLine));

            return(Task.Run(() =>
            {
                if (account.AccountGroupType != AccountGroupType.Assets)
                {
                    return null;
                }

                decimal credit = creditInfoValues.Credit;
                decimal balance = creditInfoValues.Balance;

                if (balance >= 0M || Math.Abs(balance) <= Math.Abs(credit))
                {
                    return null;
                }

                return (IPostingWarning) new PostingWarning(PostingWarningReason.AccountIsOverdrawn, account, Math.Abs(balance) - Math.Abs(credit), postingLine);
            }));
        }
Example #14
0
        private IPostingLine CreateSut(DateTime?postingDate = null, int?sortOrder = null, ICreditInfoValues accountValuesAtPostingDate = null)
        {
            int year  = _random.Next(InfoBase <ICreditInfo> .MinYear, Math.Min(DateTime.Today.Year, InfoBase <ICreditInfo> .MaxYear));
            int month = _random.Next(InfoBase <ICreditInfo> .MinMonth, Math.Min(DateTime.Today.Month, InfoBase <ICreditInfo> .MaxMonth));
            int day   = _random.Next(1, DateTime.DaysInMonth(year, month));

            return(new Domain.Accounting.PostingLine(Guid.NewGuid(), postingDate ?? new DateTime(year, month, day), _fixture.Create <string>(), _fixture.BuildAccountMock().Object, _fixture.Create <string>(), null, Math.Abs(_fixture.Create <decimal>()), Math.Abs(_fixture.Create <decimal>()), null, sortOrder ?? Math.Abs(_fixture.Create <int>()), accountValuesAtPostingDate));
        }
        public PostingLine(Guid identifier, DateTime postingDate, string reference, IAccount account, string details, IBudgetAccount budgetAccount, decimal debit, decimal credit, IContactAccount contactAccount, int sortOrder, ICreditInfoValues accountValuesAtPostingDate = null, IBudgetInfoValues budgetAccountValuesAtPostingDate = null, IContactInfoValues contactAccountValuesAtPostingDate = null)
        {
            NullGuard.NotNull(account, nameof(account))
            .NotNullOrWhiteSpace(details, nameof(details));

            if (postingDate.Year < InfoBase <ICreditInfo> .MinYear || postingDate.Year > InfoBase <ICreditInfo> .MaxYear)
            {
                throw new ArgumentException($"Year for the posting data should be between {InfoBase<ICreditInfo>.MinYear} and {InfoBase<ICreditInfo>.MaxYear}.", nameof(postingDate));
            }

            if (postingDate.Month < InfoBase <ICreditInfo> .MinMonth || postingDate.Month > InfoBase <ICreditInfo> .MaxMonth)
            {
                throw new ArgumentException($"Month for the posting data should be between {InfoBase<ICreditInfo>.MinMonth} and {InfoBase<ICreditInfo>.MaxMonth}.", nameof(postingDate));
            }

            if (budgetAccount != null && budgetAccount.Accounting.Number != account.Accounting.Number)
            {
                throw new ArgumentException("Accounting on the given budget account does not match the accounting on the given account.", nameof(budgetAccount));
            }

            if (debit < 0M)
            {
                throw new ArgumentException("Debit cannot be lower than 0.", nameof(debit));
            }

            if (credit < 0M)
            {
                throw new ArgumentException("Credit cannot be lower than 0.", nameof(credit));
            }

            if (contactAccount != null && contactAccount.Accounting.Number != account.Accounting.Number)
            {
                throw new ArgumentException("Accounting on the given contact account does not match the accounting on the given account.", nameof(contactAccount));
            }

            if (sortOrder < 0)
            {
                throw new ArgumentException("Sort order cannot be lower than 0.", nameof(sortOrder));
            }

            Identifier  = identifier;
            Accounting  = account.Accounting;
            PostingDate = postingDate.Date;
            Reference   = string.IsNullOrWhiteSpace(reference) ? null : reference.Trim();
            Account     = account;
            AccountValuesAtPostingDate = accountValuesAtPostingDate ?? new CreditInfoValues(0M, 0M);
            Details       = details.Trim();
            BudgetAccount = budgetAccount;
            BudgetAccountValuesAtPostingDate = budgetAccount != null ? budgetAccountValuesAtPostingDate ?? new BudgetInfoValues(0M, 0M) : null;
            Debit          = debit;
            Credit         = credit;
            ContactAccount = contactAccount;
            ContactAccountValuesAtPostingDate = contactAccount != null ? contactAccountValuesAtPostingDate ?? new ContactInfoValues(0M) : null;
            SortOrder = sortOrder;

            _calculateAccountValuesAtPostingDate        = accountValuesAtPostingDate == null;
            _calculateBudgetAccountValuesAtPostingDate  = budgetAccountValuesAtPostingDate == null;
            _calculateContactAccountValuesAtPostingDate = contactAccountValuesAtPostingDate == null;
        }