|
1 | | -import requests_cache |
2 | 1 | import os |
| 2 | +from datetime import date, timedelta |
| 3 | + |
3 | 4 | import pandas as pd |
4 | 5 | import pytest |
| 6 | +import requests_cache |
5 | 7 | from pandas_datareader import data as web |
6 | | -from datetime import date |
7 | | - |
8 | | -from allokation import ( |
9 | | - get_closing_price_from_yahoo, |
10 | | - map_columns_without_suffix, |
11 | | - transpose_prices, |
12 | | - calculate_multiplier, |
13 | | - calculate_amount, |
14 | | - calculate_total_for_each_ticker, |
15 | | - calculate_percentage_of_each_ticker |
16 | | -) |
| 8 | + |
| 9 | +from allokation.utils import (calculate_amount, calculate_multiplier, |
| 10 | + calculate_percentage_of_each_ticker, |
| 11 | + calculate_total_for_each_ticker, |
| 12 | + get_closing_price_from_yahoo, get_target_date, |
| 13 | + map_columns_without_suffix, transpose_prices) |
17 | 14 |
|
18 | 15 | STOCKS_DATA_FILEPATH = os.path.join(os.path.dirname(__file__), './data/stocks.csv') |
19 | 16 |
|
20 | 17 |
|
| 18 | +def test_get_target_date_when_today_is_a_weekday(): |
| 19 | + base_date = date(year=2020, month=9, day=4) |
| 20 | + expected = base_date |
| 21 | + |
| 22 | + result = get_target_date(base_date=base_date) |
| 23 | + |
| 24 | + assert result == expected |
| 25 | + |
| 26 | + |
| 27 | +def test_get_target_date_when_today_is_saturday(): |
| 28 | + base_date = date(year=2020, month=9, day=5) |
| 29 | + expected = base_date - timedelta(days=1) |
| 30 | + |
| 31 | + result = get_target_date(base_date=base_date) |
| 32 | + |
| 33 | + assert result == expected |
| 34 | + |
| 35 | + |
| 36 | +def test_get_target_date_when_today_is_sunday(): |
| 37 | + base_date = date(year=2020, month=9, day=6) |
| 38 | + expected = base_date - timedelta(days=2) |
| 39 | + |
| 40 | + result = get_target_date(base_date=base_date) |
| 41 | + |
| 42 | + assert result == expected |
| 43 | + |
| 44 | + |
21 | 45 | def test_get_closing_price_from_yahoo(mocker): |
22 | 46 | """ |
23 | 47 | The get_closing_price_from_yahoo() calls get_data_yahoo from pandas_datareader, which returns a pandas dataframe. |
@@ -45,7 +69,7 @@ def test_get_closing_price_from_yahoo(mocker): |
45 | 69 |
|
46 | 70 | expected = cached_df['Adj Close'] |
47 | 71 |
|
48 | | - mocker.patch('allokation.functions.web.get_data_yahoo', lambda tickers, date: cached_df) |
| 72 | + mocker.patch('allokation.utils.web.get_data_yahoo', lambda tickers, date: cached_df) |
49 | 73 | result = get_closing_price_from_yahoo(tickers, target_date) |
50 | 74 |
|
51 | 75 | assert result.equals(expected) |
|
0 commit comments