Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
Updates tests to provide instances of `PricesYahoo` with `calendars` and
`delays`. This removes reliance on some Yahoo API endpoints being
available to map symbols to calendars and delays.
  • Loading branch information
maread99 committed Oct 26, 2023
1 parent c83188c commit 5b35fc0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 14 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def _pickle_new_prices(symbols: str, path: pathlib.Path):
file.close()


CAL_START = "1986-03-13"
XLON = xcals.get_calendar("XLON", start=CAL_START)
CAL_MAPPING = {
"AZN.L": XLON,
"BARC.L": XLON,
"MSFT": xcals.get_calendar("XNYS", start=CAL_START),
"9988.HK": xcals.get_calendar("XHKG", start=CAL_START),
}
DELAY_MAPPING = {"AZN.L": 15, "BARC.L": 15, "MSFT": 0, "9988.HK": 15}


def _unpickle_prices(path: pathlib.Path, class_mocker) -> mp.PricesYahoo:
"""Reconstruct an instance of `PricesYahoo` to use in testing"""
file = open(path, "rb")
Expand All @@ -87,7 +98,9 @@ def _mock_now(*args, **kwargs) -> pd.Timestamp:

# reconstruct each of the data instances
symbols = pickle.load(file)
prices = mp.PricesYahoo(symbols)
calendars = {s: CAL_MAPPING[s] for s in symbols}
delays = {s: DELAY_MAPPING[s] for s in symbols}
prices = mp.PricesYahoo(symbols, calendars=calendars, delays=delays)

for bi in prices.bis:
data = prices._pdata[bi]
Expand Down
8 changes: 6 additions & 2 deletions tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,16 @@ def tz(self) -> abc.Iterator[ZoneInfo]:
yield ZoneInfo("Europe/London")

def test_constructor_raises(self):
prices = mp.PricesYahoo(
"AZN.L, MSFT", calendars=["XLON", "XNYS"], delays=[15, 0]
)
msg = re.escape(
"The Analysis class requires a `prices` instance that gets"
"price data for a single symbol, although the past instance"
" gets prices for 2: ['AZN.L', 'MSFT']."
)
with pytest.raises(ValueError, match=msg):
analysis.Analysis(mp.PricesYahoo("AZN.L, MSFT"))
analysis.Analysis(prices)

def test_prices(self, analy, prices_analysis):
assert analy.prices is prices_analysis
Expand Down Expand Up @@ -1399,13 +1402,14 @@ def tz(self) -> abc.Iterator[ZoneInfo]:
yield ZoneInfo("America/New_York")

def test_constructor_raises(self):
prices = mp.PricesYahoo("MSFT", calendars="XNYS", delays=0)
msg = re.escape(
"The Compare class requires a `prices` instance that gets"
"price data for multiple symbols, although the past instance"
"gets prices for only one: MSFT."
)
with pytest.raises(ValueError, match=msg):
analysis.Compare(mp.PricesYahoo("MSFT"))
analysis.Compare(prices)

def test_prices(self, analy, prices_compare):
assert analy.prices is prices_compare
Expand Down

0 comments on commit 5b35fc0

Please sign in to comment.