-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
97 lines (74 loc) · 2.62 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import unittest
from classes.stocks import Stock
from classes.markets import Market
sample_common_stock = {
'type': 'common',
'last_dividend': 8,
'par_value': 100
}
sample_preffered_stock = {
'type': 'preferred',
'last_dividend': 8,
'fixed_dividend': 0.02,
'par_value': 100
}
class TestStockObject(unittest.TestCase):
def __init__(self, *args, **kwargs):
self.common_stock = Stock(
'COM', sample_common_stock)
self.preffered_stock = Stock(
'PER', sample_preffered_stock)
super(TestStockObject, self).__init__(*args, **kwargs)
def test_setprice(self):
self.common_stock.set_price(25)
self.assertEqual(self.common_stock.price, 25)
def test_dividend_yield(self):
self.common_stock.set_price(64)
self.preffered_stock.set_price(64)
self.assertEqual(
self.common_stock.get_dividend_yield(),
0.125)
self.assertEqual(
self.preffered_stock.get_dividend_yield(),
0.03125)
def test_pe_ratio(self):
self.common_stock.set_price(64)
self.preffered_stock.set_price(64)
self.assertEqual(
self.common_stock.get_pe_ratio(),
512)
self.assertEqual(
self.preffered_stock.get_pe_ratio(),
2048)
def test_volume_weighted_price(self):
self.common_stock.set_price(49)
self.common_stock.trade('BUY', 30)
self.common_stock.set_price(75)
self.common_stock.trade('BUY', 20)
self.assertEqual(len(self.common_stock.trades), 2)
self.assertEqual(
self.common_stock.volume_weighted_price(),
59.4)
class TestMarketObject(unittest.TestCase):
def __init__(self, *args, **kwargs):
self.common_stock = Stock(
'COM', sample_common_stock)
self.common_stock.set_price(25)
self.preffered_stock = Stock(
'PER', sample_preffered_stock)
self.preffered_stock.set_price(34)
self.market = Market()
super(TestMarketObject, self).__init__(*args, **kwargs)
def test_add_stock(self):
self.assertNotIn('COM', self.market.stocks)
self.market.add_stock(self.common_stock)
self.assertIn('COM', self.market.stocks)
def test_geometric_mean(self):
self.market.add_stock(self.common_stock)
self.assertEqual(self.market.geometric_mean(), 25)
self.market.add_stock(self.preffered_stock)
self.assertEqual(
self.market.geometric_mean(),
(25*34)**0.5)
if __name__ == '__main__':
unittest.main()