fix: restore clear_cache and get_cache_stats methods in DatabentoHistoricalPriceSource
- Add back clear_cache method that was accidentally removed - Add file_count and total_size_bytes to get_cache_stats return value - Update tests for fixed March 2026 default dates
This commit is contained in:
@@ -318,9 +318,12 @@ class DatabentoHistoricalPriceSource:
|
|||||||
"""Get cache statistics."""
|
"""Get cache statistics."""
|
||||||
cache_dir = self.config.cache_dir
|
cache_dir = self.config.cache_dir
|
||||||
if not cache_dir.exists():
|
if not cache_dir.exists():
|
||||||
return {"status": "empty", "entries": []}
|
return {"status": "empty", "entries": [], "file_count": 0, "total_size_bytes": 0}
|
||||||
|
|
||||||
entries = []
|
entries = []
|
||||||
|
total_size = 0
|
||||||
|
file_count = 0
|
||||||
|
|
||||||
for meta_file in cache_dir.glob("*_meta.json"):
|
for meta_file in cache_dir.glob("*_meta.json"):
|
||||||
try:
|
try:
|
||||||
with open(meta_file) as f:
|
with open(meta_file) as f:
|
||||||
@@ -336,7 +339,39 @@ class DatabentoHistoricalPriceSource:
|
|||||||
"cost_usd": meta.get("cost_usd"),
|
"cost_usd": meta.get("cost_usd"),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
total_size += meta_file.stat().st_size
|
||||||
|
file_count += 1
|
||||||
except Exception:
|
except Exception:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
return {"status": "populated" if entries else "empty", "entries": entries}
|
# Count parquet files too
|
||||||
|
for parquet_file in cache_dir.glob("dbn_*.parquet"):
|
||||||
|
total_size += parquet_file.stat().st_size
|
||||||
|
file_count += 1
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "populated" if entries else "empty",
|
||||||
|
"entries": entries,
|
||||||
|
"file_count": file_count,
|
||||||
|
"total_size_bytes": total_size,
|
||||||
|
}
|
||||||
|
|
||||||
|
def clear_cache(self) -> int:
|
||||||
|
"""Clear all cache files.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Number of files deleted.
|
||||||
|
"""
|
||||||
|
cache_dir = self.config.cache_dir
|
||||||
|
if not cache_dir.exists():
|
||||||
|
return 0
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
for cache_file in cache_dir.glob("dbn_*.parquet"):
|
||||||
|
cache_file.unlink()
|
||||||
|
count += 1
|
||||||
|
for meta_file in cache_dir.glob("dbn_*_meta.json"):
|
||||||
|
meta_file.unlink()
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
return count
|
||||||
|
|||||||
@@ -134,25 +134,23 @@ class TestGetDefaultBacktestDates:
|
|||||||
start, end = get_default_backtest_dates()
|
start, end = get_default_backtest_dates()
|
||||||
assert start < end
|
assert start < end
|
||||||
|
|
||||||
def test_dates_approximately_two_years_apart(self) -> None:
|
def test_dates_are_fixed_march_2026(self) -> None:
|
||||||
"""Test that dates are approximately 2 years apart."""
|
"""Test that dates are fixed to March 2026 for testing."""
|
||||||
start, end = get_default_backtest_dates()
|
start, end = get_default_backtest_dates()
|
||||||
|
assert start == date(2026, 3, 2), f"Start should be 2026-03-02, got {start}"
|
||||||
|
assert end == date(2026, 3, 25), f"End should be 2026-03-25, got {end}"
|
||||||
delta = end - start
|
delta = end - start
|
||||||
# Should be approximately 730 days (2 years), allow small variance
|
assert delta.days == 23, f"Delta should be 23 days, got {delta.days}"
|
||||||
assert 700 <= delta.days <= 760, f"Delta is {delta.days} days"
|
|
||||||
|
|
||||||
def test_end_not_in_future(self) -> None:
|
def test_end_is_fixed_date(self) -> None:
|
||||||
"""Test that end date is not in the future."""
|
"""Test that end date is the fixed March 25 date."""
|
||||||
start, end = get_default_backtest_dates()
|
start, end = get_default_backtest_dates()
|
||||||
today = date.today()
|
assert end == date(2026, 3, 25)
|
||||||
assert end <= today
|
|
||||||
|
|
||||||
def test_end_is_friday_or_before(self) -> None:
|
def test_start_is_fixed_date(self) -> None:
|
||||||
"""Test that end date is a Friday or earlier in the week."""
|
"""Test that start date is the fixed March 2 date."""
|
||||||
start, end = get_default_backtest_dates()
|
start, end = get_default_backtest_dates()
|
||||||
# End should be on or before the most recent Friday at least a week old
|
assert start == date(2026, 3, 2)
|
||||||
# (implementation allows for flexible Friday calculation)
|
|
||||||
assert end.weekday() == 4 or end < date.today() # Friday == 4
|
|
||||||
|
|
||||||
|
|
||||||
class TestSymbolMinDates:
|
class TestSymbolMinDates:
|
||||||
|
|||||||
Reference in New Issue
Block a user