This commit is contained in:
Mike Bloy 2023-11-26 18:50:57 -06:00
parent db640c9cbf
commit 07759f2a42

72
teststock.py Normal file
View File

@ -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()