コード例 #1
0
        public void TestCalculatePortfolioCost()
        {
            {
                // -- case 1: mutiple buy trade records
                List <TradeHistory> histories = new List <TradeHistory>()
                {
                    new TradeHistory {
                        Ticker = "MSFT", TradeAction = TradeAction.Buy, Price = 85.95m, Quantity = 100
                    },
                    new TradeHistory {
                        Ticker = "MSFT", TradeAction = TradeAction.Buy, Price = 87.82m, Quantity = 150
                    }
                };
                var result = portfolioService.CalculatePortfolioCost(histories);

                Assert.AreEqual(21768m, result);
            }
            Init();
            {
                // -- case 2: mutiple buy and sell trade records
                List <TradeHistory> histories = new List <TradeHistory>()
                {
                    new TradeHistory {
                        Ticker = "MSFT", TradeAction = TradeAction.Buy, Price = 85.95m, Quantity = 100
                    },
                    new TradeHistory {
                        Ticker = "MSFT", TradeAction = TradeAction.Buy, Price = 87.82m, Quantity = 150
                    },
                    new TradeHistory {
                        Ticker = "MSFT", TradeAction = TradeAction.Sell, Price = 100.00m, Quantity = 100
                    }
                };
                var result = portfolioService.CalculatePortfolioCost(histories);

                Assert.AreEqual(11768m, result);
            }
            Init();
            {
                // -- case 3: mutiple buy and sell trade records for mutiple tickers
                List <TradeHistory> histories = new List <TradeHistory>()
                {
                    new TradeHistory {
                        Ticker = "MSFT", TradeAction = TradeAction.Buy, Price = 85.95m, Quantity = 100
                    },
                    new TradeHistory {
                        Ticker = "MSFT", TradeAction = TradeAction.Buy, Price = 87.82m, Quantity = 150
                    },
                    new TradeHistory {
                        Ticker = "GOOG", TradeAction = TradeAction.Buy, Price = 1065.00m, Quantity = 50
                    },
                };
                var result = portfolioService.CalculatePortfolioCost(histories);

                Assert.AreEqual(75018m, result, "Should return total cost for all records");
            }
        }