Пример #1
0
        /// <summary>
        /// Mocking the context using in memory database with 4 accounts.
        /// Accounts ID 1-5. ID 5 has a balance of 100 000 and 1-4 500
        /// The tests are depending on the mocked accounts.
        /// Don't change the balance directly in this method.
        /// </summary>
        /// <returns>BankAppDataContext</returns>
        public static BankAppDataContext Get()
        {
            var options = new DbContextOptionsBuilder <BankAppDataContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;
            var context  = new BankAppDataContext(options);
            var accounts = new[]
            {
                new Account {
                    AccountId = 1, Balance = 500m, Frequency = "Monthly", Created = DateTime.Now
                },
                new Account {
                    AccountId = 2, Balance = 500m, Frequency = "Monthly", Created = DateTime.Now
                },
                new Account {
                    AccountId = 3, Balance = 500m, Frequency = "Monthly", Created = DateTime.Now
                },
                new Account {
                    AccountId = 4, Balance = 500m, Frequency = "Monthly", Created = DateTime.Now
                },
                new Account {
                    AccountId = 5, Balance = 100000m, Frequency = "Monthly", Created = DateTime.Now
                },
            };

            context.Accounts.AddRange(accounts);
            context.SaveChanges();
            return(context);
        }
        public void Transfer_CantTransferNegativeAmounts()
        {
            using (var context = new BankAppDataContext(_options))
            {
                // Arrange
                int accountIdFrom = 56789;
                int accountIdTo   = 67890;

                Account temporaryFromAccount = new Account();
                temporaryFromAccount.Balance   = 500.00m;
                temporaryFromAccount.AccountId = accountIdFrom;
                decimal expectedFrom = temporaryFromAccount.Balance;

                Account temporaryToAccount = new Account();
                temporaryToAccount.Balance   = 300.00m;
                temporaryToAccount.AccountId = accountIdTo;
                decimal expectedTo = temporaryToAccount.Balance;
                // Saving the account to DB
                context.Accounts.Add(temporaryFromAccount);
                context.Accounts.Add(temporaryToAccount);
                context.SaveChanges();
                decimal transferAmount = -800.00m;
                // Act
                Action act = () =>
                {
                    new CreateTransaction(context).Transfer(accountIdFrom, accountIdTo, transferAmount);
                };
                var fromAcc = context.Accounts.Where(a => a.AccountId == accountIdFrom).SingleOrDefault();
                var toAcc   = context.Accounts.Where(a => a.AccountId == accountIdTo).SingleOrDefault();
                // Assert
                Assert.Equal(expectedFrom, fromAcc.Balance);
                Assert.Equal(expectedTo, toAcc.Balance);
            }
        }
        [Fact] // TODO
        public void Withdrawal_CantWithdrawNegativeAmounts()
        {
            using (var context = new BankAppDataContext(_options))
            {
                int accountId = 23456;
                // Arrange
                Account temporaryAccount = new Account();
                temporaryAccount.Balance   = 500.00m;
                temporaryAccount.AccountId = accountId;
                decimal expectedBalance = temporaryAccount.Balance;

                // Saving the account to DB
                context.Accounts.Add(temporaryAccount);
                context.SaveChanges();

                decimal withdrawalAmount = -300.00m;

                // Act
                Action act = () =>
                {
                    new AccountHandler().Withdrawal(accountId, withdrawalAmount, context);
                };

                // Assert
                var account = context.Accounts.Where(a => a.AccountId == accountId).SingleOrDefault();
                Assert.Equal(expectedBalance, account.Balance);
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            using (var context = new BankAppDataContext())
            {
                var stopWatch = new Stopwatch();
                stopWatch.Start();
                //var kunder = context.Customers.Take(1000).ToList();
                var kunder = context.Customers
                             .Include(c => c.Dispositions)
                             .ThenInclude(d => d.Account)
                             .Take(1000).ToList();

                foreach (var kund in kunder)
                {
                    Console.WriteLine($"{kund.CustomerId} {kund.Givenname} {kund.Surname}");

                    foreach (var disposition in kund.Dispositions)
                    {
                        Console.WriteLine($"{disposition.AccountId} {disposition.Account.Created} {disposition.Account.Frequency}");
                    }
                }

                stopWatch.Stop();
                Console.WriteLine($"Tid: {stopWatch.Elapsed}ms");
            }
        }
Пример #5
0
        public void AccountBalanceDecreased_WhenUserWithdraws()
        {
            int accountId = 30;

            options = new DbContextOptionsBuilder <BankAppDataContext>()
                      .UseInMemoryDatabase(databaseName: "TestingDb")
                      .Options;
            using (var context = new BankAppDataContext(options))
            {
                accountQueriesHandler = new AccountQueriesHandler(context);
                accountCommandHandler = new AccountCommandHandler(context);

                context.Accounts.Add(new Account {
                    AccountId = accountId, Balance = 2000
                });
                context.SaveChanges();
            }

            using (var context = new BankAppDataContext(options))
            {
                accountQueriesHandler = new AccountQueriesHandler(context);
                accountCommandHandler = new AccountCommandHandler(context);

                decimal balanceBefore    = accountQueriesHandler.GetAccount(accountId).Balance;
                decimal withdrawalAmount = 1000;

                accountCommandHandler.Withdraw(accountId, withdrawalAmount);

                decimal balanceAfter = accountQueriesHandler.GetAccount(accountId).Balance;

                Assert.Less(balanceAfter, balanceBefore);
            }
        }
Пример #6
0
 public BankController(BankAppDataContext context, UserManager <ApplicationUser> userManager,
                       SignInManager <ApplicationUser> signInManager)
 {
     _context       = context;
     _userManager   = userManager;
     _signInManager = signInManager;
 }
Пример #7
0
 public AccountCommandHandler(BankAppDataContext context, ISystemClock systemClock)
 {
     this.context = context;
     this.accountQueriesHandler      = new AccountQueriesHandler(context);
     this.transactionCommandsHandler = new TransactionCommandsHandler(context);
     this.systemClock = systemClock;
 }
Пример #8
0
        public async Task CreateTransactionsRight_Tests(decimal amount, string type)
        {
            var command = new CreateTransactionCommand()
            {
                AccountId = 1, Amount = amount, Type = type
            };
            var options = new DbContextOptionsBuilder <BankAppDataContext>()
                          .UseInMemoryDatabase(databaseName: "CreateTransactionsRight_Tests")
                          .Options;

            using (var context = new BankAppDataContext(options))
            {
                await context.AddAsync(new Account()
                {
                    AccountId = 1, Balance = 1000
                });

                await context.SaveChangesAsync(CancellationToken.None);

                var handler = new CreateTransactionCommandHandler(context);

                var result = await handler.Handle(command, CancellationToken.None);

                decimal expected = 1100;
                var     account  = await context.Accounts.SingleOrDefaultAsync(a => a.AccountId == 1);

                var transactions = await context.Accounts.Include(a => a.Transactions).SingleOrDefaultAsync(a => a.AccountId == 1);

                var count  = transactions.Transactions.Count();
                var actual = account.Balance;
                Assert.Equal(expected, actual);
                Assert.Equal(1, count);
            }
        }
        public void Deposit_CantDepositwNegativeAmounts()
        {
            using (var context = new BankAppDataContext(_options))
            {
                // Arrange
                int accountId = 78901;

                Account temporaryAccount = new Account();
                temporaryAccount.Balance   = 500.00m;
                temporaryAccount.AccountId = accountId;
                decimal expected = temporaryAccount.Balance;

                // Saving the account to DB
                context.Accounts.Add(temporaryAccount);
                context.SaveChanges();
                decimal amount = -800.00m;
                // Act & Assert
                Action act = () =>
                {
                    bool actual = new AccountHandler().Deposit(accountId, amount, context);
                    Assert.False(actual);
                };
                var account = context.Accounts.Where(a => a.AccountId == temporaryAccount.AccountId).SingleOrDefault();
                // Assert
                Assert.Equal(expected, account.Balance);
            }
        }
Пример #10
0
 public AccountCommandHandler()
 {
     this.context = new BankAppDataContext();
     this.accountQueriesHandler      = new AccountQueriesHandler(context);
     this.transactionCommandsHandler = new TransactionCommandsHandler(context);
     this.systemClock = new SystemClock();
 }
Пример #11
0
        public AccountViewModel(int accountId, int customerId)
        {
            this.context          = new BankAppDataContext();
            this.customer_queries = new CustomerQueriesHandler(context);
            this.account_queries  = new AccountQueriesHandler(context);

            Account  = account_queries.GetAccountWithTransactions(accountId);
            Customer = customer_queries.GetCustomer(customerId);
        }
Пример #12
0
        public CustomerProfile(int customerId)
        {
            this.context             = new BankAppDataContext();
            this.customer_queries    = new CustomerQueriesHandler(context);
            this.disposition_queries = new DispositionQueriesHandler(context);

            Customer = customer_queries.GetCustomer(customerId);
            this.ConnectedDispositions = disposition_queries.GetConnectedDispositions(Customer.CustomerId);
        }
Пример #13
0
        public EditCustomerViewModel(int id)
        {
            this.context = new BankAppDataContext();
            this.customerCommandHandler = new CustomerCommandHandler(context);
            this.customerQueriesHandler = new CustomerQueriesHandler(context);

            Customer = customerQueriesHandler.GetCustomer(id);
            Birthday = (DateTime)Customer.Birthday;
        }
Пример #14
0
 public IndexViewModel()
 {
     this.context          = new BankAppDataContext();
     this.Account_Queries  = new AccountQueriesHandler(context);
     this.Customer_Queries = new CustomerQueriesHandler(context);
     this.TotalCustomers   = Customer_Queries.GetCustomersList().Count;
     this.TotalAccounts    = Account_Queries.GetAllAccounts().Count;
     this.TotalBalance     = Account_Queries.CalculateTotalBalance();
 }
Пример #15
0
        public BaseTest()
        {
            var options = new DbContextOptionsBuilder <BankAppDataContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .EnableSensitiveDataLogging()
                          .Options;

            ctxInMemmory = new BankAppDataContext(options);

            fixture.Behaviors.OfType <ThrowingRecursionBehavior>().ToList()
            .ForEach(b => fixture.Behaviors.Remove(b));
            fixture.Behaviors.Add(new OmitOnRecursionBehavior(1));
        }
Пример #16
0
        public void Setup()
        {
            options = new DbContextOptionsBuilder <BankAppDataContext>()
                      .UseInMemoryDatabase(databaseName: "TestingDb")
                      .Options;

            systemClock               = Substitute.For <ISystemClock>();
            context                   = new BankAppDataContext(options);
            accountQueriesHandler     = new AccountQueriesHandler(context);
            accountCommandHandler     = new AccountCommandHandler(context);
            customerCommandHandler    = new CustomerCommandHandler(context);
            customerQueriesHandler    = new CustomerQueriesHandler(context);
            dispositionQueriesHandler = new DispositionQueriesHandler(context);
        }
Пример #17
0
        private void UploadDocuments(ISearchIndexClient indexClient)
        {
            BankAppDataContext context = new BankAppDataContext();
            var searchCustomer         = context.Customers.Select(c => new SearchCustomer
            {
                CustomerId = c.CustomerId,
                Id         = c.CustomerId.ToString(),
                NationalId = c.NationalId,
                Name       = c.Givenname + " " + c.Surname,
                Address    = c.Streetaddress,
                City       = c.City
            }).ToArray();

            var batch = IndexBatch.Upload(searchCustomer);

            indexClient.Documents.Index(batch);
        }
Пример #18
0
        public void Setup()
        {
            var options = new DbContextOptionsBuilder <BankAppDataContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            _context = new BankAppDataContext(options);

            if (_context.Accounts.FirstOrDefault(x => x.AccountId == 2) == null)
            {
                _context.Accounts.Add(new Accounts {
                    AccountId = 2, Balance = 100
                });
                _context.SaveChanges();
            }

            _sut = new InsertService(_context);
        }
Пример #19
0
        public async Task CreateTransfer_Tests(decimal amount, int fromacc, int toacc)
        {
            var command = new CreateTransferCommand()
            {
                Amount = amount, FromAccountId = fromacc, ToAccountId = toacc
            };
            var options = new DbContextOptionsBuilder <BankAppDataContext>()
                          .UseInMemoryDatabase(databaseName: "CreateTransfer_Tests")
                          .Options;

            using (var context = new BankAppDataContext(options))
            {
                var handler = new CreateTransferCommandHandler(context);
                var result  = await handler.Handle(command, CancellationToken.None);

                Assert.False(result.Success);
            }
        }
Пример #20
0
        public async Task AddInterest_Tests(decimal yearlyint, int accountid)
        {
            var account = new Account()
            {
                AccountId = 1, Balance = 10000
            };
            var command = new AddInterestCommand()
            {
                AccountId      = accountid,
                YearlyInterest = yearlyint, LatestInterest = DateTime.Now
            };

            var options = new DbContextOptionsBuilder <BankAppDataContext>()
                          .UseInMemoryDatabase(databaseName: "AddInterest_Tests")
                          .Options;

            decimal expected;

            using (var context = new BankAppDataContext(options))
            {
                context.Add(new Account()
                {
                    AccountId = 1, Balance = 10000
                });
                context.SaveChanges();
                var machine = new MachineDateTime()
                {
                };
                machine.Now = machine.Now.AddDays(30);
                var handler = new AddInterestCommandHandler(context, machine);

                expected = 10039.73m;

                await handler.Handle(command, CancellationToken.None);

                var newbalance = await context.Accounts.SingleOrDefaultAsync(a => a.AccountId == 1);

                var test = Math.Round(newbalance.Balance, 2);
                Assert.Equal(expected, test);
            }
        }
Пример #21
0
        public void InterestIsCorrectlyApplied_WhenUserActivatesInterest()
        {
            int accountId = 78;

            options = new DbContextOptionsBuilder <BankAppDataContext>()
                      .UseInMemoryDatabase(databaseName: "TestingDb")
                      .Options;
            using (var context = new BankAppDataContext(options))
            {
                accountQueriesHandler = new AccountQueriesHandler(context);
                accountCommandHandler = new AccountCommandHandler(context, systemClock);

                context.Accounts.Add(new Account {
                    AccountId = accountId, Balance = 2000
                });
                context.SaveChanges();
            }

            using (var context = new BankAppDataContext(options))
            {
                var systemClock = Substitute.For <ISystemClock>();
                systemClock.GetCurrentTime().Returns(new DateTime(2020, 4, 4, 14, 0, 0, DateTimeKind.Utc));
                accountQueriesHandler = new AccountQueriesHandler(context);
                accountCommandHandler = new AccountCommandHandler(context, systemClock);

                decimal balanceBefore = accountQueriesHandler.GetAccount(accountId).Balance;

                double rate = 2;

                DateTime latestInterestDate = new DateTime(2018, 02, 02);
                var      currentDate        = new DateTime(2020, 4, 4, 14, 0, 0, DateTimeKind.Utc);

                accountCommandHandler.ApplyInterest(accountId, rate, latestInterestDate);

                decimal balanceAfter = accountQueriesHandler.GetAccount(accountId).Balance;
                double  days         = (currentDate - latestInterestDate).TotalDays;

                Assert.AreEqual(balanceAfter, Decimal.Round(balanceBefore + (decimal)((double)balanceBefore * rate / 100 / 365 * days), 2));
            }
        }
Пример #22
0
        public void Setup()
        {
            var options = new DbContextOptionsBuilder <BankAppDataContext>()
                          .UseInMemoryDatabase(databaseName: "BankAppData")
                          .Options;

            _context = new BankAppDataContext(options);

            if (_context.Accounts.FirstOrDefault(x => x.AccountId == 1) == null)
            {
                _context.Accounts.Add(new Accounts {
                    AccountId = 1, Balance = 100
                });
                _context.SaveChanges();
            }

            Mock <IAccountService> mockedAccountService = new Mock <IAccountService>();

            mockedAccountService.Setup(x => x.GetAccountBalanceByID(It.IsAny <int>())).Returns(100m);

            _sut = new WithdrawService(_context, mockedAccountService.Object);
        }
Пример #23
0
        public void TransactionIsCreated_WhenUserAppliesInterest()
        {
            int accountId = 77;

            options = new DbContextOptionsBuilder <BankAppDataContext>()
                      .UseInMemoryDatabase(databaseName: "TestingDb")
                      .Options;
            using (var context = new BankAppDataContext(options))
            {
                accountQueriesHandler = new AccountQueriesHandler(context);
                accountCommandHandler = new AccountCommandHandler(context, systemClock);

                context.Accounts.Add(new Account {
                    AccountId = accountId, Balance = 2000
                });
                context.SaveChanges();
            }

            using (var context = new BankAppDataContext(options))
            {
                var systemClock = Substitute.For <ISystemClock>();
                systemClock.GetCurrentTime().Returns(new DateTime(2020, 4, 4, 14, 0, 0, DateTimeKind.Utc));
                accountQueriesHandler = new AccountQueriesHandler(context);
                accountCommandHandler = new AccountCommandHandler(context, systemClock);

                int allTransactionsBefore = context.Transactions.CountAsync().Result;

                double   rate = 0.02;
                DateTime latestInterestDate = new DateTime(2018, 02, 02);
                var      currentDate        = DateTime.Now;
                accountCommandHandler.ApplyInterest(accountId, rate, latestInterestDate);

                int allTransactionsAfter = context.Transactions.CountAsync().Result;

                Assert.AreEqual(allTransactionsAfter, allTransactionsBefore + 1);
            }
        }
Пример #24
0
        public async Task CreateTransaction_Tests(decimal amount, string type)
        {
            var command = new CreateTransactionCommand()
            {
                AccountId = 1, Amount = amount, Type = type
            };
            var options = new DbContextOptionsBuilder <BankAppDataContext>()
                          .UseInMemoryDatabase(databaseName: "CreateTransaction_Tests")
                          .Options;

            using (var context = new BankAppDataContext(options))
            {
                context.Accounts.Add(new Account()
                {
                    AccountId = 1, Balance = 1000
                });

                var handler = new CreateTransactionCommandHandler(context);

                var result = await handler.Handle(command, CancellationToken.None);

                Assert.False(result.Success);
            }
        }
 public CustomerRepository(BankAppDataContext context)
 {
     _context = context;
 }
Пример #26
0
 public TransactionRepository(BankAppDataContext context)
 {
     _context = context;
 }
Пример #27
0
 public EditCustomerViewModel()
 {
     this.context = new BankAppDataContext();
     this.customerCommandHandler = new CustomerCommandHandler(context);
     this.customerQueriesHandler = new CustomerQueriesHandler(context);
 }
Пример #28
0
 public Repository(BankAppDataContext Context)
 {
     _context = Context;
 }
Пример #29
0
 public LoginRepository(BankAppDataContext context, UserManager <IdentityUser> userManager)
 {
     _context = context;
 }
Пример #30
0
 public InsertService(BankAppDataContext context)
 {
     _context = context;
 }