diff --git a/src/pricehist/sources/yahoo.py b/src/pricehist/sources/yahoo.py index ae3179a..f93f53f 100644 --- a/src/pricehist/sources/yahoo.py +++ b/src/pricehist/sources/yahoo.py @@ -84,8 +84,10 @@ class Yahoo(BaseSource): def _amount(self, row, type): if type == "mid" and row["high"] != "null" and row["low"] != "null": return sum([Decimal(row["high"]), Decimal(row["low"])]) / 2 - else: + elif row[type] != "null": return Decimal(row[type]) + else: + return None def _data(self, series) -> (dict, csv.DictReader): base_url = "https://query1.finance.yahoo.com/v7/finance" diff --git a/tests/pricehist/sources/test_yahoo.py b/tests/pricehist/sources/test_yahoo.py index 51f6ca9..e1954dc 100644 --- a/tests/pricehist/sources/test_yahoo.py +++ b/tests/pricehist/sources/test_yahoo.py @@ -64,6 +64,13 @@ def long_ok(requests_mock): yield requests_mock +@pytest.fixture +def date_with_nulls_ok(requests_mock): + json = (Path(os.path.splitext(__file__)[0]) / "ibm-date-with-nulls.csv").read_text() + requests_mock.add(responses.GET, history_url("IBM"), body=json, status=200) + yield requests_mock + + def test_normalizesymbol(src): assert src.normalizesymbol("tsla") == "TSLA" @@ -163,6 +170,13 @@ def test_fetch_from_before_start(src, type, spark_ok, long_ok): assert len(series.prices) > 9 +def test_fetch_skips_dates_with_nulls(src, type, spark_ok, date_with_nulls_ok): + series = src.fetch(Series("IBM", "", type, "2021-01-05", "2021-01-07")) + assert series.prices[0] == Price("2021-01-05", Decimal("123.101204")) + assert series.prices[1] == Price("2021-01-07", Decimal("125.882545")) + assert len(series.prices) == 2 + + def test_fetch_to_future(src, type, spark_ok, recent_ok): series = src.fetch(Series("TSLA", "", type, "2021-01-04", "2100-01-08")) assert len(series.prices) > 0 diff --git a/tests/pricehist/sources/test_yahoo/ibm-date-with-nulls.csv b/tests/pricehist/sources/test_yahoo/ibm-date-with-nulls.csv new file mode 100644 index 0000000..601b395 --- /dev/null +++ b/tests/pricehist/sources/test_yahoo/ibm-date-with-nulls.csv @@ -0,0 +1,4 @@ +Date,Open,High,Low,Close,Adj Close,Volume +2021-01-05,125.010002,126.680000,124.610001,126.139999,123.101204,6114600 +2021-01-06,null,null,null,null,null,null +2021-01-07,130.039993,130.460007,128.259995,128.990005,125.882545,4507400