def test_GBCE_calculation(self):
        market1 = Market()
        stock1 = Stock("GOOG")
        stock2 = Stock("APPL")

        market1.add_stock_to_market(stock1)
        market1.add_stock_to_market(stock2)

        stock1.add_transaction('buy', 10, 100)
        stock2.add_transaction('buy', 10, 100)

        s1_price = stock1.price()
        s2_price = stock2.price()
        no_of_stocks = len(market1.list_stocks())
        asi = (s1_price * s2_price)**(1 / no_of_stocks)
        self.assertEqual(market1.all_share_index(), asi)
    def test_GBCE_calculation(self):
        market1 = Market()
        stock1 = Stock("GOOG")
        stock2 = Stock("APPL")

        market1.add_stock_to_market(stock1)
        market1.add_stock_to_market(stock2)

        stock1.add_transaction('buy', 10, 100)
        stock2.add_transaction('buy', 10, 100)

        s1_price = stock1.price()
        s2_price = stock2.price()
        no_of_stocks = len(market1.list_stocks())
        asi = (s1_price*s2_price)**(1/no_of_stocks)
        self.assertEqual(market1.all_share_index(), asi)
 def test_get_latest_transactions(self):
     time_now = datetime.datetime.now()
     stock1 = Stock('GOOG')
     stock1.add_transaction('buy', 10, 100, time_now)
     stock1.add_transaction('buy', 10, 100, time_now - datetime.timedelta(minutes=10))
     latest_transactions = stock1.get_transactions_for_last_x_min()
     self.assertEqual(len(latest_transactions), 2)
     stock1.add_transaction('buy', 10, 100, time_now - datetime.timedelta(minutes=30))
     self.assertEqual(len(latest_transactions), 2)
 def test_get_latest_transactions(self):
     time_now = datetime.datetime.now()
     stock1 = Stock('GOOG')
     stock1.add_transaction('buy', 10, 100, time_now)
     stock1.add_transaction('buy', 10, 100,
                            time_now - datetime.timedelta(minutes=10))
     latest_transactions = stock1.get_transactions_for_last_x_min()
     self.assertEqual(len(latest_transactions), 2)
     stock1.add_transaction('buy', 10, 100,
                            time_now - datetime.timedelta(minutes=30))
     self.assertEqual(len(latest_transactions), 2)
class TestTransactions(TestCase):

    def setUp(self):
        self.stock = Stock('GOOG')

    def test_add_transaction_to_stock(self):
        self.stock.add_transaction('buy', 12.3, 100)
        self.assertEqual(self.stock.transactions[-1].signal, 'buy')
        self.assertEqual(self.stock.transactions[-1].price, 12.3)
        self.assertEqual(self.stock.transactions[-1].volume, 100)

    def test_add_bad_transaction(self):
        self.assertRaises(ValueError ,self.stock.add_transaction('blah', 12.3, 100))
        self.assertRaises(ValueError, self.stock.add_transaction('buy', -1, 100))
        self.assertRaises(TypeError, self.stock.add_transaction('buy', 'asfkjh', 100))
        self.assertRaises(ValueError, self.stock.add_transaction('buy', 12.3, -1))
        self.assertRaises(TypeError, self.stock.add_transaction('buy', 12.3, 'dsfjh'))
class TestTransactions(TestCase):
    def setUp(self):
        self.stock = Stock('GOOG')

    def test_add_transaction_to_stock(self):
        self.stock.add_transaction('buy', 12.3, 100)
        self.assertEqual(self.stock.transactions[-1].signal, 'buy')
        self.assertEqual(self.stock.transactions[-1].price, 12.3)
        self.assertEqual(self.stock.transactions[-1].volume, 100)

    def test_add_bad_transaction(self):
        self.assertRaises(ValueError,
                          self.stock.add_transaction('blah', 12.3, 100))
        self.assertRaises(ValueError,
                          self.stock.add_transaction('buy', -1, 100))
        self.assertRaises(TypeError,
                          self.stock.add_transaction('buy', 'asfkjh', 100))
        self.assertRaises(ValueError,
                          self.stock.add_transaction('buy', 12.3, -1))
        self.assertRaises(TypeError,
                          self.stock.add_transaction('buy', 12.3, 'dsfjh'))
 def test_calculate_stock_price(self):
     stock1 = Stock("GOOG")
     stock1.add_transaction('buy', 10, 100)
     stock1.add_transaction('buy', 10, 100)
     stock_price = ((10*100)+(10*100))/(100+100.)
     self.assertEqual(stock1.price(), stock_price)
 def test_calculate_stock_price(self):
     stock1 = Stock("GOOG")
     stock1.add_transaction('buy', 10, 100)
     stock1.add_transaction('buy', 10, 100)
     stock_price = ((10 * 100) + (10 * 100)) / (100 + 100.)
     self.assertEqual(stock1.price(), stock_price)