Ejemplo n.º 1
0
        public void BuyLogic()
        {
            IBroker broker = new TestBrokerage();
            var bank = new Bank();
            var account = bank.GetAccount(Guid.Empty);
            var stock = bank.GetStock("TST");

            account.Deposit(100);

            Maybe<IOrder> submitted = null;
            using (var reservation = account.Reserve(50, TimeSpan.MaxValue)) {
                Assert.IsTrue(reservation.Awarded);

                submitted = broker.SubmitBuyOrder("TST", account, 50).Result;

                Assert.IsFalse(submitted.Value.IsExecuted);
                Assert.IsFalse(submitted.Value.IsExecuted);
            }

            broker.CheckBuyOrder(submitted.Value.Id).Result
                .Match(
                    none: () => Assert.Fail("order not fulfilled"),
                    some: o => {
                        account.Adjust(o.Slippage);
                        stock.Purchase(o.Shares);

                        Assert.AreEqual(105, account.Available);
                    });
        }
Ejemplo n.º 2
0
        public void CanDeposit()
        {
            var bank = new Bank();
            var account = bank.OpenAccount();
            account.Deposit(10);

            Assert.AreEqual(10, account.Balance.Amount);
        }
Ejemplo n.º 3
0
        public void CanReturnReservation()
        {
            var bank = new Bank();
            var account = bank.OpenAccount(100);

            using (var reservation = account.Reserve(50, TimeSpan.MaxValue)) {
                Assert.IsTrue(reservation.Awarded);
                Assert.AreEqual(50, account.Available.Amount);
            }

            Assert.AreEqual(100, account.Available.Amount);
        }
Ejemplo n.º 4
0
        public void CanReserve()
        {
            var bank = new Bank();
            var account = bank.OpenAccount(100);

            using (var reservation = account.Reserve(50, TimeSpan.FromSeconds(1))) {
                Assert.IsTrue(reservation.Awarded);
                Assert.AreEqual(50, account.Available);

                Thread.Sleep(TimeSpan.FromSeconds(1));
                Assert.AreEqual(100, account.Available);
            }
        }
Ejemplo n.º 5
0
        public void CanAdjust()
        {
            var bank = new Bank();
            var account = bank.GetAccount(Guid.Empty);

            account.Adjust(10);
            Assert.AreEqual(10, account.Available);

            account.Adjust(-5);
            Assert.AreEqual(5, account.Available);

            account.Adjust(0);
            Assert.AreEqual(5, account.Available);
        }
Ejemplo n.º 6
0
        public void CanReserveMultiple()
        {
            var bank = new Bank();
            var account = bank.OpenAccount(100);

            using (var reserve50 = account.Reserve(50, TimeSpan.FromSeconds(1)))
            using (var reserve25 = account.Reserve(25, TimeSpan.FromSeconds(2))) {

                Assert.AreEqual(true, reserve50.Awarded);
                Assert.AreEqual(true, reserve25.Awarded);
                Assert.AreEqual(25, account.Available);

                Thread.Sleep(TimeSpan.FromSeconds(1));
                Assert.AreEqual(75, account.Available);

                Thread.Sleep(TimeSpan.FromSeconds(1));
                Assert.AreEqual(100, account.Available);
            }
        }
Ejemplo n.º 7
0
        public void SellLogic()
        {
            IBroker broker = new TestBrokerage();
            var bank = new Bank();
            var account = bank.GetAccount(Guid.Empty);
            var stock = bank.GetStock("TST");

            account.Deposit(100);

            var submitted = broker.SubmitSellOrder("TST", account, 1).Result;
            Assert.IsTrue(submitted.HasValue);
            Assert.IsFalse(submitted.Value.IsExecuted);

            broker.CheckSellOrder(submitted.Value.Id).Result
                .Match(
                    none: () => Assert.Fail("order not fulfilled"),
                    some: o => {
                        account.Adjust(o.Slippage);
                        stock.Sell(o.Shares);

                        Assert.AreEqual(109, account.Available);
                    });
        }
Ejemplo n.º 8
0
        public void CanSplitAccount()
        {
            var bank = new Bank();
            var account = bank.OpenAccount(100);
            var split = bank.SplitAccount(account, 50);

            Assert.AreEqual(50, account.Balance.Amount);
            Assert.AreEqual(50, split.Balance.Amount);
        }
Ejemplo n.º 9
0
 public void WithdrawEnforcesBalance()
 {
     var bank = new Bank();
     var account = bank.OpenAccount();
     account.Deposit(5M);
     account.Withdraw(10M);
 }