Beispiel #1
0
        public async Task OnRetrieve_GetsRelatedItems()
        {
            //Arrange
            AutoMocker mocker = new();

            using var factory = mocker.WithDbScope();

            var category = new ExpenseCategory {
                CurrentBalance = 250
            };

            using var setupContext = factory.Create();
            setupContext.Add(category);
            await setupContext.SaveChangesAsync();

            await setupContext.AddIncome("Test", DateTime.Now, (100, category.ID));

            //Act
            using var actContext = factory.Create();
            ExpenseCategoryItem income = await actContext.ExpenseCategoryItems
                                         .Include(x => x.Details)
                                         .ThenInclude(x => x.ExpenseCategory)
                                         .SingleAsync();

            //Assert
            Assert.IsNotNull(income?.Details);
            var item = income !.Details.Single();

            Assert.AreEqual(100, item.Amount);
            Assert.AreEqual(category.ID, item.ExpenseCategory?.ID);
        }
Beispiel #2
0
        public BudgetHistoryViewModel(ExpenseCategoryItem item, int currentAccountAmount)
        {
            Item = item;

            Date          = item.Date;
            Description   = item.Description ?? "";
            CurrentAmount = currentAccountAmount;

            Details = item.Details?
                      .Select(x => new BudgetHistoryDetailsViewModel(x))
                      .OrderBy(x => x.ExpenseCategoryName)
                      .ToList() ?? new List <BudgetHistoryDetailsViewModel>();

            if (item.IsTransfer)
            {
                Type = BudgetHistoryViewModelType.Transfer;
            }
            else if (item.Details?.Sum(x => x.Amount) > 0)
            {
                Type = BudgetHistoryViewModelType.Income;
            }
            else
            {
                Type = BudgetHistoryViewModelType.Transaction;
            }

            DisplayAmount = item.GetDisplayAmount();
        }
Beispiel #3
0
        public void Transfer_ShowCorrectDetails()
        {
            // Arrange
            var expenseCategoryFrom = new ExpenseCategory
            {
                ID   = 72,
                Name = "From Category Name"
            };
            var expenseCategoryTo = new ExpenseCategory
            {
                ID   = 73,
                Name = "To Category Name"
            };
            var transfer = new ExpenseCategoryItem
            {
                ID          = 42,
                Date        = DateTime.Today,
                Description = "Transfer Description",
                Details     = new List <ExpenseCategoryItemDetail>()
            };
            var fromDetail = new ExpenseCategoryItemDetail
            {
                Amount                = -450,
                ExpenseCategory       = expenseCategoryFrom,
                ExpenseCategoryId     = expenseCategoryFrom.ID,
                ExpenseCategoryItem   = transfer,
                ExpenseCategoryItemId = transfer.ID,
                ID = 1
            };
            var toDetail = new ExpenseCategoryItemDetail
            {
                Amount                = 450,
                ExpenseCategory       = expenseCategoryTo,
                ExpenseCategoryId     = expenseCategoryTo.ID,
                ExpenseCategoryItem   = transfer,
                ExpenseCategoryItemId = transfer.ID,
                ID = 2
            };

            transfer.Details.Add(toDetail);
            transfer.Details.Add(fromDetail);

            //Act
            var vm = new BudgetHistoryViewModel(transfer, 400);

            //Assert
            Assert.AreEqual(transfer.Date, vm.Date);
            Assert.AreEqual(transfer.Description, vm.Description);
            Assert.AreEqual(400, vm.CurrentAmount);
            Assert.AreEqual($"<{4.50:c}>", vm.DisplayAmount);
            Assert.AreEqual(2, vm.Details.Count);

            var from = vm.Details[0];
            var to   = vm.Details[1];

            Assert.AreEqual($"{-4.50:c}", from.Amount);
            Assert.AreEqual(expenseCategoryFrom.Name, from.ExpenseCategoryName);
            Assert.AreEqual($"{4.50:c}", to.Amount);
            Assert.AreEqual(expenseCategoryTo.Name, to.ExpenseCategoryName);
        }
Beispiel #4
0
        public async Task OnRemove_RemovesChildren()
        {
            //Arrange
            AutoMocker mocker = new();

            using var factory = mocker.WithDbScope();
            var category = new ExpenseCategory {
                CurrentBalance = 250
            };

            using var setupContext = factory.Create();
            setupContext.Add(category);
            await setupContext.SaveChangesAsync();

            await setupContext.AddIncome("Test", DateTime.Now, (100, category.ID));

            //Act
            using var actContext = factory.Create();
            ExpenseCategoryItem item = await actContext.ExpenseCategoryItems.SingleAsync();

            actContext.Remove(item);
            await actContext.SaveChangesAsync();

            int itemId = item.ID;

            //Assert
            using var assertContext = factory.Create();
            Assert.IsFalse(await assertContext.ExpenseCategoryItems.AnyAsync());
            Assert.IsFalse(await assertContext.ExpenseCategoryItemDetails.AnyAsync());
            Assert.AreEqual(250, (await assertContext.FindAsync <ExpenseCategory>(category.ID)).CurrentBalance);
        }
Beispiel #5
0
        public async Task GetTransfersWithoutDateRange_ReturnsTransfersForCategory()
        {
            //Arrange
            AutoMocker mocker = new();

            using var factory = mocker.WithDbScope();

            var account   = new Account();
            var category1 = new ExpenseCategory {
                Account = account
            };
            var category2 = new ExpenseCategory {
                Account = account
            };
            var category3 = new ExpenseCategory {
                Account = account
            };

            using var setupContext = factory.Create();
            setupContext.ExpenseCategories.AddRange(category1, category2, category3);
            await setupContext.SaveChangesAsync();

            ExpenseCategoryItem transfer1 = await setupContext.AddTransfer("", DateTime.Now, 10, category1, category2);

            ExpenseCategoryItem transfer2 = await setupContext.AddTransfer("", DateTime.Now, 10, category2, category3);

            ExpenseCategoryItem transfer3 = await setupContext.AddTransfer("", DateTime.Now, 10, category3, category1);

            //Act
            using var actContext = factory.Create();
            IList <ExpenseCategoryItem> transfers = await actContext.GetTransfers(category2);

            //Assert
            CollectionAssert.AreEquivalent(new[] { transfer1.ID, transfer2.ID }, transfers.Select(x => x.ID).ToList());
        }
Beispiel #6
0
        public async Task AddTransactionWithoutDate_CreatesTransaction()
        {
            //Arrange
            AutoMocker mocker = new();

            using var factory = mocker.WithDbScope();

            const string description = "Test Description";
            const int    amount      = 100;
            var          category    = new ExpenseCategory
            {
                Account = new Account()
            };

            using var setupContext = factory.Create();
            setupContext.ExpenseCategories.Add(category);
            await setupContext.SaveChangesAsync();

            //Act
            using var actContext = factory.Create();
            ExpenseCategoryItem transaction = await actContext.AddTransaction(category.ID, amount, description);

            //Assert
            using var assertContext = factory.Create();
            var foundTransaction = assertContext.ExpenseCategoryItems
                                   .Include(x => x.Details)
                                   .Single(x => x.ID == transaction !.ID);

            Assert.AreEqual(description, foundTransaction.Description);

            Assert.AreEqual(1, foundTransaction.Details?.Count);
            Assert.AreEqual(-amount, foundTransaction.Details?[0].Amount);
            Assert.AreEqual(category.ID, foundTransaction.Details?[0].ExpenseCategoryId);
        }
Beispiel #7
0
        public void SetIgnoreBudget_True_Throws()
        {
            var item = new ExpenseCategoryItem();

            var ex = Assert.ThrowsException <ArgumentNullException>(() => item.IgnoreBudget = null);

            Assert.AreEqual("value", ex.ParamName);
        }
Beispiel #8
0
        public void Income_ShowCorrectDetails()
        {
            // Arrange
            var expenseCategory = new ExpenseCategory
            {
                ID   = 72,
                Name = "Category Name"
            };
            var income = new ExpenseCategoryItem
            {
                ID          = 42,
                Date        = DateTime.Today,
                Description = "Income Description",
                Details     = new List <ExpenseCategoryItemDetail>()
            };
            var detail = new ExpenseCategoryItemDetail
            {
                Amount                = 500,
                ExpenseCategory       = expenseCategory,
                ExpenseCategoryId     = expenseCategory.ID,
                ExpenseCategoryItem   = income,
                ExpenseCategoryItemId = income.ID,
                ID = 1
            };

            income.Details.Add(detail);

            //Act
            var vm = new BudgetHistoryViewModel(income, 400);

            //Assert
            Assert.AreEqual(income.Date, vm.Date);
            Assert.AreEqual(income.Description, vm.Description);
            Assert.AreEqual(400, vm.CurrentAmount);
            Assert.AreEqual($"{5.00:c}", vm.DisplayAmount);
            var item1 = vm.Details.Single();

            Assert.AreEqual($"{5.00:c}", item1.Amount);
            Assert.AreEqual(expenseCategory.Name, item1.ExpenseCategoryName);
        }
Beispiel #9
0
        public void Transactions_ShowCorrectDetails()
        {
            // Arrange
            var expenseCategory = new ExpenseCategory
            {
                ID   = 72,
                Name = "Category Name"
            };
            var transaction = new ExpenseCategoryItem
            {
                ID          = 42,
                Date        = DateTime.Today,
                Description = "Transaction Description",
                Details     = new List <ExpenseCategoryItemDetail>()
            };
            var detail = new ExpenseCategoryItemDetail
            {
                Amount                = -500,
                ExpenseCategory       = expenseCategory,
                ExpenseCategoryId     = expenseCategory.ID,
                ExpenseCategoryItem   = transaction,
                ExpenseCategoryItemId = transaction.ID,
                ID = 1
            };

            transaction.Details.Add(detail);

            //Act
            var vm = new BudgetHistoryViewModel(transaction, 200);

            //Assert
            Assert.AreEqual(transaction.Date, vm.Date);
            Assert.AreEqual(transaction.Description, vm.Description);
            Assert.AreEqual(200, vm.CurrentAmount);
            Assert.AreEqual($"({5.00:c})", vm.DisplayAmount);
            var item1 = vm.Details.Single();

            Assert.AreEqual($"({5.00:c})", item1.Amount);
            Assert.AreEqual(expenseCategory.Name, item1.ExpenseCategoryName);
        }
Beispiel #10
0
        public async Task GetTransactionItemsWithoutDateRange_ReturnsTransactionItems()
        {
            // Arrange
            AutoMocker mocker = new();

            using var factory = mocker.WithDbScope();

            var account   = new Account();
            var category1 = new ExpenseCategory {
                Account = account
            };
            var category2 = new ExpenseCategory {
                Account = account
            };

            using var setupContext = factory.Create();
            setupContext.ExpenseCategories.AddRange(category1, category2);
            await setupContext.SaveChangesAsync();

            ExpenseCategoryItem transaction1 = await setupContext.AddTransaction(category1.ID, 100, "Transaction 1");

            ExpenseCategoryItem transaction2 = await setupContext.AddTransaction(category2.ID, 200, "Transaction 2");

            ExpenseCategoryItem transaction3 = await setupContext.AddTransaction(category1.ID, 300, "Transaction 3");

            //Act
            using var actContext = factory.Create();
            IList <ExpenseCategoryItemDetail> transactionItems = await actContext.GetCategoryItemDetails(category1);

            //Assert
            Assert.AreEqual(2, transactionItems.Count);

            Assert.AreEqual(-100, transactionItems[0].Amount);
            Assert.AreEqual("Transaction 1", transactionItems[0].ExpenseCategoryItem?.Description);

            Assert.AreEqual(-300, transactionItems[1].Amount);
            Assert.AreEqual("Transaction 3", transactionItems[1].ExpenseCategoryItem?.Description);
        }
Beispiel #11
0
        public async Task SubmitCommand_TransactionIgnoreBudget_CreatesTransaction()
        {
            var mocker = new AutoMocker().WithDefaults();

            using var factory = mocker.WithDbScope();
            using var _       = mocker.WithAutoDIResolver();
            using var __      = mocker.WithSynchonousTaskRunner();
            using var context = factory.Create();

            var category = new ExpenseCategory();

            context.ExpenseCategories.Add(category);
            await context.SaveChangesAsync();

            var vm = mocker.CreateInstance <AddItemViewModel>();

            var today = DateTime.Today;

            vm.SelectedType                  = AddType.Transaction;
            vm.Description                   = "Test description";
            vm.Date                          = today;
            vm.LineItems[0].Amount           = 25_00;
            vm.LineItems[0].SelectedCategory = category;

            await vm.SubmitCommand.ExecuteAsync(true);

            using var verificationContext = factory.Create();
            ExpenseCategoryItem transaction = await verificationContext.ExpenseCategoryItems
                                              .Include(x => x.Details)
                                              .SingleAsync();

            Assert.AreEqual("Test description", transaction.Description);
            Assert.AreEqual(today, transaction.Date);
            Assert.IsTrue(transaction.IgnoreBudget);
            Assert.AreEqual(1, transaction.Details?.Count);
            Assert.AreEqual(-25_00, transaction.Details ![0].Amount);
Beispiel #12
0
        public void IgnoreBudget_WithNoDetails_ReturnsNull()
        {
            var item = new ExpenseCategoryItem();

            Assert.IsNull(item.IgnoreBudget);
        }
Beispiel #13
0
 public ImportItem(ExpenseCategoryItem item)
 {
     Item = item;
 }