Beispiel #1
0
        public async Task Given_ExistingPeriodWhichIsLastPeriod_When_EndDateUpdated_Then_ResultIsSuccess()
        {
            // Arrange.
            var period = new Period(
                new DateTime(2020, 1, 1),
                new DateTime(2020, 1, 31));

            AccountingManager testObject = AccountingManagerFactory.Create(out IAccountingDataAccess accountingDataAccess);

            accountingDataAccess
            .GetLastPeriod()
            .Returns(GetResult <Period> .CreateSuccess(period));

            var updateEndDate = new UpdateLastPeriodEndDateDto(period.End.AddDays(1));

            accountingDataAccess
            .UpdateLastPeriodEndDate(updateEndDate.NewEnd)
            .Returns(ActionResult.CreateSuccess());

            // Act.
            ActionResult result = await testObject.UpdateLastPeriodEndDate(updateEndDate);

            // Assert.
            Assert.IsTrue(result.IsSuccess);
        }
        public async Task Given_Transaction_When_ProcessedSuccessfully_Then_ResultIsSuccess()
        {
            // Arrange.
            const string  debitAccountName  = "Funds";
            const string  creditAccountName = "Expense";
            const decimal amount            = 123.45m;

            AccountingManager testObject = AccountingManagerFactory.Create(out IAccountingDataAccess accountingDataAccess);

            AccountingManagerFactory.ConfigureDataAccessWithBaseAccounts(accountingDataAccess);

            Account debitAccount  = accountingDataAccess.GetAccount(debitAccountName).Result.Result;
            Account creditAccount = accountingDataAccess.GetAccount(creditAccountName).Result.Result;

            accountingDataAccess
            .GetAccounts(Arg.Any <IEnumerable <string> >())
            .Returns(GetResult <IReadOnlyDictionary <string, Account> > .CreateSuccess(
                         new Dictionary <string, Account>
            {
                { debitAccountName, debitAccount },
                { creditAccountName, creditAccount }
            }));

            accountingDataAccess
            .GetParentAccountsOrdered(debitAccountName)
            .Returns(GetResult <IEnumerable <Account> > .CreateSuccess(new Account[0]));

            accountingDataAccess
            .GetParentAccountsOrdered(creditAccountName)
            .Returns(GetResult <IEnumerable <Account> > .CreateSuccess(new Account[0]));

            accountingDataAccess
            .UpdateAccountBalances(default)
Beispiel #3
0
        public async Task Given_ExistingLastPeriod_When_EndDateUpdatedWithDateBeforeStart_Then_ResultIsFailure()
        {
            // Arrange.
            var lastPeriod = new Period(
                new DateTime(2020, 2, 1),
                new DateTime(2020, 2, 29));

            AccountingManager testObject = AccountingManagerFactory.Create(out IAccountingDataAccess accountingDataAccess);

            accountingDataAccess
            .GetLastPeriod()
            .Returns(GetResult <Period> .CreateSuccess(lastPeriod));

            var updateEndDate = new UpdateLastPeriodEndDateDto(lastPeriod.Start.AddDays(-1));

            accountingDataAccess
            .UpdateLastPeriodEndDate(updateEndDate.NewEnd)
            .Returns(ActionResult.CreateSuccess());

            // Act.
            ActionResult result = await testObject.UpdateLastPeriodEndDate(updateEndDate);

            // Assert.
            Assert.IsFalse(result.IsSuccess);
        }
Beispiel #4
0
        public async Task Given_NewAccount_When_Added_Then_BalanceIsZero()
        {
            // Arrange.
            AccountingManager testObject = AccountingManagerFactory.Create(out IAccountingDataAccess accountingDataAccess);

            accountingDataAccess
            .GetAccounts(Arg.Any <string[]>())
            .Returns(GetResult <IReadOnlyDictionary <string, Account> > .CreateSuccess(
                         new Dictionary <string, Account>
            {
                { BaseAccountNames[0], new Account() }
            }));

            var newAccount = new NewAccountDto
            {
                Name = "SomeAccount",
                ParentAccountQualifiedName = BaseAccountNames[0]
            };

            // Act.
            await testObject.AddAccount(newAccount);

            // Assert.
            await accountingDataAccess
            .Received()
            .AddAccount(Arg.Is <Account>(a => a.Balance == 0m));
        }
        public async Task <GetResult <Period> > GetLastPeriod()
        {
            if (_periods.Count == 0)
            {
                return(await Task.FromResult(
                           GetResult <Period> .CreateFailure("No periods found")));
            }

            Period period = _periods
                            .OrderBy(p => p.Start)
                            .LastOrDefault();

            return(await Task.FromResult(
                       GetResult <Period> .CreateSuccess(period)));
        }
Beispiel #6
0
        public async Task <GetResult <IEnumerable <TransactionDto> > > GetTransactions()
        {
            _logger.LogVerbose("Received request for transactions.");

            GetResult <IEnumerable <Transaction> > getTransactionsResult = await _accountingDataAccess.GetTransactions();

            if (!getTransactionsResult.IsSuccess)
            {
                return(GetResult <IEnumerable <TransactionDto> > .CreateFailure(getTransactionsResult.FailureMessage));
            }

            IEnumerable <TransactionDto> transactions =
                _mapper.Map <IEnumerable <Transaction>, IEnumerable <TransactionDto> >(getTransactionsResult.Result);

            return(GetResult <IEnumerable <TransactionDto> > .CreateSuccess(transactions));
        }
        public async Task <GetResult <Period> > GetPeriodForDate(DateTime date)
        {
            Period period = _periods
                            .FirstOrDefault(p =>
                                            p.Start <= date &&
                                            p.End >= date);

            if (period == null)
            {
                return(await Task.FromResult(
                           GetResult <Period> .CreateFailure($"No period found for date: {date:yyyy-MM-dd}")));
            }

            return(await Task.FromResult(
                       GetResult <Period> .CreateSuccess(period)));
        }
Beispiel #8
0
        public async Task Given_ExistingPeriods_When_Retrieved_Then_AllPeriodsArePresent()
        {
            // Arrange.
            var period1 = new Period(
                new DateTime(2020, 2, 1),
                new DateTime(2020, 2, 29));

            var period2 = new Period(
                new DateTime(2020, 3, 1),
                new DateTime(2020, 3, 31));

            AccountingManager testObject = AccountingManagerFactory.Create(out IAccountingDataAccess accountingDataAccess);

            accountingDataAccess
            .GetLastPeriod()
            .Returns(GetResult <Period> .CreateSuccess(period1));

            accountingDataAccess
            .GetAllPeriods()
            .Returns(GetResult <IEnumerable <Period> > .CreateSuccess(new[]
            {
                period1,
                period2
            }));

            // Act.
            GetResult <IEnumerable <PeriodDto> > result = await testObject.GetAllPeriods();

            // Assert.
            Assert.IsTrue(result.IsSuccess);

            IEnumerable <PeriodDto> periods = result.Result;

            Assert.AreEqual(2, periods.Count());

            Assert.IsNotNull(
                periods
                .SingleOrDefault(p =>
                                 p.Start == period1.Start &&
                                 p.End == period1.End));

            Assert.IsNotNull(
                periods
                .SingleOrDefault(p =>
                                 p.Start == period2.Start &&
                                 p.End == period2.End));
        }
Beispiel #9
0
        public async Task Given_AccountsExist_When_RetrievingTransactionDebitAccounts_Then_CorrectAccountsAreReturned()
        {
            // Arrange.
            AccountingManager testObject = AccountingManagerFactory.Create(out IAccountingDataAccess accountingDataAccess);

            string someNonBaseAccount = $"{FundsAccountName}{AccountQualifiedNameSeparator}SomeAccount";

            accountingDataAccess
            .GetAccounts(
                isFunds: true,
                isIncome: true,
                isDebtor: true,
                isCreditor: true)
            .Returns(GetResult <IEnumerable <Account> > .CreateSuccess(new[]
            {
                new Account {
                    QualifiedName = FundsAccountName
                },
                new Account {
                    QualifiedName = IncomeAccountName
                },
                new Account {
                    QualifiedName = DebtorAccountName
                },
                new Account {
                    QualifiedName = CreditorAccountName
                },
                new Account {
                    QualifiedName = someNonBaseAccount, ParentAccountQualifiedName = FundsAccountName
                }
            }));

            // Act.
            GetResult <IEnumerable <AccountDto> > accountsResult = await testObject.GetTransactionDebitAccounts();

            // Assert.
            IEnumerable <AccountDto> accounts = accountsResult.Result;

            Assert.IsNull(accounts.SingleOrDefault(a => a.QualifiedName.Equals(FundsAccountName)));

            accounts.Single(a => a.QualifiedName.Equals(IncomeAccountName));
            accounts.Single(a => a.QualifiedName.Equals(DebtorAccountName));
            accounts.Single(a => a.QualifiedName.Equals(CreditorAccountName));
            accounts.Single(a => a.QualifiedName.Equals(someNonBaseAccount));

            Assert.Pass();
        }
Beispiel #10
0
        public async Task Given_PeriodWithEndBeforeStart_When_Adding_Then_ResultIsFailure()
        {
            // Arrange.
            AccountingManager testObject = AccountingManagerFactory.Create(out IAccountingDataAccess accountingDataAccess);

            accountingDataAccess
            .GetLastPeriod()
            .Returns(GetResult <Period> .CreateSuccess(null));

            accountingDataAccess.AddPeriod(Arg.Any <Period>()).Returns(ActionResult.CreateSuccess());

            var period = new PeriodDto(DateTime.Now, DateTime.Now.AddDays(-1));

            // Act.
            ActionResult result = await testObject.AddPeriod(period);

            // Assert.
            Assert.IsFalse(result.IsSuccess);
        }
Beispiel #11
0
        public async Task Given_ExistingPeriods_When_NewPeriodDoesNotStartAfterLastExistingPeriod_Then_ResultIsFailure()
        {
            // Arrange.
            var lastPeriod = new Period(
                new DateTime(2020, 1, 1),
                new DateTime(2020, 1, 31));

            AccountingManager testObject = AccountingManagerFactory.Create(out IAccountingDataAccess accountingDataAccess);

            accountingDataAccess
            .GetLastPeriod()
            .Returns(GetResult <Period> .CreateSuccess(lastPeriod));

            var period = new PeriodDto(
                lastPeriod.End.AddDays(2),
                lastPeriod.End.AddDays(30));

            // Act.
            ActionResult result = await testObject.AddPeriod(period);

            // Assert.
            Assert.IsFalse(result.IsSuccess);
        }
 public async Task <GetResult <IEnumerable <Period> > > GetAllPeriods()
 {
     return(await Task.FromResult(
                GetResult <IEnumerable <Period> > .CreateSuccess(_periods)));
 }