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."""
|
||||
cache_dir = self.config.cache_dir
|
||||
if not cache_dir.exists():
|
||||
return {"status": "empty", "entries": []}
|
||||
return {"status": "empty", "entries": [], "file_count": 0, "total_size_bytes": 0}
|
||||
|
||||
entries = []
|
||||
total_size = 0
|
||||
file_count = 0
|
||||
|
||||
for meta_file in cache_dir.glob("*_meta.json"):
|
||||
try:
|
||||
with open(meta_file) as f:
|
||||
@@ -336,7 +339,39 @@ class DatabentoHistoricalPriceSource:
|
||||
"cost_usd": meta.get("cost_usd"),
|
||||
}
|
||||
)
|
||||
total_size += meta_file.stat().st_size
|
||||
file_count += 1
|
||||
except Exception:
|
||||
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()
|
||||
assert start < end
|
||||
|
||||
def test_dates_approximately_two_years_apart(self) -> None:
|
||||
"""Test that dates are approximately 2 years apart."""
|
||||
def test_dates_are_fixed_march_2026(self) -> None:
|
||||
"""Test that dates are fixed to March 2026 for testing."""
|
||||
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
|
||||
# Should be approximately 730 days (2 years), allow small variance
|
||||
assert 700 <= delta.days <= 760, f"Delta is {delta.days} days"
|
||||
assert delta.days == 23, f"Delta should be 23 days, got {delta.days}"
|
||||
|
||||
def test_end_not_in_future(self) -> None:
|
||||
"""Test that end date is not in the future."""
|
||||
def test_end_is_fixed_date(self) -> None:
|
||||
"""Test that end date is the fixed March 25 date."""
|
||||
start, end = get_default_backtest_dates()
|
||||
today = date.today()
|
||||
assert end <= today
|
||||
assert end == date(2026, 3, 25)
|
||||
|
||||
def test_end_is_friday_or_before(self) -> None:
|
||||
"""Test that end date is a Friday or earlier in the week."""
|
||||
def test_start_is_fixed_date(self) -> None:
|
||||
"""Test that start date is the fixed March 2 date."""
|
||||
start, end = get_default_backtest_dates()
|
||||
# End should be on or before the most recent Friday at least a week old
|
||||
# (implementation allows for flexible Friday calculation)
|
||||
assert end.weekday() == 4 or end < date.today() # Friday == 4
|
||||
assert start == date(2026, 3, 2)
|
||||
|
||||
|
||||
class TestSymbolMinDates:
|
||||
|
||||
Reference in New Issue
Block a user