def test_stock_dividend_yield_preferred(self): """ Tests correct dividend yield calculation for `preferred` stock types. """ # Make a mock object for testing. sGIN = Stock('GIN', 'preferred', 8, 0.02, 100) # A stock without trades has a default ticker price equal to its par # value. In this case, `preferred` stock types should have a price # earnings ratio equal to their par value. self.assertEqual(sGIN.dividend_yield(), 0.02) # Add some mock Trades. sGIN.buy(320, 95) sGIN.sell(180, 110) self.assertEqual(len(sGIN._trades), 2) # `preferred` stocks should not use the `common` calculation for # dividend yields... self.assertNotEqual(sGIN.dividend_yield(), 8 / ((320*95 + 180*110) / (320+180))) # ... instead, they should use the `preferred` calculation. self.assertEqual(sGIN.dividend_yield(), (0.02 * 100) / ((320*95 + 180*110) / (320+180)))
def test_stock_dividend_yield_common(self): """ Tests correct dividend yield calculation for `common` stock types. """ # Make a mock object for testing. sALE = Stock('ALE', 'common', 23, nan, 60) # A stock without trades has a default ticker price equal to its par # value. self.assertEqual(sALE.dividend_yield(), 23. / 60) # Add some mock Trades. sALE.buy(500, 25) sALE.sell(300, 15) self.assertEqual(len(sALE._trades), 2) # The dividend yield calculation should now use a ticker price # determined from the average trading price. self.assertEqual(sALE.dividend_yield(), 23. / (((500*25)+(300*15))/(500+300)))