feat(kevin_bridge): blocklist + daily risk counters
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was canceled
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was canceled
This commit is contained in:
parent
3347847e38
commit
a417cae77b
4 changed files with 195 additions and 0 deletions
49
tests/services/kevin_signal_bridge/test_blocklist.py
Normal file
49
tests/services/kevin_signal_bridge/test_blocklist.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
"""Tests for KevinBlocklist using fakeredis."""
|
||||
|
||||
import fakeredis.aioredis
|
||||
|
||||
from services.kevin_signal_bridge.blocklist import KevinBlocklist
|
||||
|
||||
|
||||
async def _redis():
|
||||
return fakeredis.aioredis.FakeRedis()
|
||||
|
||||
|
||||
async def test_blocklist_add_and_is_blocked():
|
||||
redis = await _redis()
|
||||
bl = KevinBlocklist(redis)
|
||||
|
||||
assert await bl.is_blocked("NVDA") is False
|
||||
await bl.add("NVDA", ttl_days=7)
|
||||
assert await bl.is_blocked("NVDA") is True
|
||||
|
||||
|
||||
async def test_blocklist_remove():
|
||||
redis = await _redis()
|
||||
bl = KevinBlocklist(redis)
|
||||
|
||||
await bl.add("NVDA", ttl_days=7)
|
||||
await bl.remove("NVDA")
|
||||
assert await bl.is_blocked("NVDA") is False
|
||||
|
||||
|
||||
async def test_blocklist_active_set_returns_all_blocked():
|
||||
redis = await _redis()
|
||||
bl = KevinBlocklist(redis)
|
||||
|
||||
await bl.add("NVDA", ttl_days=7)
|
||||
await bl.add("AAPL", ttl_days=7)
|
||||
active = await bl.active_set()
|
||||
assert active == {"NVDA", "AAPL"}
|
||||
|
||||
|
||||
async def test_blocklist_ttl_expires():
|
||||
redis = await _redis()
|
||||
bl = KevinBlocklist(redis)
|
||||
|
||||
# ttl_days=0 -> 0 seconds; set should be cleared immediately by Redis
|
||||
# so use a small TTL and skip ahead. fakeredis supports `time` jump but
|
||||
# the simplest test asserts the key exists with TTL set
|
||||
await bl.add("NVDA", ttl_days=1)
|
||||
ttl = await redis.ttl("kevin:blocked:NVDA")
|
||||
assert ttl > 0
|
||||
55
tests/services/kevin_signal_bridge/test_risk_counters.py
Normal file
55
tests/services/kevin_signal_bridge/test_risk_counters.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
"""Tests for KevinRiskCounters using fakeredis."""
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
import fakeredis.aioredis
|
||||
|
||||
from services.kevin_signal_bridge.risk_counters import KevinRiskCounters
|
||||
|
||||
|
||||
async def _redis():
|
||||
return fakeredis.aioredis.FakeRedis()
|
||||
|
||||
|
||||
async def test_get_daily_trades_starts_at_zero():
|
||||
rc = KevinRiskCounters(await _redis())
|
||||
assert await rc.get_daily_trades() == 0
|
||||
|
||||
|
||||
async def test_increment_daily_trades_increments():
|
||||
rc = KevinRiskCounters(await _redis())
|
||||
n = await rc.increment_daily_trades()
|
||||
assert n == 1
|
||||
n2 = await rc.increment_daily_trades()
|
||||
assert n2 == 2
|
||||
assert await rc.get_daily_trades() == 2
|
||||
|
||||
|
||||
async def test_get_daily_alloc_starts_at_zero():
|
||||
rc = KevinRiskCounters(await _redis())
|
||||
assert await rc.get_daily_alloc() == Decimal("0")
|
||||
|
||||
|
||||
async def test_add_daily_alloc_accumulates():
|
||||
rc = KevinRiskCounters(await _redis())
|
||||
await rc.add_daily_alloc(Decimal("1000"))
|
||||
new = await rc.add_daily_alloc(Decimal("2500"))
|
||||
assert new == Decimal("3500")
|
||||
assert await rc.get_daily_alloc() == Decimal("3500")
|
||||
|
||||
|
||||
async def test_pause_flag_default_false():
|
||||
rc = KevinRiskCounters(await _redis())
|
||||
assert await rc.is_trading_paused() is False
|
||||
|
||||
|
||||
async def test_set_trading_paused_with_ttl_sets_flag():
|
||||
rc = KevinRiskCounters(await _redis())
|
||||
await rc.set_trading_paused(ttl_seconds=60)
|
||||
assert await rc.is_trading_paused() is True
|
||||
|
||||
|
||||
async def test_set_trading_paused_permanent_sets_flag():
|
||||
rc = KevinRiskCounters(await _redis())
|
||||
await rc.set_trading_paused()
|
||||
assert await rc.is_trading_paused() is True
|
||||
Loading…
Add table
Add a link
Reference in a new issue