|
2 | 2 | from datetime import date, timedelta |
3 | 3 |
|
4 | 4 | import pandas as pd |
5 | | -import pytest |
6 | 5 | import requests_cache |
7 | 6 | from pandas_datareader import data as web |
8 | 7 |
|
9 | | -from allokation.utils import (calculate_amount, calculate_multiplier, |
| 8 | +from allokation.utils import (calculate_amount, |
10 | 9 | calculate_percentage_of_each_ticker, |
11 | 10 | calculate_total_for_each_ticker, |
12 | | - get_closing_price_from_yahoo, get_target_date, |
13 | | - map_columns_without_suffix, transpose_prices) |
| 11 | + get_closing_price_from_yahoo, |
| 12 | + get_percentage_of_stocks, get_target_date, |
| 13 | + transpose_prices) |
14 | 14 |
|
15 | 15 | STOCKS_DATA_FILEPATH = os.path.join(os.path.dirname(__file__), './data/stocks.csv') |
16 | 16 |
|
17 | 17 |
|
| 18 | +def test_get_percentage_of_stocks_without_percentage_should_return_equal_distribution(): |
| 19 | + tickers = [ |
| 20 | + 'B3SA3.SA', |
| 21 | + 'BBDC4.SA', |
| 22 | + 'CSAN3.SA', |
| 23 | + 'CYRE3.SA', |
| 24 | + ] |
| 25 | + |
| 26 | + expected = 0.25 |
| 27 | + |
| 28 | + result = get_percentage_of_stocks(tickers=tickers) |
| 29 | + |
| 30 | + assert result == expected |
| 31 | + |
| 32 | + |
| 33 | +def test_get_percentage_of_stocks_with_percentage_should_return_pandas_series(): |
| 34 | + tickers = [ |
| 35 | + 'B3SA3.SA', |
| 36 | + 'BBDC4.SA', |
| 37 | + 'CSAN3.SA', |
| 38 | + 'CYRE3.SA', |
| 39 | + ] |
| 40 | + percentages = [40, 20, 20, 20] |
| 41 | + expected = pd.Series([0.4, 0.2, 0.2, 0.2]) |
| 42 | + |
| 43 | + result = get_percentage_of_stocks(tickers=tickers, percentages=percentages) |
| 44 | + |
| 45 | + assert result.equals(expected) |
| 46 | + |
| 47 | + |
18 | 48 | def test_get_target_date_when_today_is_a_weekday(): |
19 | 49 | base_date = date(year=2020, month=9, day=4) |
20 | 50 | expected = base_date |
@@ -88,42 +118,26 @@ def test_transpose_prices(): |
88 | 118 | assert result.equals(expected) |
89 | 119 |
|
90 | 120 |
|
91 | | -def test_map_columns_without_suffix(): |
92 | | - tickers = [ |
93 | | - 'MGLU3.SA', |
94 | | - 'PETR4.SA', |
95 | | - 'VVAR3.SA', |
96 | | - ] |
97 | | - |
98 | | - result = map_columns_without_suffix(tickers) |
99 | | - |
100 | | - expected = { |
101 | | - 'MGLU3.SA': 'MGLU3', |
102 | | - 'PETR4.SA': 'PETR4', |
103 | | - 'VVAR3.SA': 'VVAR3', |
104 | | - } |
105 | | - assert result == expected |
106 | | - |
107 | | - |
108 | | -def test_calculate_multiplier(): |
| 121 | +def test_calculate_amount_with_equal_distribution(): |
109 | 122 | df = pd.read_csv(STOCKS_DATA_FILEPATH) |
110 | | - number_of_tickers = len(df.values) |
111 | 123 | available_money = 1000 |
| 124 | + percentage_multiplier = 1/len(df) |
112 | 125 |
|
113 | | - expected = pytest.approx(3.57, 0.01) |
| 126 | + expected = (available_money*percentage_multiplier/df['price']).round(0) |
114 | 127 |
|
115 | | - result = calculate_multiplier(df, number_of_tickers, available_money) |
| 128 | + result = calculate_amount(df, available_money=available_money, percentage_multiplier=percentage_multiplier) |
116 | 129 |
|
117 | | - assert result == expected |
| 130 | + assert result.equals(expected) |
118 | 131 |
|
119 | 132 |
|
120 | 133 | def test_calculate_amount(): |
121 | 134 | df = pd.read_csv(STOCKS_DATA_FILEPATH) |
122 | | - multiplier = 1 |
| 135 | + available_money = 1000 |
| 136 | + percentage_multiplier = pd.Series([0.33, 0.33, 0.34]) |
123 | 137 |
|
124 | | - expected = (df['price'].max()/df['price']).round(0) |
| 138 | + expected = (available_money*percentage_multiplier/df['price']).round(0) |
125 | 139 |
|
126 | | - result = calculate_amount(df, multiplier=multiplier) |
| 140 | + result = calculate_amount(df, available_money=available_money, percentage_multiplier=percentage_multiplier) |
127 | 141 |
|
128 | 142 | assert result.equals(expected) |
129 | 143 |
|
|
0 commit comments