|
| 1 | +from datetime import date |
| 2 | + |
1 | 3 | from dsl.c10_working_with_data.e1007_manipulation import average_daily_change_by_month, DailyChange, \ |
2 | 4 | find_largest_and_smallest_changes, StockPrice, day_over_day_changes, max_prices_by_symbol, max_stock_price |
3 | 5 |
|
4 | 6 |
|
5 | 7 | def test_max_stock_price(): |
6 | | - data = [StockPrice("AAPL", "2023-10-01", 150.0), |
7 | | - StockPrice("AAPL", "2023-10-02", 155.0)] |
| 8 | + data = [StockPrice("AAPL", date(2023, 10, 1), 150.0), |
| 9 | + StockPrice("AAPL", date(2023, 10, 2), 155.0)] |
8 | 10 | assert max_stock_price(data, 'AAPL') == 155.0 |
9 | 11 |
|
| 12 | + |
10 | 13 | def test_max_prices_by_symbol(): |
11 | | - data = [StockPrice("AAPL", "2023-10-01", 150.0), |
12 | | - StockPrice("AAPL", "2023-10-02", 155.0), |
13 | | - StockPrice("MSFT", "2023-10-01", 300.0), |
14 | | - StockPrice("MSFT", "2023-10-02", 305.0)] |
| 14 | + data = [StockPrice("AAPL", date(2023, 10, 1), 150.0), |
| 15 | + StockPrice("AAPL", date(2023, 10, 2), 155.0), |
| 16 | + StockPrice("MSFT", date(2023, 10, 1), 300.0), |
| 17 | + StockPrice("MSFT", date(2023, 10, 2), 305.0)] |
15 | 18 | max_prices = max_prices_by_symbol(data) |
16 | 19 | assert max_prices['AAPL'] == 155.0 |
17 | 20 | assert max_prices['MSFT'] == 305.0 |
18 | 21 |
|
| 22 | + |
19 | 23 | def test_day_over_day_changes(): |
20 | | - prices = [StockPrice("AAPL", "2023-10-01", 150.0), |
21 | | - StockPrice("AAPL", "2023-10-02", 155.0)] |
| 24 | + prices = [StockPrice("AAPL", date(2023, 10, 1), 150.0), |
| 25 | + StockPrice("AAPL", date(2023, 10, 2), 155.0)] |
22 | 26 | changes = day_over_day_changes(prices) |
23 | 27 | assert len(changes) == 1 |
24 | 28 | assert changes[0].pct_change == (155.0 / 150.0 - 1) |
25 | 29 |
|
| 30 | + |
26 | 31 | def test_find_largest_and_smallest_changes(): |
27 | | - data = [StockPrice("AAPL", "2023-10-01", 150.0), |
28 | | - StockPrice("AAPL", "2023-10-02", 155.0), |
29 | | - StockPrice("AAPL", "2023-10-03", 140.0)] |
| 32 | + data = [StockPrice("AAPL", date(2023, 10, 1), 150.0), |
| 33 | + StockPrice("AAPL", date(2023, 10, 2), 155.0), |
| 34 | + StockPrice("AAPL", date(2023, 10, 3), 140.0)] |
30 | 35 | max_change, min_change = find_largest_and_smallest_changes(data) |
31 | 36 | assert max_change.pct_change == (155.0 / 150.0 - 1) |
32 | 37 | assert min_change.pct_change == (140.0 / 155.0 - 1) |
33 | 38 |
|
| 39 | + |
34 | 40 | def test_average_daily_change_by_month(): |
35 | 41 | changes = [ |
36 | | - DailyChange("AAPL", "2023-10-01", 0.05), |
37 | | - DailyChange("AAPL", "2023-10-02", -0.03) |
| 42 | + DailyChange("AAPL", date(2023, 10, 1), 0.05), |
| 43 | + DailyChange("AAPL", date(2023, 10, 2), -0.03) |
38 | 44 | ] |
39 | 45 | averages = average_daily_change_by_month(changes) |
40 | 46 | assert averages[10] == (0.05 - 0.03) / 2 |
41 | | - |
|
0 commit comments