public async Task SelectedType_Income_LoadsItems() { var mocker = new AutoMocker().WithDefaults(); using var factory = mocker.WithDbScope(); using var context = factory.Create(); var category1 = new ExpenseCategory { Name = "Foo" }; var category2 = new ExpenseCategory { Name = "Bar" }; var category3 = new ExpenseCategory { Name = "Cat" }; var category4 = new ExpenseCategory { Name = "Hidden", IsHidden = true }; context.AddRange(category1, category2, category3, category4); await context.SaveChangesAsync(); var vm = mocker.CreateInstance <AddItemViewModel>(); vm.SelectedType = AddType.Income; Assert.AreEqual(3, vm.LineItems.Count); Assert.AreEqual(category2, vm.LineItems[0].SelectedCategory); Assert.AreEqual(category3, vm.LineItems[1].SelectedCategory); Assert.AreEqual(category1, vm.LineItems[2].SelectedCategory); }
public async Task SelectedType_Transfer_LoadsItems() { var mocker = new AutoMocker().WithDefaults(); using var factory = mocker.WithDbScope(); using var _ = mocker.WithAutoDIResolver(); using var context = factory.Create(); var category1 = new ExpenseCategory { Name = "Foo" }; var category2 = new ExpenseCategory { Name = "Bar" }; var category3 = new ExpenseCategory { Name = "Cat" }; context.AddRange(category1, category2, category3); await context.SaveChangesAsync(); var vm = mocker.CreateInstance <AddItemViewModel>(); vm.SelectedType = AddType.Transfer; Assert.AreEqual(2, vm.LineItems.Count); Assert.IsNull(vm.LineItems[0].SelectedCategory); Assert.IsNull(vm.LineItems[1].SelectedCategory); Assert.AreEqual(DateTime.Today, vm.Date); }
public async Task AutoAllocateCommand_AllocatesIncomeAmount() { var mocker = new AutoMocker().WithDefaults(); using var factory = mocker.WithDbScope(); using var context = factory.Create(); var category1 = new ExpenseCategory { Name = "Bar", BudgetedPercentage = 10 }; var category2 = new ExpenseCategory { Name = "Cat", BudgetedAmount = 100 }; var category3 = new ExpenseCategory { Name = "Foo", BudgetedAmount = 200 }; context.AddRange(category1, category2, category3); await context.SaveChangesAsync(); var vm = mocker.CreateInstance <AddItemViewModel>(); vm.SelectedType = AddType.Income; vm.TotalAmount = 200; vm.AutoAllocateCommand.Execute(null); Assert.AreEqual(3, vm.LineItems.Count); Assert.AreEqual(20, vm.LineItems[0].Amount); Assert.AreEqual(100, vm.LineItems[1].Amount); Assert.AreEqual(80, vm.LineItems[2].Amount); }
public async Task OnImport_WhenItemMatchesExistingIncome_IsMarkedDone() { var mocker = new AutoMocker().WithDefaults(); using var factory = mocker.WithDbScope(); using var context = factory.Create(); var date = DateTime.Today.AddDays(-1); var category = new ExpenseCategory(); context.ExpenseCategories.Add(category); await context.SaveChangesAsync(); var item = new ExpenseCategoryItem { Date = date, Description = "Existing item", Details = new() { new ExpenseCategoryItemDetail { Amount = 10_00, ExpenseCategory = category }, new ExpenseCategoryItemDetail { Amount = 5_23, ExpenseCategory = category } } };
public async Task SaveChanges_WithFoundCategory_UpdatesBudgettedPercentage() { AutoMocker mocker = new AutoMocker().WithDefaults(); using var factory = mocker.WithDbScope(); using var context = factory.Create(); context.ExpenseCategories.Add(new ExpenseCategory { ID = 42, CategoryName = "CategoryName", Name = "Name", BudgetedAmount = 100 }); await context.SaveChangesAsync(); var vm = mocker.CreateInstance <BudgetViewModel>(); var expenseCategory = await ExpenseCategoryViewModelEx.Create(mocker.Get <Func <BudgetContext> >(), new ExpenseCategory { ID = 42 }); expenseCategory.EditingCategory = "CategoryNameChanged"; expenseCategory.EditingName = "NameChanged"; expenseCategory.EditAmount = 10; expenseCategory.EditIsAmountType = false; bool result = await vm.SaveChanges(expenseCategory); Assert.IsTrue(result); using var verificationContext = factory.Create(); var existing = await verificationContext.ExpenseCategories.FindAsync(42); Assert.AreEqual("CategoryNameChanged", existing.CategoryName); Assert.AreEqual("NameChanged", existing.Name); Assert.AreEqual(0, existing.BudgetedAmount); Assert.AreEqual(10, existing.BudgetedPercentage); }
public async Task DeleteAccount_PostsNotification() { //Arrange var mocker = new AutoMocker().WithMessenger(); var watcher = new MessageWatcher <DatabaseEvent <Account> >(); mocker.Get <IMessenger>().Register(watcher); using var factory = mocker.WithDbScope(); var account1 = new Account(); using var setupContext = factory.Create(); setupContext.Accounts.Add(account1); await setupContext.SaveChangesAsync(); //Act using var actContext = factory.Create(); var account = await actContext.Accounts.FindAsync(account1.ID); actContext.Accounts.Remove(account); await actContext.SaveChangesAsync(); //Assert DatabaseEvent <Account>?message = watcher.Messages.Last(); Assert.AreEqual(account1.ID, message.Item.ID); Assert.AreEqual(EventType.Deleted, message.Type); }
public void Constructor_CreatesDependencies() { var mocker = new AutoMocker().WithMessenger(); using var scope = mocker.WithDbScope(); var vm = mocker.CreateInstance <MainWindowViewModel>(); Assert.IsNotNull(vm.Budget); Assert.IsNotNull(vm.History); Assert.IsNotNull(vm.Accounts); }
public async Task SaveChanges_CategoryNotFound_ReturnsFalse() { AutoMocker mocker = new AutoMocker().WithDefaults(); using var _ = mocker.WithDbScope(); var vm = mocker.CreateInstance <BudgetViewModel>(); var expenseCategory = await ExpenseCategoryViewModelEx.Create(mocker.Get <Func <BudgetContext> >(), new ExpenseCategory { ID = 42 }); bool result = await vm.SaveChanges(expenseCategory); Assert.IsFalse(result); }
public void Date_MonthsWithinRange_DoesNotShowWarning(int monthOffset) { var mocker = new AutoMocker().WithDefaults(); using var __ = mocker.WithAutoDIResolver(); using var _ = mocker.WithDbScope(); var vm = mocker.CreateInstance <AddItemViewModel>(); vm.Date = DateTime.Now.AddMonths(monthOffset); var errors = vm.GetErrors(nameof(vm.Date)).OfType <string>().ToList(); Assert.AreEqual(0, errors.Count); }
public void OnDoneAddingItemMessage_AddItemCleared() { var mocker = new AutoMocker().WithMessenger(); using var scope = mocker.WithDbScope(); var vm = mocker.CreateInstance <MainWindowViewModel>(); vm.AddItem = mocker.CreateInstance <AddItemViewModel>(); IMessenger messenger = mocker.Get <IMessenger>(); messenger.Send(new DoneAddingItemMessage()); Assert.IsNull(vm.AddItem); }
public void AddCommands_SetsAddItem(AddType?parameter, AddType expected) { var mocker = new AutoMocker() .WithDefaults(); using var scope = mocker.WithDbScope(); using var _ = mocker.WithAutoDIResolver(); var vm = mocker.CreateInstance <MainWindowViewModel>(); Assert.IsTrue(vm.ShowAddCommand.CanExecute(parameter)); vm.ShowAddCommand.Execute(parameter); Assert.IsNotNull(vm.AddItem); Assert.AreEqual(expected, vm.AddItem?.SelectedType); }
public void Date_MonthsOutOfRange_ShowsWarning(int monthOffset) { var now = DateTime.Now; var mocker = new AutoMocker().WithDefaults(); using var __ = mocker.WithAutoDIResolver(); using var _ = mocker.WithDbScope(); var vm = mocker.CreateInstance <AddItemViewModel>(); vm.Date = DateTime.Now.AddMonths(monthOffset); var errors = vm.GetErrors(nameof(vm.Date)).OfType <string>().ToList(); Assert.AreEqual(1, errors.Count); Assert.AreEqual($"Date should be between {now.AddMonths(-2).StartOfMonth():d} and {now.AddMonths(1).EndOfMonth():d}", errors[0]); }
public void RemovingItem_UpdatesRemainingAmount() { var mocker = new AutoMocker().WithDefaults(); using var _ = mocker.WithDbScope(); var vm = mocker.CreateInstance <AddItemViewModel>(); vm.SelectedType = AddType.Transaction; vm.AddItemCommand.Execute(null); vm.LineItems[0].Amount = 300; vm.LineItems[1].Amount = 400; vm.RemoveItemCommand.Execute(vm.LineItems[0]); Assert.AreEqual(400, vm.TotalAmount); }
public void TotalAmount_Transaction_UpdatesRemainingAmount() { var mocker = new AutoMocker().WithDefaults(); using var _ = mocker.WithDbScope(); using var __ = mocker.WithAutoDIResolver(); using var ___ = mocker.WithSynchonousTaskRunner(); var vm = mocker.CreateInstance <AddItemViewModel>(); vm.SelectedType = AddType.Transaction; vm.AddItemCommand.Execute(null); vm.TotalAmount = 100_00; vm.LineItems[0].Amount = 25_00; Assert.AreEqual(2, vm.LineItems.Count); Assert.AreEqual(25_00, vm.LineItems[0].Amount); Assert.AreEqual(0, vm.LineItems[1].Amount); Assert.AreEqual(75_00, vm.RemainingAmount); }
public async Task OnImport_WhenItemMatchesExistingTransaction_IsMarkedDone() { var mocker = new AutoMocker().WithDefaults(); using var factory = mocker.WithDbScope(); using var context = factory.Create(); var date = DateTime.Today.AddDays(-2); var category = new ExpenseCategory(); context.ExpenseCategories.Add(category); await context.SaveChangesAsync(); var item = new ExpenseCategoryItem { Date = date, Description = "Existing item", Details = new() { new ExpenseCategoryItemDetail { Amount = -10_71, ExpenseCategory = category } } }; context.ExpenseCategoryItems.Add(item); await context.SaveChangesAsync(); var vm = mocker.CreateInstance <ImportViewModel>(); vm.CsvData = TestCSVData.SingleTransaction; await vm.ImportCommand.ExecuteAsync(null); ImportItem importItem = vm.ImportedRecords.Single(); Assert.IsTrue(importItem.IsDone); }
public void Date_ChangingCurrentMonth_ClearsWarning() { var mocker = new AutoMocker().WithDefaults(); using var _ = mocker.WithDbScope(); using var __ = mocker.WithAutoDIResolver(); using var ___ = mocker.WithSynchonousTaskRunner(); ICurrentMonth current = mocker.Get <ICurrentMonth>(); var vm = mocker.CreateInstance <AddItemViewModel>(); current.CurrenMonth = DateTime.Now.AddMonths(-10); vm.Date = DateTime.Now; var errorsBefore = vm.GetErrors(nameof(vm.Date)).OfType <string>().ToList(); current.CurrenMonth = DateTime.Now; var errorsAfter = vm.GetErrors(nameof(vm.Date)).OfType <string>().ToList(); Assert.AreEqual(1, errorsBefore.Count); Assert.AreEqual(0, errorsAfter.Count); }
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);