From 07759f2a42f79d6c1070aeb79df48bda00f0d975 Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sun, 26 Nov 2023 18:50:57 -0600 Subject: [PATCH] ex5.6 --- teststock.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 teststock.py diff --git a/teststock.py b/teststock.py new file mode 100644 index 0000000..d860731 --- /dev/null +++ b/teststock.py @@ -0,0 +1,72 @@ +import unittest + +import stock + + +class TestStock(unittest.TestCase): + def test_create(self): + s = stock.Stock("GOOG", 100, 490.1) + self.assertEquals(s.name, "GOOG") + self.assertEquals(s.shares, 100) + self.assertEquals(s.price, 490.1) + + def test_create_keyword(self): + s = stock.Stock(name="GOOG", shares=100, price=490.1) + self.assertEquals(s.name, "GOOG") + self.assertEquals(s.shares, 100) + self.assertEquals(s.price, 490.1) + + def test_from_row(self): + s = stock.Stock.from_row(("GOOG", 100, 490.1)) + self.assertEquals(s.name, "GOOG") + self.assertEquals(s.shares, 100) + self.assertEquals(s.price, 490.1) + + def test_cost(self): + s = stock.Stock("GOOG", 100, 490.1) + self.assertEquals(s.cost, 49010) + + def test_sell(self): + s = stock.Stock("GOOG", 100, 490.1) + s.sell(50) + self.assertEqual(s.shares, 50) + + def test_repr(self): + s = stock.Stock("GOOG", 100, 490.1) + self.assertEquals(repr(s), "Stock('GOOG', 100, 490.1)") + + def test_eq(self): + s1 = stock.Stock("GOOG", 100, 490.1) + s2 = stock.Stock("GOOG", 100, 490.1) + s3 = stock.Stock("GOOG", 50, 490.1) + self.assertEqual(s1, s2) + self.assertNotEqual(s1, s3) + + def test_shares_type(self): + s = stock.Stock("GOOG", 100, 490.1) + with self.assertRaises(TypeError): + s.shares = "50" + + def test_shares_value(self): + s = stock.Stock("GOOG", 100, 490.1) + with self.assertRaises(ValueError): + s.shares = -50 + + def test_price_type(self): + s = stock.Stock("GOOG", 100, 490.1) + with self.assertRaises(TypeError): + s.price = "50" + + def test_price_value(self): + s = stock.Stock("GOOG", 100, 490.1) + with self.assertRaises(ValueError): + s.price = -90.9 + + def test_bad_attr(self): + s = stock.Stock("GOOG", 100, 490.1) + with self.assertRaises(AttributeError): + s.share = "foo" + + +if __name__ == "__main__": + unittest.main()