public async Task Test_Withdraw_exceeded() { //Arrange var options = new DbContextOptionsBuilder <BankAppDbContext>() .UseInMemoryDatabase(databaseName: "Test_Withdraw_exceed") .Options; //Act using (var context = new BankAppDbContext(options)) { var account = new Account { Balance = 10, }; context.Accounts.Add(account); await context.SaveChangesAsync(); var x = new CreateWithdrawCommandHandler(context); await x.Handle(new CreateWithdrawCommand { Amount = 20, AccountId = account.AccountId }, new CancellationToken()); } //Assert that method throws AmountExceedsBalanceException }
public async Task Test_Transfer_exceeded() { //Arrange var options = new DbContextOptionsBuilder <BankAppDbContext>() .UseInMemoryDatabase(databaseName: "Test_Transfer_exceeded") .Options; //Act using (var context = new BankAppDbContext(options)) { var senderAccount = new Account { Balance = 10, }; var recieverAccount = new Account { Balance = 0 }; context.AddRange(senderAccount, recieverAccount); await context.SaveChangesAsync(); var x = new CreateTransferCommandHandler(context); await x.Handle(new CreateTransferCommand { Amount = 20, SenderAccountId = senderAccount.AccountId, RecieverAccountId = recieverAccount.AccountId }, new CancellationToken()); } //Assert that method throws AmountExceedsBalanceException }
private void btb_addcredit_Click(object sender, EventArgs e) { using (BankAppDbContext bn = new BankAppDbContext()) { try { if (txbx_amount.Text != null) { Credit credit = new Credit() { Amount = Convert.ToDecimal(txbx_amount.Text), GivenDate = dateTimePicker1.Value, CustomerId = bn.Customers.ToList().Last().Id }; bn.Credits.Add(credit); bn.SaveChanges(); ShowAllForm showAllForm = new ShowAllForm(); showAllForm.ShowDialog(); } } catch (DbEntityValidationException exp) { foreach (var item in exp.EntityValidationErrors) { foreach (var i in item.ValidationErrors) { label2.Text += i.ErrorMessage + "\n"; } } } } }
private void btn_takecredit_Click(object sender, EventArgs e) { using (BankAppDbContext bc = new BankAppDbContext()) { try { Customer customer = new Customer() { Name = txbx_name.Text, Surname = txbx_surname.Text, PassportNumber = Convert.ToInt32(txbx_passportnumber.Text) }; bc.Customers.Add(customer); bc.SaveChanges(); TakeCreditForm takeCreditForm = new TakeCreditForm(); takeCreditForm.ShowDialog(); } catch (DbEntityValidationException exp) { foreach (var item in exp.EntityValidationErrors) { label4.Text = ""; foreach (var i in item.ValidationErrors) { label4.Text += i.ErrorMessage + "\n"; } } } } }
private void TakeCreditForm_Load(object sender, EventArgs e) { using (BankAppDbContext bc = new BankAppDbContext()) { List <Customer> customers = bc.Customers.Where(x => x.Id > 0).ToList(); comboBox1.Items.Add(bc.Customers.ToList().Last().Name); } }
public async Task <IActionResult> UserList([FromServices] BankAppDbContext context) { var model = await(from u in context.Users join ur in context.UserClaims on u.Id equals ur.UserId select new SelectListItem { Value = u.Id, Text = u.Email + "*" + ur.ClaimType }).ToListAsync(); return(View(model)); }
public async Task Test_Interest_With_Prior_Applied() { //Arrange var options = new DbContextOptionsBuilder <BankAppDbContext>() .UseInMemoryDatabase(databaseName: "Test_Interest_With_Prior_Applied") .Options; var accountCreatedClock = Substitute.For <IDateTime>(); accountCreatedClock.GetCurrentTime().Returns(new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Local)); var systemClockCheckInterestInitial = Substitute.For <IDateTime>(); systemClockCheckInterestInitial.GetCurrentTime().Returns(new DateTime(2010, 2, 1, 0, 0, 0, DateTimeKind.Local)); var systemClockCheckInterest = Substitute.For <IDateTime>(); systemClockCheckInterest.GetCurrentTime().Returns(new DateTime(2010, 4, 1, 0, 0, 0, DateTimeKind.Local)); decimal actual = 0; //Act using (var context = new BankAppDbContext(options)) { var account = new Account { Balance = 100, Created = accountCreatedClock.GetCurrentTime() }; context.Accounts.Add(account); await context.SaveChangesAsync(); var x = new CreateInterestCommandHandler(context); await x.Handle(new CreateInterestCommand { AccountId = account.AccountId, DateTimeProvider = systemClockCheckInterestInitial }, new CancellationToken()); await x.Handle(new CreateInterestCommand { AccountId = account.AccountId, DateTimeProvider = systemClockCheckInterest }, new CancellationToken()); actual = account.Balance; } // Assert equation: 2.3% interest applied daily since last interest credit. // This check creates an applies an initial interest credit, followed by a second one. // First one will create an intial once since account creation. // Second one will apply days since first interest was applied. // ((2.3% / 365) * account.Balance) * number of days since last credit decimal expected = 100.57m; Assert.AreEqual(expected, actual); }
public async Task <IActionResult> Delete(string userId, [FromServices] BankAppDbContext context) { var user = await context.Users.FindAsync(userId); var result = await _userManager.DeleteAsync(user); if (result.Succeeded) { TempData["successMessage"] = "Employee successfully deleted"; return(RedirectToAction("UserList")); } else { TempData["errorMessage"] = "Error deleting employee"; return(RedirectToAction("UserList")); } }
private void ShowAllForm_Load(object sender, EventArgs e) { using (BankAppDbContext bn = new BankAppDbContext()) { var names = bn.Customers.ToList(); var view = from cr in bn.Credits join cu in bn.Customers on cr.CustomerId equals cu.Id group cr by cu.Name into g select new { CustomerName = g.Key, TotalAmount = g.Sum(x => x.Amount) }; dataGridView1.DataSource = view.ToList(); } }
public async Task Test_Transaction_Created() { //Arrange var options = new DbContextOptionsBuilder <BankAppDbContext>() .UseInMemoryDatabase(databaseName: "Test_Withdraw_exceed") .Options; int actual = 0; //Act using (var context = new BankAppDbContext(options)) { var senderAccount = new Account { Balance = 100, }; var recieverAccount = new Account { Balance = 0 }; context.AddRange(senderAccount, recieverAccount); await context.SaveChangesAsync(); var withdrawCommand = new CreateWithdrawCommandHandler(context); await withdrawCommand.Handle(new CreateWithdrawCommand { Amount = 10, AccountId = senderAccount.AccountId }, new CancellationToken()); var depositCommand = new CreateDepositCommandHandler(context); await depositCommand.Handle(new CreateDepositCommand { Amount = 10, AccountId = senderAccount.AccountId }, new CancellationToken()); var transferCommand = new CreateTransferCommandHandler(context); await transferCommand.Handle(new CreateTransferCommand { Amount = 10, SenderAccountId = senderAccount.AccountId, RecieverAccountId = recieverAccount.AccountId }, new CancellationToken()); actual = await context.Transactions.CountAsync(); } //Assert that method creates 4 transfers int expected = 4; Assert.AreEqual(expected, actual); }
public async Task <IActionResult> UpdateUser(UpdateUserViewModel model, [FromServices] BankAppDbContext context) { if (!ModelState.IsValid) { return(View(model)); } else { var user = await context.Users.FindAsync(model.UserId); var claimToRemove = new Claim(model.OldClaim, ""); if (model.Password != null) { var result = await _userManager.RemovePasswordAsync(user); if (result.Succeeded) { await _userManager.AddPasswordAsync(user, model.Password); } } user.Email = model.Email; user.UserName = model.Email; user.PhoneNumber = model.PhoneNumber; await _userManager.RemoveClaimAsync(user, claimToRemove); await _userManager.AddClaimAsync(user, new Claim(model.RoleId, "")); var updateResult = await _userManager.UpdateAsync(user); if (updateResult.Succeeded) { return(RedirectToAction("userlist")); } else { ModelState.AddModelError("", "Failed to update employee"); return(View(model)); } } }