Skip to content

Commit fee9af0

Browse files
committed
Fix tests and a typo
1 parent d64777c commit fee9af0

File tree

6 files changed

+66
-52
lines changed

6 files changed

+66
-52
lines changed

tests/test_cache.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
python -m unittest tests.cache.TestCache
99
1010
"""
11-
from unittest import TestSuite
12-
1311
from tests.context import yfinance as yf
1412

1513
import unittest
@@ -48,46 +46,5 @@ def test_setTzCacheLocation(self):
4846
self.assertTrue(os.path.exists(os.path.join(self.tempCacheDir.name, "tkr-tz.db")))
4947

5048

51-
class TestCacheNoPermission(unittest.TestCase):
52-
@classmethod
53-
def setUpClass(cls):
54-
if os.name == "nt": # Windows
55-
cls.cache_path = "C:\\Windows\\System32\\yf-cache"
56-
else: # Unix/Linux/MacOS
57-
# Use a writable directory
58-
cls.cache_path = "/yf-cache"
59-
yf.set_tz_cache_location(cls.cache_path)
60-
61-
def test_tzCacheRootStore(self):
62-
# Test that if cache path in read-only filesystem, no exception.
63-
tkr = 'AMZN'
64-
tz1 = "America/New_York"
65-
66-
# During attempt to store, will discover cannot write
67-
yf.cache.get_tz_cache().store(tkr, tz1)
68-
69-
# Handling the store failure replaces cache with a dummy
70-
cache = yf.cache.get_tz_cache()
71-
self.assertTrue(cache.dummy)
72-
cache.store(tkr, tz1)
73-
74-
def test_tzCacheRootLookup(self):
75-
# Test that if cache path in read-only filesystem, no exception.
76-
tkr = 'AMZN'
77-
# During attempt to lookup, will discover cannot write
78-
yf.cache.get_tz_cache().lookup(tkr)
79-
80-
# Handling the lookup failure replaces cache with a dummy
81-
cache = yf.cache.get_tz_cache()
82-
self.assertTrue(cache.dummy)
83-
cache.lookup(tkr)
84-
85-
def suite():
86-
ts: TestSuite = unittest.TestSuite()
87-
ts.addTest(TestCache('Test cache'))
88-
ts.addTest(TestCacheNoPermission('Test cache no permission'))
89-
return ts
90-
91-
9249
if __name__ == '__main__':
9350
unittest.main()

tests/test_cache_noperms.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Tests for cache
3+
4+
To run all tests in suite from commandline:
5+
python -m unittest tests.cache
6+
7+
Specific test class:
8+
python -m unittest tests.cache.TestCache
9+
10+
"""
11+
from tests.context import yfinance as yf
12+
13+
import unittest
14+
import os
15+
16+
17+
class TestCacheNoPermission(unittest.TestCase):
18+
@classmethod
19+
def setUpClass(cls):
20+
if os.name == "nt": # Windows
21+
cls.cache_path = "C:\\Windows\\System32\\yf-cache"
22+
else: # Unix/Linux/MacOS
23+
# Use a writable directory
24+
cls.cache_path = "/yf-cache"
25+
yf.set_tz_cache_location(cls.cache_path)
26+
27+
def test_tzCacheRootStore(self):
28+
# Test that if cache path in read-only filesystem, no exception.
29+
tkr = 'AMZN'
30+
tz1 = "America/New_York"
31+
32+
# During attempt to store, will discover cannot write
33+
yf.cache.get_tz_cache().store(tkr, tz1)
34+
35+
# Handling the store failure replaces cache with a dummy
36+
cache = yf.cache.get_tz_cache()
37+
self.assertTrue(cache.dummy)
38+
cache.store(tkr, tz1)
39+
40+
def test_tzCacheRootLookup(self):
41+
# Test that if cache path in read-only filesystem, no exception.
42+
tkr = 'AMZN'
43+
# During attempt to lookup, will discover cannot write
44+
yf.cache.get_tz_cache().lookup(tkr)
45+
46+
# Handling the lookup failure replaces cache with a dummy
47+
cache = yf.cache.get_tz_cache()
48+
self.assertTrue(cache.dummy)
49+
cache.lookup(tkr)
50+
51+
if __name__ == '__main__':
52+
unittest.main()

tests/test_ticker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ def test_isin_info(self):
10341034
def test_empty_info(self):
10351035
# Test issue 2343 (Empty result _fetch)
10361036
data = self.tickers[10].info
1037-
self.assertCountEqual(["trailingPegRatio"], data.keys())
1037+
self.assertCountEqual(['quoteType', 'symbol', 'underlyingSymbol', 'uuid', 'maxAge', 'trailingPegRatio'], data.keys())
10381038
self.assertIn("trailingPegRatio", data.keys(), "Did not find expected key 'trailingPegRatio' in info dict")
10391039

10401040
# Test issue 2363 (Empty QuoteResponse)

yfinance/lookup.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import pandas as pd
2525

2626
from . import utils
27-
from .const import _QUERY1_URL_
27+
from .const import _QUERY1_URL_, _SENTINEL_
2828
from .data import YfData
2929
from .exceptions import YFException
3030

@@ -44,15 +44,19 @@ class Lookup:
4444
"""
4545

4646
def __init__(self, query: str, session=None, proxy=None, timeout=30, raise_errors=True):
47+
self.session = session
48+
self._data = YfData(session=self.session)
49+
50+
if proxy is not _SENTINEL_:
51+
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_proxy(proxy)")
52+
self._data._set_proxy(proxy)
53+
4754
self.query = query
4855

49-
self.session = session
50-
self.proxy = proxy
5156
self.timeout = timeout
5257
self.raise_errors = raise_errors
5358

5459
self._logger = utils.get_yf_logger()
55-
self._data = YfData(session=self.session)
5660

5761
self._cache = {}
5862

@@ -75,7 +79,7 @@ def _fetch_lookup(self, lookup_type="all", count=25) -> dict:
7579

7680
self._logger.debug(f'GET Lookup for ticker ({self.query}) with parameters: {str(dict(params))}')
7781

78-
data = self._data.get(url=url, params=params, proxy=self.proxy, timeout=self.timeout)
82+
data = self._data.get(url=url, params=params, timeout=self.timeout)
7983
if data is None or "Will be right back" in data.text:
8084
raise RuntimeError("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***\n"
8185
"Our engineers are working quickly to resolve "

yfinance/scrapers/history.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def history(self, period="1mo", interval="1d",
386386
msg = f'{self.ticker}: OHLC after combining events: {df.index[0]} -> {df.index[-1]}'
387387
logger.debug(msg)
388388

389-
df, last_trade = utils.fix_Yahoo_returning_live_separate(df, params["interval"], tz_exchange, repair=repair, currency=currency)
389+
df, last_trade = utils.fix_Yahoo_returning_live_separate(df, params["interval"], tz_exchange, prepost, repair=repair, currency=currency)
390390
if last_trade is not None:
391391
self._history_metadata['lastTrade'] = {'Price':last_trade['Close'], "Time":last_trade.name}
392392

yfinance/search.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ def __init__(self, query, max_results=8, news_count=8, lists_count=8, include_cb
4848
timeout: Request timeout in seconds (default 30).
4949
raise_errors: Raise exceptions on error (default True).
5050
"""
51+
self.session = session
52+
self._data = YfData(session=self.session)
53+
5154
if proxy is not _SENTINEL_:
5255
utils.print_once("YF deprecation warning: set proxy via new config function: yf.set_proxy(proxy)")
5356
self._data._set_proxy(proxy)
@@ -56,7 +59,6 @@ def __init__(self, query, max_results=8, news_count=8, lists_count=8, include_cb
5659
self.max_results = max_results
5760
self.enable_fuzzy_query = enable_fuzzy_query
5861
self.news_count = news_count
59-
self.session = session
6062
self.timeout = timeout
6163
self.raise_errors = raise_errors
6264

@@ -67,7 +69,6 @@ def __init__(self, query, max_results=8, news_count=8, lists_count=8, include_cb
6769
self.enable_cultural_assets = include_cultural_assets
6870
self.recommended = recommended
6971

70-
self._data = YfData(session=self.session)
7172
self._logger = utils.get_yf_logger()
7273

7374
self._response = {}

0 commit comments

Comments
 (0)