Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii committed Jun 5, 2024
1 parent 62a8e72 commit 6b91b6b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion labs_api/insights_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def save(
with Session(self.engine) as session:
cached = MarketInsightsResponseCacheModel(
market_id=market_insights.market_id,
datetime_=utcnow(),
datetime_=market_insights.created_at,
json_dump=market_insights.model_dump_json(),
)
session.add(cached)
Expand Down
60 changes: 60 additions & 0 deletions tests/test_insights_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from datetime import timedelta
from typing import Generator

import pytest
from prediction_market_agent_tooling.gtypes import HexAddress, HexStr
from prediction_market_agent_tooling.tools.utils import utcnow

from labs_api.insights_cache import MarketInsightsResponse, MarketInsightsResponseCache


@pytest.fixture(scope="session")
def market_insights_response_cache() -> (
Generator[MarketInsightsResponseCache, None, None]
):
"""Creates a in-memory SQLite DB for testing"""
db_storage = MarketInsightsResponseCache(sqlalchemy_db_url="sqlite://")
yield db_storage


def test_connects(
market_insights_response_cache: MarketInsightsResponseCache,
) -> None:
# Would raise an exception if it fails.
market_insights_response_cache.engine.connect()


def test_find_non_existent(
market_insights_response_cache: MarketInsightsResponseCache,
) -> None:
assert (
market_insights_response_cache.find(market_id=HexAddress(HexStr("0x0"))) is None
)


def test_save_and_find(
market_insights_response_cache: MarketInsightsResponseCache,
) -> None:
test_market_id = HexAddress(HexStr("0x1"))
dummy_response_4_days_old = MarketInsightsResponse(
market_id=test_market_id,
summary="summary",
results=[],
created_at=utcnow() - timedelta(days=5),
)
market_insights_response_cache.save(dummy_response_4_days_old)
assert (
market_insights_response_cache.find(market_id=test_market_id) is None
), "Should not find old data"

dummy_response_1_day_old = MarketInsightsResponse(
market_id=test_market_id,
summary="summary",
results=[],
created_at=utcnow() - timedelta(days=1),
)
market_insights_response_cache.save(dummy_response_1_day_old)
assert (
market_insights_response_cache.find(market_id=test_market_id)
== dummy_response_1_day_old
), "Should find fresh data"

0 comments on commit 6b91b6b

Please sign in to comment.