Step 2: Add seeding logic for pricing config

- Add seed_pricing_config() function to seed.py
- Seed initial values from shared/constants.json
- Add seeding to seed_e2e.py for E2E tests
- Add tests for seeding functionality
This commit is contained in:
counterweight 2025-12-26 20:11:00 +01:00
parent 32ce27180d
commit 74b934135a
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
3 changed files with 178 additions and 0 deletions

View file

@ -158,3 +158,103 @@ class TestPricingRepository:
result = await db.execute(select(PricingConfig))
all_configs = list(result.scalars().all())
assert len(all_configs) == 1
class TestPricingSeeding:
"""Test pricing configuration seeding."""
@pytest.mark.asyncio
async def test_seeding_creates_config_with_defaults(self, client_factory):
"""Seeding creates config with values from constants.json."""
import json
from pathlib import Path
# Load expected values from constants.json
constants_path = (
Path(__file__).parent.parent.parent / "shared" / "constants.json"
)
with constants_path.open() as f:
constants = json.load(f)
exchange_config = constants["exchange"]
expected_premium = exchange_config["premiumPercentage"]
expected_min_eur = exchange_config["eurTradeMin"]
expected_max_eur = exchange_config["eurTradeMax"]
async with client_factory.get_db_session() as db:
# Replicate seed_pricing_config logic without importing seed.py
repo = PricingRepository(db)
config = await repo.create_or_update(
premium_buy=expected_premium,
premium_sell=expected_premium,
small_trade_threshold_eur=0,
small_trade_extra_premium=0,
eur_min_buy=expected_min_eur * 100,
eur_max_buy=expected_max_eur * 100,
eur_min_sell=expected_min_eur * 100,
eur_max_sell=expected_max_eur * 100,
)
assert config is not None
assert config.premium_buy == expected_premium
assert config.premium_sell == expected_premium
assert config.eur_min_buy == expected_min_eur * 100
assert config.eur_max_buy == expected_max_eur * 100
assert config.eur_min_sell == expected_min_eur * 100
assert config.eur_max_sell == expected_max_eur * 100
assert config.small_trade_threshold_eur == 0
assert config.small_trade_extra_premium == 0
@pytest.mark.asyncio
async def test_re_running_seed_updates_existing_config(self, client_factory):
"""Re-running seed updates existing config instead of creating duplicate."""
import json
from pathlib import Path
constants_path = (
Path(__file__).parent.parent.parent / "shared" / "constants.json"
)
with constants_path.open() as f:
constants = json.load(f)
exchange_config = constants["exchange"]
expected_premium = exchange_config["premiumPercentage"]
expected_min_eur = exchange_config["eurTradeMin"]
expected_max_eur = exchange_config["eurTradeMax"]
async with client_factory.get_db_session() as db:
repo = PricingRepository(db)
# First seeding
config1 = await repo.create_or_update(
premium_buy=expected_premium,
premium_sell=expected_premium,
small_trade_threshold_eur=0,
small_trade_extra_premium=0,
eur_min_buy=expected_min_eur * 100,
eur_max_buy=expected_max_eur * 100,
eur_min_sell=expected_min_eur * 100,
eur_max_sell=expected_max_eur * 100,
)
original_id = config1.id
# Modify config manually
config1.premium_buy = 99
await db.commit()
# Re-run seed (update existing)
config2 = await repo.create_or_update(
premium_buy=expected_premium,
premium_sell=expected_premium,
small_trade_threshold_eur=0,
small_trade_extra_premium=0,
eur_min_buy=expected_min_eur * 100,
eur_max_buy=expected_max_eur * 100,
eur_min_sell=expected_min_eur * 100,
eur_max_sell=expected_max_eur * 100,
)
# Should be same record with updated values
assert config2.id == original_id
assert config2.premium_buy == expected_premium # Should be reset
assert config2.premium_buy != 99