class StockTrendTest(unittest.TestCase): """The Stock Trend Test suite""" def setUp(self): """This method is called before every test method below is executed""" self.goog = Stock("GOOG") def given_a_series_of_prices(self, prices): """Helper method to keep our tests DRY""" timestamps = [datetime(2015, 5, 28), datetime(2015, 5, 29), datetime(2015, 5, 30)] for timestamp, price in zip(timestamps, prices): self.goog.update(timestamp, price) def test_increasing_trend_is_true_if_price_increase_for_3_updates(self): """Checks if a stock has an increasing trend""" self.given_a_series_of_prices([8, 10, 12]) self.assertTrue(self.goog.is_increasing_trend()) def test_increasing_trend_is_false_if_price_decreases(self): """Checks that increasing trend is false if price decreases""" self.given_a_series_of_prices([8, 12, 10]) self.assertFalse(self.goog.is_increasing_trend()) def test_increasing_trend_is_false_if_price_equal(self): """Checks that increasing trend is false if price remains equal""" self.given_a_series_of_prices([8, 10, 10]) self.assertFalse(self.goog.is_increasing_trend())
def test_a_PriceRule_is_False_if_the_stock_hasnt_got_an_update_yet(self): """ Tests if a PriceRule is False if the stock hasn't got an update yet """ self.exchange["AAPL"] = Stock("AAPL") rule = PriceRule("AAPL", lambda stock: stock.price > 10) self.assertFalse(rule.matches(self.exchange))
class StockTest(unittest.TestCase): """The Stock Test suite""" def setUp(self): """This method is called before every test method below is executed""" self.goog = Stock("GOOG") def test_price_of_a_new_stock_class_should_be_None(self): """Tests that a price of a new Stock should be None""" self.assertIsNone(self.goog.price) def test_stock_update(self): """An update should set the price on the stock object We will be using the `datetime` module for the timestamp """ # Following the Arrange-Act-Assert pattern # Arrange is in the setUp method above self.goog.update(datetime(2015, 5, 28), price=10) # Act self.assertEqual(10, self.goog.price) # Assert def test_negative_price_should_throw_ValueError(self): """Tests that a price should not be negative """ self.assertRaises(ValueError, self.goog.update, datetime(2015, 5, 28), -1) def test_stock_price_should_give_the_latest_price(self): """After multiple updates, a Stock gives us the latest price""" self.goog.update(datetime(2015, 5, 28), price=10) self.goog.update(datetime(2015, 5, 29), price=8.4) self.assertAlmostEqual(8.4, self.goog.price, delta=0.0001) def test_price_is_the_latest_even_if_updates_are_made_out_of_order(self): """Tests updates which come out of order""" self.goog.update(datetime(2015, 5, 29), price=8) self.goog.update(datetime(2015, 5, 28), price=10) self.assertEqual(8, self.goog.price)
def exchange(self): goog = Stock("GOOG") goog.update(datetime(2016, 9, 26), 11) return {"GOOG": goog}
def test_matches_is_false_if_stock_has_not_been_updated(self, exchange): exchange["AAPL"] = Stock("AAPL") rule = PriceRule("AAPL", lambda stock: stock.price > 10) assert rule.matches(exchange) is False
def setUp(self): """This method is called before every test method below is executed""" self.goog = Stock("GOOG")
def stock(): return Stock("GOOG")
def setUpClass(cls): """This method is called before every test method below is executed""" goog = Stock("GOOG") goog.update(datetime(2015, 5, 28), 11) cls.exchange = {"GOOG": goog}
def setUpClass(cls): """This method is called before every test method below is executed""" goog = Stock("GOOG") goog.update(datetime(2015, 5, 28), 8) goog.update(datetime(2015, 5, 29), 10) goog.update(datetime(2015, 5, 30), 12) msft = Stock("MSFT") msft.update(datetime(2015, 5, 28), 10) msft.update(datetime(2015, 5, 29), 10) msft.update(datetime(2015, 5, 30), 12) redhat = Stock("RHT") redhat.update(datetime(2015, 5, 28), 7) cls.exchange = {"GOOG": goog, "MSFT": msft, "RHT": redhat}
def test_price_of_another_stock_should_be_none(self): stock = Stock("AAPL") self.assertIsNone(stock.price)
def test_price_of_a_new_stock_class_should_be_none(self): stock = Stock("GOOG") self.assertIsNone(stock.price)
def setUpClass(cls): goog = Stock("GOOG") goog.update(datetime(2014,2,10), 11) cls.exchange = {"GOOG":goog}